Skip to content

Commit fd7b4a5

Browse files
committed
Add failing tests
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.
1 parent bc4e751 commit fd7b4a5

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,78 @@ describe('ReactDOMInput', () => {
544544
expect(node.value).toBe('foobar');
545545
});
546546

547+
it('should not throw for date inputs even if `defaultValue` is an object where valueOf() throws', () => {
548+
class Foo {
549+
valueOf() {
550+
// Throwing here is the behavior of ECMAScript "Temporal" date/time API.
551+
// See https://tc39.es/proposal-temporal/docs/plaindate.html#valueOf
552+
throw new Error();
553+
}
554+
toString() {
555+
return '2020-01-01';
556+
}
557+
}
558+
const input = ReactDOM.render(
559+
<input defaultValue={new Foo()} type="date" />,
560+
container,
561+
);
562+
expect(input.value).toBe('2020-01-01');
563+
});
564+
565+
it('should not throw for text inputs even if `defaultValue` is an object where valueOf() throws', () => {
566+
class Foo {
567+
valueOf() {
568+
// Throwing here is the behavior of ECMAScript "Temporal" date/time API.
569+
// See https://tc39.es/proposal-temporal/docs/plaindate.html#valueOf
570+
throw new Error();
571+
}
572+
toString() {
573+
return '2020-01-01';
574+
}
575+
}
576+
const input = ReactDOM.render(
577+
<input defaultValue={new Foo()} type="text" />,
578+
container,
579+
);
580+
expect(input.value).toBe('2020-01-01');
581+
});
582+
583+
it('should not throw for date inputs even if `value` is an object where valueOf() throws', () => {
584+
class Foo {
585+
valueOf() {
586+
// Throwing here is the behavior of ECMAScript "Temporal" date/time API.
587+
// See https://tc39.es/proposal-temporal/docs/plaindate.html#valueOf
588+
throw new Error();
589+
}
590+
toString() {
591+
return '2020-01-01';
592+
}
593+
}
594+
const input = ReactDOM.render(
595+
<input value={new Foo()} type="date" readOnly={true} />,
596+
container,
597+
);
598+
expect(input.value).toBe('2020-01-01');
599+
});
600+
601+
it('should not throw for date inputs even if `value` is an object where valueOf() throws', () => {
602+
class Foo {
603+
valueOf() {
604+
// Throwing here is the behavior of ECMAScript "Temporal" date/time API.
605+
// See https://tc39.es/proposal-temporal/docs/plaindate.html#valueOf
606+
throw new Error();
607+
}
608+
toString() {
609+
return '2020-01-01';
610+
}
611+
}
612+
const input = ReactDOM.render(
613+
<input value={new Foo()} type="text" readOnly={true} />,
614+
container,
615+
);
616+
expect(input.value).toBe('2020-01-01');
617+
});
618+
547619
it('should display `value` of number 0', () => {
548620
const stub = <input type="text" value={0} onChange={emptyFunction} />;
549621
const node = ReactDOM.render(stub, container);

0 commit comments

Comments
 (0)