-
Notifications
You must be signed in to change notification settings - Fork 276
feat: add a11y queries #178
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
551517d
First implementation of matcher generator
Esemesek d7c79a0
A11y queries added
Esemesek 4038f6a
extract prepareErrorMessage
thymikee 73141fa
Add tests
Esemesek 1d73c79
Make aliases
Esemesek 634bb7d
Move create queryByError
Esemesek cd87805
Add missing aliases
Esemesek 589ebb4
simplify valid node check
thymikee e6fb1d8
add docs
thymikee 8147322
fix types
thymikee 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,41 @@ | ||
// @flow | ||
import React from 'react'; | ||
import { TouchableOpacity, Text } from 'react-native'; | ||
import { render } from '..'; | ||
|
||
const BUTTON_LABEL = 'button-label'; | ||
const TEXT_LABEL = 'text-label'; | ||
|
||
class Button extends React.Component<*> { | ||
render() { | ||
return ( | ||
<TouchableOpacity | ||
accessibilityLabel={BUTTON_LABEL} | ||
accessibilityStates={['disabled', 'selected']} | ||
> | ||
<Text accessibilityLabel={TEXT_LABEL}>Text 1</Text> | ||
<Text accessibilityLabel={TEXT_LABEL}>Text 2</Text> | ||
</TouchableOpacity> | ||
); | ||
} | ||
} | ||
|
||
test('getByA11yLabel, queryByA11yLabel', () => { | ||
const { getByA11yStates } = render(<Button />); | ||
|
||
console.log(getByA11yStates(['disabled', 'selected'])); | ||
}); | ||
|
||
test('getAllByA11yLabel, queryAllByA11yLabel', () => {}); | ||
|
||
test('getByA11yHint, queryByA11yHint', () => {}); | ||
|
||
test('getAllByA11yHint, queryAllByA11yHint', () => {}); | ||
|
||
test('getByA11yRole, queryByA11yRole', () => {}); | ||
|
||
test('getAllByA11yRole, queryAllByA11yRole', () => {}); | ||
|
||
test('getByA11yStates, queryByA11yStates', () => {}); | ||
|
||
test('getAllByA11yStates, queryAllByA11yStates', () => {}); |
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,98 @@ | ||
// @flow | ||
import makeQuery from './makeQuery'; | ||
|
||
type TextQueryFn = (string | RegExp) => ReactTestInstance; | ||
type ArrayQueryFn = (string | Array<string>) => ReactTestInstance; | ||
|
||
type A11yAPI = { | ||
getByA11yLabel: TextQueryFn, | ||
getAllByA11yLabel: TextQueryFn, | ||
queryByA11yLabel: TextQueryFn, | ||
queryAllByA11yLabel: TextQueryFn, | ||
getByA11yHint: TextQueryFn, | ||
getAllByA11yHint: TextQueryFn, | ||
queryByA11yHint: TextQueryFn, | ||
queryAllByA11yHint: TextQueryFn, | ||
getByA11yRole: TextQueryFn, | ||
getAllByA11yRole: TextQueryFn, | ||
queryByA11yRole: TextQueryFn, | ||
queryAllByA11yRole: TextQueryFn, | ||
getByA11yStates: ArrayQueryFn, | ||
getAllByA11yStates: ArrayQueryFn, | ||
queryByA11yStates: ArrayQueryFn, | ||
queryAllByA11yStates: ArrayQueryFn, | ||
}; | ||
|
||
export function matchStringValue(prop?: string, matcher: string | RegExp) { | ||
if (!prop) { | ||
return false; | ||
} | ||
|
||
if (typeof matcher === 'string') { | ||
return prop === matcher; | ||
} | ||
|
||
return Boolean(prop.match(matcher)); | ||
} | ||
|
||
export function matchArrayValue( | ||
prop?: Array<string>, | ||
matcher: string | Array<string> | ||
) { | ||
if (!prop) { | ||
return false; | ||
} | ||
|
||
if (typeof matcher === 'string') { | ||
return prop.includes(matcher); | ||
} | ||
|
||
// $FlowFixMe | ||
return !matcher.some(e => !prop.includes(e)); | ||
} | ||
|
||
const a11yAPI = (instance: ReactTestInstance): A11yAPI => | ||
({ | ||
...makeQuery( | ||
'accessibilityLabel', | ||
{ | ||
getBy: 'getByA11yLabel', | ||
getAllBy: 'getAllByA11yLabel', | ||
queryBy: 'queryByA11yLabel', | ||
queryAllBy: 'queryAllByA11yLabel', | ||
}, | ||
matchStringValue | ||
)(instance), | ||
...makeQuery( | ||
'accessibilityHint', | ||
{ | ||
getBy: 'getByA11yHint', | ||
getAllBy: 'getAllByA11yHint', | ||
queryBy: 'queryByA11yHint', | ||
queryAllBy: 'queryAllByA11yHint', | ||
}, | ||
matchStringValue | ||
)(instance), | ||
...makeQuery( | ||
'accessibilityRole', | ||
{ | ||
getBy: 'getByA11yRole', | ||
getAllBy: 'getAllByA11yRole', | ||
queryBy: 'queryByA11yRole', | ||
queryAllBy: 'queryAllByA11yRole', | ||
}, | ||
matchStringValue | ||
)(instance), | ||
...makeQuery( | ||
'accessibilityStates', | ||
{ | ||
getBy: 'getByA11yStates', | ||
getAllBy: 'getAllByA11yStates', | ||
queryBy: 'queryByA11yStates', | ||
queryAllBy: 'queryAllByA11yStates', | ||
}, | ||
matchArrayValue | ||
)(instance), | ||
}: any); | ||
|
||
export default a11yAPI; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// @flow | ||
import { ErrorWithStack, prepareErrorMessage } from './errors'; | ||
|
||
function getNodeName(node: ReactTestInstance) { | ||
return typeof node.type !== 'string' | ||
? node.type.displayName || node.type.name | ||
: ''; | ||
} | ||
|
||
function isNodeValid(node: ReactTestInstance) { | ||
return ['View', 'Text', 'TextInput', 'Image', 'ScrollViewMock'].includes( | ||
getNodeName(node) | ||
); | ||
} | ||
|
||
const createQueryByError = (error: Error, callsite: Function) => { | ||
if (error.message.includes('No instances found')) { | ||
return null; | ||
} | ||
throw new ErrorWithStack(error.message, callsite); | ||
}; | ||
|
||
type QueryNames = { | ||
getBy: string, | ||
getAllBy: string, | ||
queryBy: string, | ||
queryAllBy: string, | ||
}; | ||
|
||
const makeQuery = <P: mixed, M: mixed>( | ||
name: string, | ||
queryNames: QueryNames, | ||
matcherFn: (prop: P, value: M) => boolean | ||
) => (instance: ReactTestInstance) => { | ||
const getBy = (matcher: M) => { | ||
try { | ||
return instance.find(node => | ||
Boolean(isNodeValid(node) && matcherFn(node.props[name], matcher)) | ||
); | ||
} catch (error) { | ||
throw new ErrorWithStack(prepareErrorMessage(error), getBy); | ||
} | ||
}; | ||
|
||
const getAllBy = (matcher: M) => { | ||
const results = instance.findAll(node => | ||
Boolean(isNodeValid(node) && matcherFn(node.props[name], matcher)) | ||
); | ||
|
||
if (results.length === 0) { | ||
throw new ErrorWithStack('No instances found', getAllBy); | ||
} | ||
|
||
return results; | ||
}; | ||
|
||
const queryBy = (matcher: M) => { | ||
try { | ||
return getBy(matcher); | ||
} catch (error) { | ||
return createQueryByError(error, queryBy); | ||
} | ||
}; | ||
|
||
const queryAllBy = (matcher: M) => { | ||
try { | ||
return getAllBy(matcher); | ||
} catch (error) { | ||
return []; | ||
} | ||
}; | ||
|
||
return { | ||
[queryNames.getBy]: getBy, | ||
[queryNames.getAllBy]: getAllBy, | ||
[queryNames.queryBy]: queryBy, | ||
[queryNames.queryAllBy]: queryAllBy, | ||
}; | ||
}; | ||
|
||
export default makeQuery; |
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.
Uh oh!
There was an error while loading. Please reload this page.