Skip to content

Commit

Permalink
Add failing tests
Browse files Browse the repository at this point in the history
Added tests for date and text inputs for both the value and defaultValue
props. The mock object used for those props approximates the behavior
of Temporal.PlainDate whose `valueOf` method will throw.
  • Loading branch information
justingrant committed Aug 16, 2021
1 parent bc4e751 commit fd7b4a5
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMInput-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,78 @@ describe('ReactDOMInput', () => {
expect(node.value).toBe('foobar');
});

it('should not throw for date inputs even if `defaultValue` is an object where valueOf() throws', () => {
class Foo {
valueOf() {
// Throwing here is the behavior of ECMAScript "Temporal" date/time API.
// See https://tc39.es/proposal-temporal/docs/plaindate.html#valueOf
throw new Error();
}
toString() {
return '2020-01-01';
}
}
const input = ReactDOM.render(
<input defaultValue={new Foo()} type="date" />,
container,
);
expect(input.value).toBe('2020-01-01');
});

it('should not throw for text inputs even if `defaultValue` is an object where valueOf() throws', () => {
class Foo {
valueOf() {
// Throwing here is the behavior of ECMAScript "Temporal" date/time API.
// See https://tc39.es/proposal-temporal/docs/plaindate.html#valueOf
throw new Error();
}
toString() {
return '2020-01-01';
}
}
const input = ReactDOM.render(
<input defaultValue={new Foo()} type="text" />,
container,
);
expect(input.value).toBe('2020-01-01');
});

it('should not throw for date inputs even if `value` is an object where valueOf() throws', () => {
class Foo {
valueOf() {
// Throwing here is the behavior of ECMAScript "Temporal" date/time API.
// See https://tc39.es/proposal-temporal/docs/plaindate.html#valueOf
throw new Error();
}
toString() {
return '2020-01-01';
}
}
const input = ReactDOM.render(
<input value={new Foo()} type="date" readOnly={true} />,
container,
);
expect(input.value).toBe('2020-01-01');
});

it('should not throw for date inputs even if `value` is an object where valueOf() throws', () => {
class Foo {
valueOf() {
// Throwing here is the behavior of ECMAScript "Temporal" date/time API.
// See https://tc39.es/proposal-temporal/docs/plaindate.html#valueOf
throw new Error();
}
toString() {
return '2020-01-01';
}
}
const input = ReactDOM.render(
<input value={new Foo()} type="text" readOnly={true} />,
container,
);
expect(input.value).toBe('2020-01-01');
});

it('should display `value` of number 0', () => {
const stub = <input type="text" value={0} onChange={emptyFunction} />;
const node = ReactDOM.render(stub, container);
Expand Down

0 comments on commit fd7b4a5

Please sign in to comment.