Skip to content

Commit d1c714d

Browse files
Yamin YassinYamin Yassin
authored andcommitted
Add Flow tests for the tightened prop and ref types
Now that the props are typed, pin the behaviour down. refs-and-props covers the host-element ref types and the props that should be accepted; expected-errors locks in the misuse Flow has to reject, so a future loosening of the types fails here instead of slipping through. Extend html-types-match with the new event payload types.
1 parent ee52885 commit d1c714d

3 files changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* Type-only NEGATIVE assertions. Each `$FlowExpectedError` is expected to fire,
8+
* proving the corresponding API is genuinely typed (not `any` / `$FlowFixMe`).
9+
*
10+
* @flow strict
11+
*/
12+
13+
import * as React from 'react';
14+
15+
import * as html from '../../native/html';
16+
17+
import type { StrictReactDOMProps } from '../StrictReactDOMProps';
18+
import type { StrictChangeEvent } from '../StrictReactDOMEvents';
19+
20+
// `role` must be a valid AriaRole, not an arbitrary number.
21+
// $FlowExpectedError[incompatible-type]
22+
export const badRole: StrictReactDOMProps['role'] = 42;
23+
24+
// The onChange payload `target.value` is a string, not a number.
25+
declare var changeEvent: StrictChangeEvent;
26+
// $FlowExpectedError[incompatible-type]
27+
export const wrongValueType: number = changeEvent.target.value;
28+
29+
// A ref to the wrong DOM element type is rejected (refs are not `any`).
30+
declare var inputRef: React.RefSetter<HTMLInputElement>;
31+
// $FlowExpectedError[incompatible-type]
32+
export const wrongRef: React.RefSetter<HTMLDivElement> = inputRef;
33+
34+
// `tabIndex` only accepts 0 or -1.
35+
// $FlowExpectedError[incompatible-type]
36+
export const badTabIndex: StrictReactDOMProps['tabIndex'] = 5;
37+
38+
// An html component rejects a ref of the wrong DOM element type.
39+
declare var divElementRef: React.RefSetter<HTMLDivElement>;
40+
export const inputRejectsWrongRef: React.Node = (
41+
// $FlowExpectedError[incompatible-type]
42+
<html.input ref={divElementRef} />
43+
);
44+
45+
// Pass-through handlers deliver an opaque event; a concrete param is rejected.
46+
export const opaqueHandlerRejectsConcreteParam: React.Node = (
47+
// $FlowExpectedError[incompatible-type]
48+
<html.div onScroll={(e: { clientX: number }) => {}} />
49+
);

‎packages/react-strict-dom/src/types/flowtests/html-types-match.js.flow‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ declare var main_web: WebHTML['main'];
177177
declare var main_native: NativeHTML['main'];
178178
(main_native as WebHTML['main']);
179179

180+
declare var mark_web: WebHTML['mark'];
181+
(mark_web as NativeHTML['mark']);
182+
declare var mark_native: NativeHTML['mark'];
183+
(mark_native as WebHTML['mark']);
184+
180185
declare var nav_web: WebHTML['nav'];
181186
(nav_web as NativeHTML['nav']);
182187
declare var nav_native: NativeHTML['nav'];
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* Type-only POSITIVE assertions for the strict-dom public API. These guard
8+
* against regressions in the component ref typing and the event-payload typing.
9+
*
10+
* @flow strict
11+
*/
12+
13+
import * as React from 'react';
14+
15+
import * as html from '../../native/html';
16+
17+
import type { StrictReactDOMProps } from '../StrictReactDOMProps';
18+
import type { StrictReactDOMInputProps } from '../StrictReactDOMInputProps';
19+
import type {
20+
StrictChangeEvent,
21+
StrictImageLoadEvent,
22+
StrictKeyEvent
23+
} from '../StrictReactDOMEvents';
24+
25+
// The public ref type is the DOM element type, not `any`. (web<->native parity
26+
// is enforced in html-types-match.js.flow.)
27+
declare var divRef: React.RefSetter<HTMLDivElement>;
28+
export const acceptsDomRef: React.RefSetter<HTMLDivElement> = divRef;
29+
30+
// An html component accepts a ref of its DOM element type.
31+
declare var inputElementRef: React.RefSetter<HTMLInputElement>;
32+
export const inputAcceptsItsRef: React.Node = (
33+
<html.input ref={inputElementRef} />
34+
);
35+
36+
// A pass-through handler with no/opaque param is accepted.
37+
export const acceptsOpaqueHandler: React.Node = (
38+
<html.div onScroll={() => {}} />
39+
);
40+
41+
// Re-synthesized event payloads are concretely typed.
42+
declare var changeEvent: StrictChangeEvent;
43+
export const changeValue: string = changeEvent.target.value;
44+
export const changeType: 'change' = changeEvent.type;
45+
46+
declare var loadEvent: StrictImageLoadEvent;
47+
export const naturalWidth: ?number = loadEvent.target.naturalWidth;
48+
49+
declare var keyEvent: StrictKeyEvent;
50+
export const keyName: string = keyEvent.key;
51+
52+
// Valid global + input prop objects compile.
53+
export const validProps: StrictReactDOMProps = {
54+
id: 'x',
55+
role: 'button',
56+
tabIndex: 0
57+
};
58+
59+
export const validInputProps: StrictReactDOMInputProps = {
60+
value: 'hello',
61+
disabled: true,
62+
type: 'text'
63+
};

0 commit comments

Comments
 (0)