-
Notifications
You must be signed in to change notification settings - Fork 274
feat: predicate query #973
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8473762
feat: predicate query POC implementation
mdjastrzebski 263dc1a
chore: improve tests
mdjastrzebski 6779e66
chore: docs
mdjastrzebski 3da4c4a
chore: flow typing
mdjastrzebski 3742694
fix: missing screen guard methods
mdjastrzebski a9cb1c6
fix: flow types
mdjastrzebski 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import React from 'react'; | ||
import { View, Text, TextInput, Button } from 'react-native'; | ||
import { ReactTestInstance } from 'react-test-renderer'; | ||
import { render } from '../..'; | ||
|
||
test('getByPredicate returns only native elements', () => { | ||
const testIdPredicate = (testID: string) => (element: ReactTestInstance) => { | ||
return element.props.testID === testID; | ||
}; | ||
|
||
const textInputPredicate = function matchTextInput( | ||
element: ReactTestInstance | ||
) { | ||
// @ts-expect-error - ReatTestInstance type is missing host element typing | ||
return element.type === 'TextInput'; | ||
}; | ||
|
||
const { getByPredicate, getAllByPredicate } = render( | ||
<View> | ||
<Text testID="text">Text</Text> | ||
<TextInput testID="textInput" /> | ||
<View testID="view" /> | ||
<Button testID="button" title="Button" onPress={jest.fn()} /> | ||
</View> | ||
); | ||
|
||
expect(getByPredicate(testIdPredicate('text'))).toBeTruthy(); | ||
expect(getByPredicate(testIdPredicate('textInput'))).toBeTruthy(); | ||
expect(getByPredicate(testIdPredicate('view'))).toBeTruthy(); | ||
expect(getByPredicate(testIdPredicate('button'))).toBeTruthy(); | ||
|
||
expect(getAllByPredicate(testIdPredicate('text'))).toHaveLength(1); | ||
expect(getAllByPredicate(testIdPredicate('textInput'))).toHaveLength(1); | ||
expect(getAllByPredicate(testIdPredicate('view'))).toHaveLength(1); | ||
expect(getAllByPredicate(testIdPredicate('button'))).toHaveLength(1); | ||
|
||
expect(getByPredicate(textInputPredicate)).toBeTruthy(); | ||
}); | ||
|
||
test('getByPredicate error messages', () => { | ||
function hasStylePredicate(element: ReactTestInstance) { | ||
return element.props.style !== undefined; | ||
} | ||
|
||
const textInputPredicate = function textInputPredicate( | ||
element: ReactTestInstance | ||
) { | ||
// @ts-expect-error - ReatTestInstance type is missing host element typing | ||
return element.type === 'TextInput'; | ||
}; | ||
|
||
const testIdPredicate = (testID: string) => (element: ReactTestInstance) => { | ||
return element.props.testID === testID; | ||
}; | ||
|
||
const { getByPredicate, getAllByPredicate } = render( | ||
<View> | ||
<Text testID="text">Text</Text> | ||
</View> | ||
); | ||
|
||
expect(() => getByPredicate(hasStylePredicate)) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"Unable to find an element matching predicate: function hasStylePredicate(element) { | ||
return element.props.style !== undefined; | ||
}" | ||
`); | ||
|
||
expect(() => getByPredicate(textInputPredicate)) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"Unable to find an element matching predicate: function textInputPredicate(element) { | ||
// @ts-expect-error - ReatTestInstance type is missing host element typing | ||
return element.type === 'TextInput'; | ||
}" | ||
`); | ||
|
||
expect(() => getByPredicate(testIdPredicate('myComponent'))) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"Unable to find an element matching predicate: element => { | ||
return element.props.testID === testID; | ||
}" | ||
`); | ||
expect(() => getAllByPredicate(testIdPredicate('myComponent'))) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"Unable to find an element matching predicate: element => { | ||
return element.props.testID === testID; | ||
}" | ||
`); | ||
}); |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import type { ReactTestInstance } from 'react-test-renderer'; | ||
import { isHostElement } from '../helpers/component-tree'; | ||
import { findAll } from '../helpers/findAll'; | ||
import { makeQueries } from './makeQueries'; | ||
import type { | ||
FindAllByQuery, | ||
FindByQuery, | ||
GetAllByQuery, | ||
GetByQuery, | ||
QueryAllByQuery, | ||
QueryByQuery, | ||
} from './makeQueries'; | ||
import { CommonQueryOptions } from './options'; | ||
|
||
type PredicateFn = (instance: ReactTestInstance) => boolean; | ||
type ByPredicateQueryOptions = CommonQueryOptions; | ||
|
||
function queryAllByPredicate(instance: ReactTestInstance) { | ||
return function queryAllByPredicateFn( | ||
predicate: PredicateFn, | ||
options?: ByPredicateQueryOptions | ||
): Array<ReactTestInstance> { | ||
const results = findAll( | ||
instance, | ||
(node) => isHostElement(node) && predicate(node), | ||
options | ||
); | ||
|
||
return results; | ||
}; | ||
} | ||
|
||
const getMultipleError = (predicate: PredicateFn) => | ||
`Found multiple elements matching predicate: ${predicate}`; | ||
|
||
const getMissingError = (predicate: PredicateFn) => | ||
`Unable to find an element matching predicate: ${predicate}`; | ||
|
||
const { getBy, getAllBy, queryBy, queryAllBy, findBy, findAllBy } = makeQueries( | ||
queryAllByPredicate, | ||
getMissingError, | ||
getMultipleError | ||
); | ||
|
||
export type ByTestIdQueries = { | ||
getByPredicate: GetByQuery<PredicateFn, ByPredicateQueryOptions>; | ||
getAllByPredicate: GetAllByQuery<PredicateFn, ByPredicateQueryOptions>; | ||
queryByPredicate: QueryByQuery<PredicateFn, ByPredicateQueryOptions>; | ||
queryAllByPredicate: QueryAllByQuery<PredicateFn, ByPredicateQueryOptions>; | ||
findByPredicate: FindByQuery<PredicateFn, ByPredicateQueryOptions>; | ||
findAllByPredicate: FindAllByQuery<PredicateFn, ByPredicateQueryOptions>; | ||
}; | ||
|
||
export const bindByPredicateQueries = ( | ||
instance: ReactTestInstance | ||
): ByTestIdQueries => ({ | ||
getByPredicate: getBy(instance), | ||
getAllByPredicate: getAllBy(instance), | ||
queryByPredicate: queryBy(instance), | ||
queryAllByPredicate: queryAllBy(instance), | ||
findByPredicate: findBy(instance), | ||
findAllByPredicate: findAllBy(instance), | ||
}); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
could we provide an example of a real scenario where we might need it and how to use it in that case? (like it's done for ByAccessibilityValue above)
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.
Good idea to add some examples there. One thought that I have atm is that these examples will be somewhat hacky, as if they were really good ideas we would make regular queries from them ;-)
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.
i see what you mean, but then RNTL only provides queries for common case scenarios. We could find a legit scenario that's very uncommon maybe? Like find all underline text? (so hard to find a rare but legit scenario haha ^^')
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.
Yeah, we can build examples to showcase how you can recreate some existing queries like *ByTestId.
I went back to the source, DTL docs, to check what examples they give, but they are basically implementing
*ByTestId
queries using somequeryByAttribute
function. All that seems like legacy stuff that has no good examples.