Skip to content

Commit

Permalink
feat:-added tests for more coverage in reactDom-input and reactText-a…
Browse files Browse the repository at this point in the history
…rea (#27796)

Small test similar to few tests added in #27740 , the `reactDom-input`
error message was just modified to match the error message, and the
`reactDomTextarea-test.js` has tests added to ensure more coverage.
  • Loading branch information
Biki-das authored Dec 8, 2023
1 parent af1fc87 commit a3aae7f
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/react-dom/src/__tests__/ReactDOMInput-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,32 @@ describe('ReactDOMInput', () => {
expect(() => {
ReactDOM.render(<input type="text" value={0} />, container);
}).toErrorDev(
'Warning: You provided a `value` prop to a form field without an `onChange` handler.',
'Warning: You provided a `value` prop to a form ' +
'field without an `onChange` handler. This will render a read-only ' +
'field. If the field should be mutable use `defaultValue`. ' +
'Otherwise, set either `onChange` or `readOnly`.',
);
});

it('should warn for controlled value of "" with missing onChange', () => {
expect(() => {
ReactDOM.render(<input type="text" value="" />, container);
}).toErrorDev(
'Warning: You provided a `value` prop to a form field without an `onChange` handler.',
'Warning: You provided a `value` prop to a form ' +
'field without an `onChange` handler. This will render a read-only ' +
'field. If the field should be mutable use `defaultValue`. ' +
'Otherwise, set either `onChange` or `readOnly`.',
);
});

it('should warn for controlled value of "0" with missing onChange', () => {
expect(() => {
ReactDOM.render(<input type="text" value="0" />, container);
}).toErrorDev(
'Warning: You provided a `value` prop to a form field without an `onChange` handler.',
'Warning: You provided a `value` prop to a form ' +
'field without an `onChange` handler. This will render a read-only ' +
'field. If the field should be mutable use `defaultValue`. ' +
'Otherwise, set either `onChange` or `readOnly`.',
);
});

Expand Down
81 changes: 81 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMTextarea-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -759,4 +759,85 @@ describe('ReactDOMTextarea', () => {
ReactDOM.render(<textarea defaultValue={null} />, container);
expect(node.defaultValue).toBe('');
});

it('should not warn about missing onChange if value is not set', () => {
expect(() => {
ReactTestUtils.renderIntoDocument(<textarea />);
}).not.toThrow();
});

it('should not warn about missing onChange if value is undefined', () => {
expect(() => {
ReactTestUtils.renderIntoDocument(<textarea value={undefined} />);
}).not.toThrow();
});

it('should not warn about missing onChange if onChange is set', () => {
expect(() => {
const change = jest.fn();
ReactTestUtils.renderIntoDocument(
<textarea value="something" onChange={change} />,
);
}).not.toThrow();
});

it('should not warn about missing onChange if disabled is true', () => {
expect(() => {
ReactTestUtils.renderIntoDocument(
<textarea value="something" disabled={true} />,
);
}).not.toThrow();
});

it('should not warn about missing onChange if value is not set', () => {
expect(() => {
ReactTestUtils.renderIntoDocument(
<textarea value="something" readOnly={true} />,
);
}).not.toThrow();
});

it('should warn about missing onChange if value is false', () => {
expect(() =>
ReactTestUtils.renderIntoDocument(<textarea value={false} />),
).toErrorDev(
'Warning: You provided a `value` prop to a form ' +
'field without an `onChange` handler. This will render a read-only ' +
'field. If the field should be mutable use `defaultValue`. ' +
'Otherwise, set either `onChange` or `readOnly`.',
);
});

it('should warn about missing onChange if value is 0', () => {
expect(() =>
ReactTestUtils.renderIntoDocument(<textarea value={0} />),
).toErrorDev(
'Warning: You provided a `value` prop to a form ' +
'field without an `onChange` handler. This will render a read-only ' +
'field. If the field should be mutable use `defaultValue`. ' +
'Otherwise, set either `onChange` or `readOnly`.',
);
});

it('should warn about missing onChange if value is "0"', () => {
expect(() =>
ReactTestUtils.renderIntoDocument(<textarea value="0" />),
).toErrorDev(
'Warning: You provided a `value` prop to a form ' +
'field without an `onChange` handler. This will render a read-only ' +
'field. If the field should be mutable use `defaultValue`. ' +
'Otherwise, set either `onChange` or `readOnly`.',
);
});

it('should warn about missing onChange if value is ""', () => {
expect(() =>
ReactTestUtils.renderIntoDocument(<textarea value="" />),
).toErrorDev(
'Warning: You provided a `value` prop to a form ' +
'field without an `onChange` handler. This will render a read-only ' +
'field. If the field should be mutable use `defaultValue`. ' +
'Otherwise, set either `onChange` or `readOnly`.',
);
});
});

0 comments on commit a3aae7f

Please sign in to comment.