Skip to content

Commit

Permalink
Add static type tests
Browse files Browse the repository at this point in the history
Co-authored-by: Mikey Binns <hello@mikeybinns.com>
  • Loading branch information
mirka and mikeybinns committed Aug 1, 2024
1 parent 34ca3a0 commit ad20004
Showing 1 changed file with 125 additions and 0 deletions.
125 changes: 125 additions & 0 deletions packages/components/src/select-control/test/select-control.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,129 @@ describe( 'SelectControl', () => {
expect.anything()
);
} );

/* eslint-disable jest/expect-expect */
describe( 'static typing', () => {
describe( 'single', () => {
it( 'should infer the value type from available `options`, but not the `value` or `onChange` prop', () => {
const onChange: ( value: 'foo' | 'bar' ) => void = () => {};

<SelectControl
value="narrow"
options={ [
{
value: 'narrow',
label: 'Narrow',
},
{
value: 'value',
label: 'Value',
},
] }
// @ts-expect-error onChange type is not compatible with inferred value type
onChange={ onChange }
/>;

<SelectControl
// @ts-expect-error "string" is not "narrow" or "value"
value="string"
options={ [
{
value: 'narrow',
label: 'Narrow',
},
{
value: 'value',
label: 'Value',
},
] }
// @ts-expect-error "string" is not "narrow" or "value"
onChange={ ( value ) => value === 'string' }
/>;
} );

it( 'should accept an explicit type argument', () => {
<SelectControl< 'narrow' | 'value' >
// @ts-expect-error "string" is not "narrow" or "value"
value="string"
options={ [
{
value: 'narrow',
label: 'Narrow',
},
{
// @ts-expect-error "string" is not "narrow" or "value"
value: 'string',
label: 'String',
},
] }
/>;
} );
} );

describe( 'multiple', () => {
it( 'should infer the value type from available `options`, but not the `value` or `onChange` prop', () => {
const onChange: (
value: ( 'foo' | 'bar' )[]
) => void = () => {};

<SelectControl
multiple
value={ [ 'narrow' ] }
options={ [
{
value: 'narrow',
label: 'Narrow',
},
{
value: 'value',
label: 'Value',
},
] }
// @ts-expect-error onChange type is not compatible with inferred value type
onChange={ onChange }
/>;

<SelectControl
multiple
// @ts-expect-error "string" is not "narrow" or "value"
value={ [ 'string' ] }
options={ [
{
value: 'narrow',
label: 'Narrow',
},
{
value: 'value',
label: 'Value',
},
] }
onChange={ ( value ) =>
// @ts-expect-error "string" is not "narrow" or "value"
value.forEach( ( v ) => v === 'string' )
}
/>;
} );

it( 'should accept an explicit type argument', () => {
<SelectControl< 'narrow' | 'value' >
multiple
// @ts-expect-error "string" is not "narrow" or "value"
value={ [ 'string' ] }
options={ [
{
value: 'narrow',
label: 'Narrow',
},
{
// @ts-expect-error "string" is not "narrow" or "value"
value: 'string',
label: 'String',
},
] }
/>;
} );
} );
} );
/* eslint-enable jest/expect-expect */
} );

0 comments on commit ad20004

Please sign in to comment.