This repository has been archived by the owner on Jul 30, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(getBy*): throw an error if more than one element is found
BREAKING CHANGE: `queryBy` and `getBy` now throw when multiple elements are returned and prompt users to use other queries if multiple results were intentional. This was done to remain in feature parity with `dom-testing-library`
- Loading branch information
1 parent
64d8a20
commit 49f2b0d
Showing
23 changed files
with
629 additions
and
320 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,187 @@ | ||
import React from 'react'; | ||
import { Text, TextInput, View } from 'react-native'; | ||
import cases from 'jest-in-case'; | ||
|
||
import { render } from '../'; | ||
|
||
cases( | ||
'getBy* queries throw an error when there are multiple elements returned', | ||
({ name, query, tree }) => { | ||
const utils = render(tree); | ||
expect(() => utils[name](query)).toThrow(/multiple elements/i); | ||
}, | ||
{ | ||
getByA11yHint: { | ||
query: /his/, | ||
tree: ( | ||
<View> | ||
<View accessibilityHint="his" /> | ||
<View accessibilityHint="history" /> | ||
</View> | ||
), | ||
}, | ||
getByA11yLabel: { | ||
query: /his/, | ||
tree: ( | ||
<View> | ||
<View accessibilityLabel="his" /> | ||
<View accessibilityLabel="history" /> | ||
</View> | ||
), | ||
}, | ||
getByA11yRole: { | ||
query: 'button', | ||
tree: ( | ||
<View> | ||
<View accessibilityRole="button" /> | ||
<View accessibilityRole="button" /> | ||
</View> | ||
), | ||
}, | ||
getByA11yStates: { | ||
query: ['selected'], | ||
tree: ( | ||
<View> | ||
<View accessibilityStates={['selected']} /> | ||
<View accessibilityStates={['selected']} /> | ||
</View> | ||
), | ||
}, | ||
getByA11yTraits: { | ||
query: ['button'], | ||
tree: ( | ||
<View> | ||
<View accessibilityTraits={['button']} /> | ||
<View accessibilityTraits={['button']} /> | ||
</View> | ||
), | ||
}, | ||
getByPlaceholder: { | ||
query: /his/, | ||
tree: ( | ||
<View> | ||
<TextInput placeholder="his" /> | ||
<TextInput placeholder="history" /> | ||
</View> | ||
), | ||
}, | ||
getByTestId: { | ||
query: /his/, | ||
tree: ( | ||
<View> | ||
<Text testID="his">text</Text> | ||
<Text testID="history">other</Text> | ||
</View> | ||
), | ||
}, | ||
getByText: { | ||
query: /his/, | ||
tree: ( | ||
<View> | ||
<Text>his</Text> | ||
<Text>history</Text> | ||
</View> | ||
), | ||
}, | ||
getByValue: { | ||
query: /his/, | ||
tree: ( | ||
<View> | ||
<TextInput value="his" /> | ||
<TextInput value="history" /> | ||
</View> | ||
), | ||
}, | ||
}, | ||
); | ||
|
||
cases( | ||
'queryBy* queries throw an error when there are multiple elements returned', | ||
({ name, query, tree }) => { | ||
const utils = render(tree); | ||
expect(() => utils[name](query)).toThrow(/multiple elements/i); | ||
}, | ||
{ | ||
queryByA11yHint: { | ||
query: /his/, | ||
tree: ( | ||
<View> | ||
<View accessibilityHint="his" /> | ||
<View accessibilityHint="history" /> | ||
</View> | ||
), | ||
}, | ||
queryByA11yLabel: { | ||
query: /his/, | ||
tree: ( | ||
<View> | ||
<View accessibilityLabel="his" /> | ||
<View accessibilityLabel="history" /> | ||
</View> | ||
), | ||
}, | ||
queryByA11yRole: { | ||
query: 'button', | ||
tree: ( | ||
<View> | ||
<View accessibilityRole="button" /> | ||
<View accessibilityRole="button" /> | ||
</View> | ||
), | ||
}, | ||
queryByA11yStates: { | ||
query: ['selected'], | ||
tree: ( | ||
<View> | ||
<View accessibilityStates={['selected']} /> | ||
<View accessibilityStates={['selected']} /> | ||
</View> | ||
), | ||
}, | ||
queryByA11yTraits: { | ||
query: ['button'], | ||
tree: ( | ||
<View> | ||
<View accessibilityTraits={['button']} /> | ||
<View accessibilityTraits={['button']} /> | ||
</View> | ||
), | ||
}, | ||
queryByPlaceholder: { | ||
query: /his/, | ||
tree: ( | ||
<View> | ||
<TextInput placeholder="his" /> | ||
<TextInput placeholder="history" /> | ||
</View> | ||
), | ||
}, | ||
queryByTestId: { | ||
query: /his/, | ||
tree: ( | ||
<View> | ||
<Text testID="his">text</Text> | ||
<Text testID="history">other</Text> | ||
</View> | ||
), | ||
}, | ||
queryByText: { | ||
query: /his/, | ||
tree: ( | ||
<View> | ||
<Text>his</Text> | ||
<Text>history</Text> | ||
</View> | ||
), | ||
}, | ||
queryByValue: { | ||
query: /his/, | ||
tree: ( | ||
<View> | ||
<TextInput value="his" /> | ||
<TextInput value="history" /> | ||
</View> | ||
), | ||
}, | ||
}, | ||
); |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React from 'react'; | ||
import { View } from 'react-native'; | ||
|
||
import { render } from '../'; | ||
import { queryByProp, queryByTestId } from '../'; | ||
|
||
// we used to use queryByProp internally, but we don't anymore. Some people | ||
// use it as an undocumented part of the API, so we'll keep it around. | ||
test('queryByProp', () => { | ||
const { container } = render( | ||
<View> | ||
<View testID="foo" importantForAccessibility="no" /> | ||
<View importantForAccessibility="no" /> | ||
<View importantForAccessibility="no-hide-descendants" /> | ||
</View>, | ||
); | ||
|
||
expect(queryByTestId(container, 'foo')).not.toBeNull(); | ||
expect(queryByProp('importantForAccessibility', container, 'auto')).toBeNull(); | ||
expect(() => queryByProp('importantForAccessibility', container, /no/)).toThrow( | ||
/multiple elements/, | ||
); | ||
}); |
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 |
---|---|---|
@@ -1,19 +1,57 @@ | ||
import React from 'react'; | ||
import { Text } from 'react-native'; | ||
import { Text, View } from 'react-native'; | ||
|
||
import { render } from '../'; | ||
import { prettyPrint } from '../pretty-print'; | ||
|
||
test('it prints out the given element tree', () => { | ||
const { container } = render(<Text>Hello World!</Text>); | ||
expect(prettyPrint(container)).toMatchInlineSnapshot(` | ||
"[36m<Text>[39m | ||
[0mHello World![0m | ||
[36m</Text>[39m" | ||
test('it prints correctly with no children', () => { | ||
const { baseElement } = render(<View />); | ||
|
||
expect(prettyPrint(baseElement)).toMatchInlineSnapshot(`"[36m<View />[39m"`); | ||
}); | ||
|
||
test('it prints correctly with one child', () => { | ||
const { baseElement } = render( | ||
<View> | ||
<Text>Hello World!</Text> | ||
</View>, | ||
); | ||
|
||
expect(prettyPrint(baseElement)).toMatchInlineSnapshot(` | ||
"[36m<View>[39m | ||
[36m<Text>[39m | ||
[0mHello World![0m | ||
[36m</Text>[39m | ||
[36m</View>[39m" | ||
`); | ||
}); | ||
|
||
test('it prints correctly with multiple children', () => { | ||
const { baseElement } = render( | ||
<View> | ||
<Text>Hello</Text> | ||
<Text>World!</Text> | ||
</View>, | ||
); | ||
|
||
expect(prettyPrint(baseElement)).toMatchInlineSnapshot(` | ||
"[36m<View>[39m | ||
[36m<Text>[39m | ||
[0mHello[0m | ||
[36m</Text>[39m | ||
[36m<Text>[39m | ||
[0mWorld![0m | ||
[36m</Text>[39m | ||
[36m</View>[39m" | ||
`); | ||
}); | ||
|
||
test('it supports truncating the output length', () => { | ||
const { container } = render(<Text>Hello World!</Text>); | ||
expect(prettyPrint(container, 5)).toMatch(/\.\.\./); | ||
const { baseElement } = render( | ||
<View> | ||
<Text>Hello World!</Text> | ||
</View>, | ||
); | ||
|
||
expect(prettyPrint(baseElement, 5)).toMatch(/\.\.\./); | ||
}); |
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
Oops, something went wrong.