Skip to content

Commit

Permalink
Expand findTestSubject utility type (#3763)
Browse files Browse the repository at this point in the history
* make findTestSubject generic

* make takeMountedSnapshot generic

* use any instead of generic

* CL
  • Loading branch information
thompsongl authored Jul 21, 2020
1 parent af8575d commit bb7b725
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Fixed `EuiComboBox` always showing a scrollbar ([#3744](https://github.com/elastic/eui/pull/3744))
- Replaced `react-focus-lock` with `react-focus-on` ([#3631](https://github.com/elastic/eui/pull/3631))
- Fixed errors in `EuiSuperDatePicker` related to invalid and `null` date formatting ([#3750](https://github.com/elastic/eui/pull/3750))
- Fixed type definitions for `findTestSubject` and `takeMountedSnapshot` ([#3763](https://github.com/elastic/eui/pull/3763))

## [`27.1.0`](https://github.com/elastic/eui/tree/v27.1.0)

Expand Down
22 changes: 10 additions & 12 deletions src/test/find_test_subject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@

import { ReactWrapper, ShallowWrapper } from 'enzyme';

type FindTestSubject<T extends ShallowWrapper | ReactWrapper> = (
mountedComponent: T,
testSubjectSelector: string,
matcher?: '=' | '~=' | '|=' | '^=' | '$=' | '*='
) => ReturnType<T['find']>;

/**
* Find node which matches a specific test subject selector. Returns ReactWrappers around DOM element,
* https://github.com/airbnb/enzyme/tree/master/docs/api/ReactWrapper.
Expand All @@ -42,13 +36,17 @@ const MATCHERS = [
'^=', // Begins with substring
'$=', // Ends with substring
'*=', // Contains substring
];
] as const;

type FindTestSubject<T extends ShallowWrapper | ReactWrapper> = (
mountedComponent: T,
testSubjectSelector: string,
matcher?: typeof MATCHERS[number]
) => ReturnType<T['find']>;

export const findTestSubject: FindTestSubject<ShallowWrapper | ReactWrapper> = (
mountedComponent,
testSubjectSelector,
matcher = '~='
) => {
export const findTestSubject: FindTestSubject<
ShallowWrapper<any> | ReactWrapper<any>
> = (mountedComponent, testSubjectSelector, matcher = '~=') => {
if (!MATCHERS.includes(matcher)) {
throw new Error(
`Matcher ${matcher} not found in list of allowed matchers: ${MATCHERS.join(
Expand Down
2 changes: 1 addition & 1 deletion src/test/take_mounted_snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ interface TakeMountedSnapshotOptions {
* leaving only HTML elements in the snapshot.
*/
export const takeMountedSnapshot = (
mountedComponent: ReactWrapper<{}, {}, Component>,
mountedComponent: ReactWrapper<any, {}, Component>,
options: TakeMountedSnapshotOptions = {}
) => {
const opts: TakeMountedSnapshotOptions = {
Expand Down

0 comments on commit bb7b725

Please sign in to comment.