Skip to content

Commit a3aae7f

Browse files
authored
feat:-added tests for more coverage in reactDom-input and reactText-area (#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.
1 parent af1fc87 commit a3aae7f

File tree

2 files changed

+93
-3
lines changed

2 files changed

+93
-3
lines changed

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

+12-3
Original file line numberDiff line numberDiff line change
@@ -110,23 +110,32 @@ describe('ReactDOMInput', () => {
110110
expect(() => {
111111
ReactDOM.render(<input type="text" value={0} />, container);
112112
}).toErrorDev(
113-
'Warning: You provided a `value` prop to a form field without an `onChange` handler.',
113+
'Warning: You provided a `value` prop to a form ' +
114+
'field without an `onChange` handler. This will render a read-only ' +
115+
'field. If the field should be mutable use `defaultValue`. ' +
116+
'Otherwise, set either `onChange` or `readOnly`.',
114117
);
115118
});
116119

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

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

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

+81
Original file line numberDiff line numberDiff line change
@@ -759,4 +759,85 @@ describe('ReactDOMTextarea', () => {
759759
ReactDOM.render(<textarea defaultValue={null} />, container);
760760
expect(node.defaultValue).toBe('');
761761
});
762+
763+
it('should not warn about missing onChange if value is not set', () => {
764+
expect(() => {
765+
ReactTestUtils.renderIntoDocument(<textarea />);
766+
}).not.toThrow();
767+
});
768+
769+
it('should not warn about missing onChange if value is undefined', () => {
770+
expect(() => {
771+
ReactTestUtils.renderIntoDocument(<textarea value={undefined} />);
772+
}).not.toThrow();
773+
});
774+
775+
it('should not warn about missing onChange if onChange is set', () => {
776+
expect(() => {
777+
const change = jest.fn();
778+
ReactTestUtils.renderIntoDocument(
779+
<textarea value="something" onChange={change} />,
780+
);
781+
}).not.toThrow();
782+
});
783+
784+
it('should not warn about missing onChange if disabled is true', () => {
785+
expect(() => {
786+
ReactTestUtils.renderIntoDocument(
787+
<textarea value="something" disabled={true} />,
788+
);
789+
}).not.toThrow();
790+
});
791+
792+
it('should not warn about missing onChange if value is not set', () => {
793+
expect(() => {
794+
ReactTestUtils.renderIntoDocument(
795+
<textarea value="something" readOnly={true} />,
796+
);
797+
}).not.toThrow();
798+
});
799+
800+
it('should warn about missing onChange if value is false', () => {
801+
expect(() =>
802+
ReactTestUtils.renderIntoDocument(<textarea value={false} />),
803+
).toErrorDev(
804+
'Warning: You provided a `value` prop to a form ' +
805+
'field without an `onChange` handler. This will render a read-only ' +
806+
'field. If the field should be mutable use `defaultValue`. ' +
807+
'Otherwise, set either `onChange` or `readOnly`.',
808+
);
809+
});
810+
811+
it('should warn about missing onChange if value is 0', () => {
812+
expect(() =>
813+
ReactTestUtils.renderIntoDocument(<textarea value={0} />),
814+
).toErrorDev(
815+
'Warning: You provided a `value` prop to a form ' +
816+
'field without an `onChange` handler. This will render a read-only ' +
817+
'field. If the field should be mutable use `defaultValue`. ' +
818+
'Otherwise, set either `onChange` or `readOnly`.',
819+
);
820+
});
821+
822+
it('should warn about missing onChange if value is "0"', () => {
823+
expect(() =>
824+
ReactTestUtils.renderIntoDocument(<textarea value="0" />),
825+
).toErrorDev(
826+
'Warning: You provided a `value` prop to a form ' +
827+
'field without an `onChange` handler. This will render a read-only ' +
828+
'field. If the field should be mutable use `defaultValue`. ' +
829+
'Otherwise, set either `onChange` or `readOnly`.',
830+
);
831+
});
832+
833+
it('should warn about missing onChange if value is ""', () => {
834+
expect(() =>
835+
ReactTestUtils.renderIntoDocument(<textarea value="" />),
836+
).toErrorDev(
837+
'Warning: You provided a `value` prop to a form ' +
838+
'field without an `onChange` handler. This will render a read-only ' +
839+
'field. If the field should be mutable use `defaultValue`. ' +
840+
'Otherwise, set either `onChange` or `readOnly`.',
841+
);
842+
});
762843
});

0 commit comments

Comments
 (0)