Skip to content

Commit

Permalink
[test] Test types of .spec lab files (#25684)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon authored Apr 9, 2021
1 parent 56030c3 commit e92e65d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
34 changes: 21 additions & 13 deletions packages/material-ui-lab/src/DatePicker/DatePicker.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as React from 'react';
import moment, { Moment } from 'moment';
import DatePicker from '@material-ui/lab/DatePicker';
import AdapterDateFns from '../AdapterDateFns';
import MomentAdapter from '../AdapterMoment';
import { expectType } from '@material-ui/types';

// Allows to set date type right with generic JSX syntax
<DatePicker<Date>
Expand Down Expand Up @@ -41,17 +40,26 @@ const InferTest = () => {
renderInput={() => <input />}
/>;

// Edge case and known issue. When the passed `value` is not a date type
// We cannot infer the type properly without explicit generic type or `dateAdapter` prop
// So in this case it is expected that type will be the type of `value` as for now
<DatePicker
value={null}
onChange={(date) =>
// getDate is any
date?.getDate()
}
renderInput={() => <input />}
/>;
// TypeScript can't know the type of the DateAdapter in the React context.
// So in this case it is expected that type will be the type of `value` as for now.
// Argueable, this usage doesn't make sense since the component would never reflect the user picked value.
{
<DatePicker
value={null}
onChange={(date) => {
expectType<null, typeof date>(date);
}}
renderInput={() => <input />}
/>;
// workaround
<DatePicker<Date>
value={null}
onChange={(date) => {
expectType<Date | null, typeof date>(date);
}}
renderInput={() => <input />}
/>;
}

{
<DatePicker
Expand Down
3 changes: 1 addition & 2 deletions packages/material-ui-lab/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
// @date-io libraries produce duplicate type `DateType`
"skipLibCheck": true
},
"include": ["src/**/*", "test/**/*"],
"exclude": ["./**/*.spec.*"]
"include": ["src/**/*", "test/**/*"]
}

0 comments on commit e92e65d

Please sign in to comment.