Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test:- Added a test for Form reset & case of prevent default. #32689

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMEventListener-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,20 @@ describe('ReactDOMEventListener', () => {
let ReactDOMServer;
let act;
let simulateEventDispatch;
let setUntrackedValue;

function dispatchEventOnNode(node, type) {
node.dispatchEvent(new Event(type, {bubbles: true, cancelable: true}));
}

beforeEach(() => {
jest.resetModules();
React = require('react');
ReactDOM = require('react-dom');
setUntrackedValue = Object.getOwnPropertyDescriptor(
HTMLInputElement.prototype,
'value',
).set;
ReactDOMClient = require('react-dom/client');
ReactDOMServer = require('react-dom/server');
act = require('internal-test-utils').act;
Expand Down Expand Up @@ -443,6 +452,139 @@ describe('ReactDOMEventListener', () => {
}
});

describe('Form resetting', () => {
it('should reset the form correctly', async () => {
const container = document.createElement('div');
document.body.appendChild(container);

try {
const textRef = React.createRef();
const buttonRef = React.createRef();

const handleChangeTextBox = jest.fn();

class Form extends React.Component {
state = {text: 'test'};

render() {
return (
<form>
<input
ref={textRef}
defaultValue="test"
onChange={event => {
this.setState({text: event.target.value});
handleChangeTextBox(event);
}}
/>
<button ref={buttonRef} type="reset">
reset
</button>
</form>
);
}
}

const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<Form />);
});

expect(textRef.current.value).toEqual('test');

await act(() => {
setUntrackedValue.call(textRef.current, 'test1');
dispatchEventOnNode(textRef.current, 'input');
});

expect(handleChangeTextBox).toHaveBeenCalledTimes(1);
expect(textRef.current.value).toEqual('test1');

await act(() => {
buttonRef.current.click();
});

expect(textRef.current.value).toEqual('test');

handleChangeTextBox.mockClear();
await act(() => {
setUntrackedValue.call(textRef.current, 'test');
dispatchEventOnNode(textRef.current, 'input');
});

expect(handleChangeTextBox).toHaveBeenCalledTimes(1);
} finally {
document.body.removeChild(container);
}
});

it('should not reset form if it is prevented', async () => {
const container = document.createElement('div');
document.body.appendChild(container);

try {
const textRef = React.createRef();
const buttonRef = React.createRef();

const handleChangeTextBox = jest.fn();

class Form extends React.Component {
state = {text: 'test'};

render() {
return (
<form>
<input
ref={textRef}
defaultValue="test"
onChange={event => {
this.setState({text: event.target.value});
handleChangeTextBox(event);
}}
/>
<button
onClick={event => {
event.preventDefault();
}}
ref={buttonRef}
type="reset">
reset
</button>
</form>
);
}
}

const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<Form />);
});

await act(() => {
setUntrackedValue.call(textRef.current, 'test1');
dispatchEventOnNode(textRef.current, 'input');
});

expect(handleChangeTextBox).toHaveBeenCalledTimes(1);

await act(() => {
buttonRef.current.click();
});

expect(textRef.current.value).toEqual('test1');

await act(() => {
setUntrackedValue.call(textRef.current, 'test1');
dispatchEventOnNode(textRef.current, 'input');
});

expect(handleChangeTextBox).toHaveBeenCalledTimes(1);
} finally {
document.body.removeChild(container);
}
});
});

it('should dispatch loadstart only for media elements', async () => {
const container = document.createElement('div');
document.body.appendChild(container);
Expand Down
Loading