Skip to content

Commit d23300c

Browse files
committed
added test for correct reset
1 parent 1471a62 commit d23300c

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

packages/react-dom/src/__tests__/ReactDOMEventListener-test.js

+66-1
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,72 @@ describe('ReactDOMEventListener', () => {
453453
});
454454

455455
describe('Form resetting', () => {
456-
it('should not reset form if is prevented', async () => {
456+
it('should reset the form correctly', async () => {
457+
const container = document.createElement('div');
458+
document.body.appendChild(container);
459+
460+
try {
461+
const textRef = React.createRef();
462+
const buttonRef = React.createRef();
463+
464+
const handleChangeTextBox = jest.fn();
465+
466+
class Form extends React.Component {
467+
state = {text: 'test'};
468+
469+
render() {
470+
return (
471+
<form>
472+
<input
473+
ref={textRef}
474+
defaultValue="test"
475+
onChange={event => {
476+
this.setState({text: event.target.value});
477+
handleChangeTextBox(event);
478+
}}
479+
/>
480+
<button ref={buttonRef} type="reset">
481+
reset
482+
</button>
483+
</form>
484+
);
485+
}
486+
}
487+
488+
const root = ReactDOMClient.createRoot(container);
489+
await act(() => {
490+
root.render(<Form />);
491+
});
492+
493+
expect(textRef.current.value).toEqual('test');
494+
495+
await act(() => {
496+
setUntrackedValue.call(textRef.current, 'test1');
497+
dispatchEventOnNode(textRef.current, 'input');
498+
});
499+
500+
expect(handleChangeTextBox).toHaveBeenCalledTimes(1);
501+
expect(textRef.current.value).toEqual('test1');
502+
503+
await act(() => {
504+
buttonRef.current.click();
505+
});
506+
507+
expect(textRef.current.value).toEqual('test');
508+
509+
handleChangeTextBox.mockClear();
510+
await act(() => {
511+
setUntrackedValue.call(textRef.current, 'test');
512+
dispatchEventOnNode(textRef.current, 'input');
513+
});
514+
515+
expect(handleChangeTextBox).toHaveBeenCalledTimes(1);
516+
} finally {
517+
document.body.removeChild(container);
518+
}
519+
});
520+
521+
it('should not reset form if it is prevented', async () => {
457522
const container = document.createElement('div');
458523
document.body.appendChild(container);
459524

0 commit comments

Comments
 (0)