Skip to content

Commit ad20004

Browse files
mirkamikeybinns
andcommitted
Add static type tests
Co-authored-by: Mikey Binns <[email protected]>
1 parent 34ca3a0 commit ad20004

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

packages/components/src/select-control/test/select-control.tsx

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,129 @@ describe( 'SelectControl', () => {
8181
expect.anything()
8282
);
8383
} );
84+
85+
/* eslint-disable jest/expect-expect */
86+
describe( 'static typing', () => {
87+
describe( 'single', () => {
88+
it( 'should infer the value type from available `options`, but not the `value` or `onChange` prop', () => {
89+
const onChange: ( value: 'foo' | 'bar' ) => void = () => {};
90+
91+
<SelectControl
92+
value="narrow"
93+
options={ [
94+
{
95+
value: 'narrow',
96+
label: 'Narrow',
97+
},
98+
{
99+
value: 'value',
100+
label: 'Value',
101+
},
102+
] }
103+
// @ts-expect-error onChange type is not compatible with inferred value type
104+
onChange={ onChange }
105+
/>;
106+
107+
<SelectControl
108+
// @ts-expect-error "string" is not "narrow" or "value"
109+
value="string"
110+
options={ [
111+
{
112+
value: 'narrow',
113+
label: 'Narrow',
114+
},
115+
{
116+
value: 'value',
117+
label: 'Value',
118+
},
119+
] }
120+
// @ts-expect-error "string" is not "narrow" or "value"
121+
onChange={ ( value ) => value === 'string' }
122+
/>;
123+
} );
124+
125+
it( 'should accept an explicit type argument', () => {
126+
<SelectControl< 'narrow' | 'value' >
127+
// @ts-expect-error "string" is not "narrow" or "value"
128+
value="string"
129+
options={ [
130+
{
131+
value: 'narrow',
132+
label: 'Narrow',
133+
},
134+
{
135+
// @ts-expect-error "string" is not "narrow" or "value"
136+
value: 'string',
137+
label: 'String',
138+
},
139+
] }
140+
/>;
141+
} );
142+
} );
143+
144+
describe( 'multiple', () => {
145+
it( 'should infer the value type from available `options`, but not the `value` or `onChange` prop', () => {
146+
const onChange: (
147+
value: ( 'foo' | 'bar' )[]
148+
) => void = () => {};
149+
150+
<SelectControl
151+
multiple
152+
value={ [ 'narrow' ] }
153+
options={ [
154+
{
155+
value: 'narrow',
156+
label: 'Narrow',
157+
},
158+
{
159+
value: 'value',
160+
label: 'Value',
161+
},
162+
] }
163+
// @ts-expect-error onChange type is not compatible with inferred value type
164+
onChange={ onChange }
165+
/>;
166+
167+
<SelectControl
168+
multiple
169+
// @ts-expect-error "string" is not "narrow" or "value"
170+
value={ [ 'string' ] }
171+
options={ [
172+
{
173+
value: 'narrow',
174+
label: 'Narrow',
175+
},
176+
{
177+
value: 'value',
178+
label: 'Value',
179+
},
180+
] }
181+
onChange={ ( value ) =>
182+
// @ts-expect-error "string" is not "narrow" or "value"
183+
value.forEach( ( v ) => v === 'string' )
184+
}
185+
/>;
186+
} );
187+
188+
it( 'should accept an explicit type argument', () => {
189+
<SelectControl< 'narrow' | 'value' >
190+
multiple
191+
// @ts-expect-error "string" is not "narrow" or "value"
192+
value={ [ 'string' ] }
193+
options={ [
194+
{
195+
value: 'narrow',
196+
label: 'Narrow',
197+
},
198+
{
199+
// @ts-expect-error "string" is not "narrow" or "value"
200+
value: 'string',
201+
label: 'String',
202+
},
203+
] }
204+
/>;
205+
} );
206+
} );
207+
} );
208+
/* eslint-enable jest/expect-expect */
84209
} );

0 commit comments

Comments
 (0)