This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 222
Add generics to findWhere and findWhereAll in react-testing #1999
Merged
+59
−13
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -468,11 +468,42 @@ expect(wrapper.find(MyComponent, {name: 'Gord'})!.props).toMatchObject({ | |
|
||
Like `find()`, but returns all matches as an array. | ||
|
||
##### <a name="findWhere"></a> `findWhere(predicate: (element: Element<unknown>) => boolean): Element<unknown> | null` | ||
##### <a name="findWhere"></a> `findWhere<Type = unknown>(predicate: (element: Element<unknown>) => boolean): Element<PropsForComponent<Type>> | null` | ||
|
||
Finds the first descendant component matching the passed function. The function is called with each `Element` from [`descendants`](#descendants) until a match is found. If no match is found, `null` is returned. | ||
|
||
##### <a name="findAllWhere"></a> `findAllWhere(predicate: (element: Element<unknown>) => boolean): Element<unknown>[]` | ||
`findWhere` accepts an optional generic argument that can be used to specify the type of the returned element. This argument is either a string or a React component, the same as the first argument on `.find`. If the generic argument is omited then the returned element will have unknown props and thus calling `.props` and `.trigger` on it will cause type errors as those functions won't know what props are valid on your element: | ||
|
||
```tsx | ||
function MyComponent({name}: {name: string}) { | ||
return <div>Hello, {name}!</div>; | ||
} | ||
|
||
function Wrapper() { | ||
return ( | ||
<> | ||
<MyComponent name="Michelle" /> | ||
<MyComponent name="Gord" /> | ||
</> | ||
); | ||
} | ||
|
||
const wrapper = mount(<Wrapper />); | ||
const startsWithM = wrapper.findWhere<MyComponent>( | ||
(node) => node.is(MyComponent) && node.prop('name').startsWith('M'), | ||
); | ||
|
||
const startsWithG = wrapper.findWhere<MyComponent>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am actually a little surprise its There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Excellent spot, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lol it would be so nice if typescript is smart enough in the future to know it should use the |
||
(node) => node.is(MyComponent) && node.prop('name').startsWith('G'), | ||
); | ||
|
||
expect(startsWithM.prop('name')).toBe('Michelle'); | ||
expect(startsWithG.prop('name')).toBe('Gord'); | ||
``` | ||
|
||
```` | ||
|
||
##### <a name="findAllWhere"></a> `findAllWhere<Type = unknown>(predicate: (element: Element<unknown>) => boolean): Element<PropsForComponent<Type>>[]` | ||
|
||
Like `findWhere`, but returns all matches as an array. | ||
|
||
|
@@ -505,7 +536,7 @@ function Wrapper() { | |
const wrapper = mount(<Wrapper />); | ||
wrapper.find(MyComponent)!.trigger('onClick', 'some-id'); | ||
expect(wrapper.find('div')!.text()).toContain('some-id'); | ||
``` | ||
```` | ||
|
||
##### <a name="triggerKeypath"></a> `triggerKeypath<T>(keypath: string, ...args: any[]): T` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The find docs reference you and Gord, I'm just being consistent :p