-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
332 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
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,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 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 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,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'); | ||
}, | ||
}; | ||
} |