-
Notifications
You must be signed in to change notification settings - Fork 44
feat: add toBeVisible matcher #73
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
mdjastrzebski
merged 11 commits into
testing-library:main
from
fluiddot:feat/to-be-visible
Oct 20, 2022
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d81bc01
feat: add toBeVisible matcher
fluiddot e9537e8
Merge branch 'main' into feat/to-be-visible
mdjastrzebski 3a3e4a4
Merge branch 'main' into feat/to-be-visible
fluiddot 2335a11
refactor: migrate `to-be-visible` to TypeScript
fluiddot 861cc72
test: update `to-be-visible` test cases
fluiddot 3348212
feat(toBeVisible): check if visible for accessibility
fluiddot 6c3bc55
Update `toBeVisible` description
fluiddot 80371a4
docs: update `toBeVisible` section in `README`
fluiddot 8bde02c
test: fix typo
fluiddot 4d09779
test(toBeVisible): add throw error test case
fluiddot 7267da4
Merge branch 'feat/to-be-visible' of github.com:fluiddot/jest-native …
fluiddot 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
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,28 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`.toBeVisible throws an error when expectation is not matched 1`] = ` | ||
"expect(element).not.toBeVisible() | ||
|
||
Received element is visible: | ||
Object { | ||
"props": Object { | ||
"children": undefined, | ||
"testID": "test", | ||
}, | ||
}" | ||
`; | ||
|
||
exports[`.toBeVisible throws an error when expectation is not matched 2`] = ` | ||
"expect(element).toBeVisible() | ||
|
||
Received element is not visible: | ||
Object { | ||
"props": Object { | ||
"children": undefined, | ||
"style": Object { | ||
"opacity": 0, | ||
}, | ||
"testID": "test", | ||
}, | ||
}" | ||
`; |
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,135 @@ | ||
import React from 'react'; | ||
import { Modal, View } from 'react-native'; | ||
import { render } from '@testing-library/react-native'; | ||
|
||
describe('.toBeVisible', () => { | ||
test('handles empty view', () => { | ||
const { getByTestId } = render(<View testID="test" />); | ||
expect(getByTestId('test')).toBeVisible(); | ||
}); | ||
|
||
test('handles view with opacity', () => { | ||
const { getByTestId } = render(<View testID="test" style={{ opacity: 0.2 }} />); | ||
expect(getByTestId('test')).toBeVisible(); | ||
}); | ||
|
||
test('handles view with 0 opacity', () => { | ||
const { getByTestId } = render(<View testID="test" style={{ opacity: 0 }} />); | ||
expect(getByTestId('test')).not.toBeVisible(); | ||
}); | ||
|
||
test('handles view with display "none"', () => { | ||
const { getByTestId } = render(<View testID="test" style={{ display: 'none' }} />); | ||
expect(getByTestId('test')).not.toBeVisible(); | ||
}); | ||
|
||
test('handles ancestor view with 0 opacity', () => { | ||
const { getByTestId } = render( | ||
<View style={{ opacity: 0 }}> | ||
<View> | ||
<View testID="test" /> | ||
</View> | ||
</View>, | ||
); | ||
expect(getByTestId('test')).not.toBeVisible(); | ||
}); | ||
|
||
test('handles ancestor view with display "none"', () => { | ||
const { getByTestId } = render( | ||
<View style={{ display: 'none' }}> | ||
<View> | ||
<View testID="test" /> | ||
</View> | ||
</View>, | ||
); | ||
expect(getByTestId('test')).not.toBeVisible(); | ||
}); | ||
|
||
test('handles empty modal', () => { | ||
const { getByTestId } = render(<Modal testID="test" />); | ||
expect(getByTestId('test')).toBeVisible(); | ||
}); | ||
|
||
test('handles view within modal', () => { | ||
const { getByTestId } = render( | ||
<Modal> | ||
<View> | ||
<View testID="view-within-modal" /> | ||
</View> | ||
</Modal>, | ||
); | ||
expect(getByTestId('view-within-modal')).toBeVisible(); | ||
}); | ||
|
||
test('handles view within not visible modal', () => { | ||
const { getByTestId, queryByTestId } = render( | ||
<Modal testID="test" visible={false}> | ||
<View> | ||
<View testID="view-within-modal" /> | ||
</View> | ||
</Modal>, | ||
); | ||
expect(getByTestId('test')).not.toBeVisible(); | ||
// Children elements of not visible modals are not rendered. | ||
expect(() => expect(getByTestId('view-within-modal')).not.toBeVisible()).toThrow(); | ||
expect(queryByTestId('view-within-modal')).toBeNull(); | ||
}); | ||
|
||
test('handles not visible modal', () => { | ||
const { getByTestId } = render(<Modal testID="test" visible={false} />); | ||
expect(getByTestId('test')).not.toBeVisible(); | ||
}); | ||
|
||
test('handles inaccessible view (iOS)', () => { | ||
const { getByTestId, update } = render(<View testID="test" accessibilityElementsHidden />); | ||
expect(getByTestId('test')).not.toBeVisible(); | ||
|
||
update(<View testID="test" accessibilityElementsHidden={false} />); | ||
expect(getByTestId('test')).toBeVisible(); | ||
}); | ||
|
||
test('handles view within inaccessible view (iOS)', () => { | ||
const { getByTestId } = render( | ||
<View accessibilityElementsHidden> | ||
<View> | ||
<View testID="test" /> | ||
</View> | ||
</View>, | ||
); | ||
expect(getByTestId('test')).not.toBeVisible(); | ||
}); | ||
|
||
test('handles inaccessible view (Android)', () => { | ||
const { getByTestId, update } = render( | ||
<View testID="test" importantForAccessibility="no-hide-descendants" />, | ||
); | ||
expect(getByTestId('test')).not.toBeVisible(); | ||
|
||
update(<View testID="test" importantForAccessibility="auto" />); | ||
expect(getByTestId('test')).toBeVisible(); | ||
}); | ||
|
||
test('handles view within inaccessible view (Android)', () => { | ||
const { getByTestId } = render( | ||
<View importantForAccessibility="no-hide-descendants"> | ||
<View> | ||
<View testID="test" /> | ||
</View> | ||
</View>, | ||
); | ||
expect(getByTestId('test')).not.toBeVisible(); | ||
}); | ||
|
||
it('handles non-React elements', () => { | ||
expect(() => expect({ name: 'Non-React element' }).not.toBeVisible()).toThrow(); | ||
expect(() => expect(true).not.toBeVisible()).toThrow(); | ||
}); | ||
|
||
it('throws an error when expectation is not matched', () => { | ||
const { getByTestId, update } = render(<View testID="test" />); | ||
expect(() => expect(getByTestId('test')).not.toBeVisible()).toThrowErrorMatchingSnapshot(); | ||
|
||
update(<View testID="test" style={{ opacity: 0 }} />); | ||
expect(() => expect(getByTestId('test')).toBeVisible()).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); |
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,48 @@ | ||
import { Modal, StyleSheet } from 'react-native'; | ||
import { matcherHint } from 'jest-matcher-utils'; | ||
import type { ReactTestInstance } from 'react-test-renderer'; | ||
|
||
import { checkReactElement, printElement } from './utils'; | ||
|
||
function isStyleVisible(element: ReactTestInstance) { | ||
const style = element.props.style || {}; | ||
const { display, opacity } = StyleSheet.flatten(style); | ||
return display !== 'none' && opacity !== 0; | ||
} | ||
|
||
function isAttributeVisible(element: ReactTestInstance) { | ||
return element.type !== Modal || element.props.visible !== false; | ||
} | ||
|
||
function isVisibleForAccessibility(element: ReactTestInstance) { | ||
const visibleForiOSVoiceOver = !element.props.accessibilityElementsHidden; | ||
const visibleForAndroidTalkBack = | ||
element.props.importantForAccessibility !== 'no-hide-descendants'; | ||
return visibleForiOSVoiceOver && visibleForAndroidTalkBack; | ||
} | ||
|
||
function isElementVisible(element: ReactTestInstance): boolean { | ||
return ( | ||
isStyleVisible(element) && | ||
isAttributeVisible(element) && | ||
isVisibleForAccessibility(element) && | ||
(!element.parent || isElementVisible(element.parent)) | ||
); | ||
} | ||
|
||
export function toBeVisible(this: jest.MatcherContext, element: ReactTestInstance) { | ||
checkReactElement(element, toBeVisible, this); | ||
const isVisible = isElementVisible(element); | ||
return { | ||
pass: isVisible, | ||
message: () => { | ||
const is = isVisible ? 'is' : 'is not'; | ||
return [ | ||
matcherHint(`${this.isNot ? '.not' : ''}.toBeVisible`, 'element', ''), | ||
'', | ||
`Received element ${is} visible:`, | ||
printElement(element), | ||
].join('\n'); | ||
}, | ||
}; | ||
} |
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.