-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement
toBeSelected
matcher (#1488)
* feat: implement toBeSelected * refactor: tweaks * refactor: tweak tests * refactor: finishing touches --------- Co-authored-by: Maciej Jastrze台bski <mdjastrzebski@gmail.com>
- Loading branch information
1 parent
8373d6a
commit 1592d35
Showing
6 changed files
with
131 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import * as React from 'react'; | ||
import { View } from 'react-native'; | ||
import { render, screen } from '../..'; | ||
import '../extend-expect'; | ||
|
||
test('.toBeSelected() basic case', () => { | ||
render( | ||
<> | ||
<View testID="selected" accessibilityState={{ selected: true }} /> | ||
<View testID="selected-aria" aria-selected /> | ||
<View testID="not-selected" accessibilityState={{ selected: false }} /> | ||
<View testID="not-selected-aria" aria-selected={false} /> | ||
<View testID="default" /> | ||
</> | ||
); | ||
|
||
expect(screen.getByTestId('selected')).toBeSelected(); | ||
expect(screen.getByTestId('selected-aria')).toBeSelected(); | ||
expect(screen.getByTestId('not-selected')).not.toBeSelected(); | ||
expect(screen.getByTestId('not-selected-aria')).not.toBeSelected(); | ||
expect(screen.getByTestId('default')).not.toBeSelected(); | ||
}); | ||
|
||
test('.toBeSelected() error messages', () => { | ||
render( | ||
<> | ||
<View testID="selected" accessibilityState={{ selected: true }} /> | ||
<View testID="selected-aria" aria-selected /> | ||
<View testID="not-selected" accessibilityState={{ selected: false }} /> | ||
<View testID="not-selected-aria" aria-selected={false} /> | ||
<View testID="default" /> | ||
</> | ||
); | ||
|
||
expect(() => expect(screen.getByTestId('selected')).not.toBeSelected()) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).not.toBeSelected() | ||
Received element is selected | ||
<View | ||
accessibilityState={ | ||
{ | ||
"selected": true, | ||
} | ||
} | ||
testID="selected" | ||
/>" | ||
`); | ||
|
||
expect(() => expect(screen.getByTestId('selected-aria')).not.toBeSelected()) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).not.toBeSelected() | ||
Received element is selected | ||
<View | ||
aria-selected={true} | ||
testID="selected-aria" | ||
/>" | ||
`); | ||
|
||
expect(() => expect(screen.getByTestId('not-selected')).toBeSelected()) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).toBeSelected() | ||
Received element is not selected | ||
<View | ||
accessibilityState={ | ||
{ | ||
"selected": false, | ||
} | ||
} | ||
testID="not-selected" | ||
/>" | ||
`); | ||
|
||
expect(() => expect(screen.getByTestId('not-selected-aria')).toBeSelected()) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).toBeSelected() | ||
Received element is not selected | ||
<View | ||
aria-selected={false} | ||
testID="not-selected-aria" | ||
/>" | ||
`); | ||
|
||
expect(() => expect(screen.getByTestId('default')).toBeSelected()) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).toBeSelected() | ||
Received element is not selected | ||
<View | ||
testID="default" | ||
/>" | ||
`); | ||
}); |
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,24 @@ | ||
import { ReactTestInstance } from 'react-test-renderer'; | ||
import { matcherHint } from 'jest-matcher-utils'; | ||
import { getAccessibilitySelectedState } from '../helpers/accessiblity'; | ||
import { checkHostElement, formatElement } from './utils'; | ||
|
||
export function toBeSelected( | ||
this: jest.MatcherContext, | ||
element: ReactTestInstance | ||
) { | ||
checkHostElement(element, toBeSelected, this); | ||
|
||
return { | ||
pass: getAccessibilitySelectedState(element), | ||
message: () => { | ||
const is = this.isNot ? 'is' : 'is not'; | ||
return [ | ||
matcherHint(`${this.isNot ? '.not' : ''}.toBeSelected`, 'element', ''), | ||
'', | ||
`Received element ${is} selected`, | ||
formatElement(element), | ||
].join('\n'); | ||
}, | ||
}; | ||
} |