|
| 1 | +// @flow |
| 2 | +import React from 'react'; |
| 3 | +import { StyleSheet, View, Button, Text, TextInput } from 'react-native'; |
| 4 | +import { render } from '..'; |
| 5 | +import '@testing-library/jest-native/extend-expect'; |
| 6 | + |
| 7 | +const style = StyleSheet.create({ |
| 8 | + style1: { |
| 9 | + color: 'red', |
| 10 | + backgroundColor: 'green', |
| 11 | + }, |
| 12 | +}); |
| 13 | + |
| 14 | +test('jest-native matchers work correctly', () => { |
| 15 | + const { getByText, getByA11yHint } = render( |
| 16 | + <View> |
| 17 | + <Button title="Enabled Button" onPress={jest.fn()} /> |
| 18 | + <Button title="Disabled Button" disabled={true} onPress={jest.fn()} /> |
| 19 | + <Text accessibilityHint="Empty Text" style={style.style1} /> |
| 20 | + <Text accessibilityHint="Not Empty Text">Not empty</Text> |
| 21 | + <View accessibilityHint="Empty View" /> |
| 22 | + <View accessibilityHint="Not Empty View"> |
| 23 | + <Text /> |
| 24 | + </View> |
| 25 | + <View accessibilityHint="Container View"> |
| 26 | + <View accessibilityHint="First-Level Child"> |
| 27 | + <Text>Second-Level Child</Text> |
| 28 | + </View> |
| 29 | + </View> |
| 30 | + <TextInput |
| 31 | + accessibilityHint="Text Input" |
| 32 | + allowFontScaling={false} |
| 33 | + secureTextEntry={true} |
| 34 | + defaultValue="111" |
| 35 | + /> |
| 36 | + </View> |
| 37 | + ); |
| 38 | + |
| 39 | + // $FlowFixMe - TODO: fix @testing-library/jest-native flow typings |
| 40 | + expect(getByText('Enabled Button')).toBeEnabled(); |
| 41 | + |
| 42 | + // $FlowFixMe - TODO: fix @testing-library/jest-native flow typings |
| 43 | + expect(getByText('Disabled Button')).not.toBeEnabled(); |
| 44 | + |
| 45 | + expect(getByText('Disabled Button')).toBeDisabled(); |
| 46 | + expect(getByText('Enabled Button')).not.toBeDisabled(); |
| 47 | + |
| 48 | + expect(getByA11yHint('Empty Text')).toBeEmpty(); |
| 49 | + expect(getByA11yHint('Empty View')).toBeEmpty(); |
| 50 | + expect(getByA11yHint('Not Empty Text')).not.toBeEmpty(); |
| 51 | + expect(getByA11yHint('Not Empty View')).not.toBeEmpty(); |
| 52 | + |
| 53 | + // $FlowFixMe - TODO: fix @testing-library/jest-native flow typings |
| 54 | + expect(getByA11yHint('Container View')).toContainElement( |
| 55 | + getByA11yHint('First-Level Child') |
| 56 | + ); |
| 57 | + // $FlowFixMe - TODO: fix @testing-library/jest-native flow typings |
| 58 | + expect(getByA11yHint('Container View')).toContainElement( |
| 59 | + getByText('Second-Level Child') |
| 60 | + ); |
| 61 | + // $FlowFixMe - TODO: fix @testing-library/jest-native flow typings |
| 62 | + expect(getByA11yHint('Container View')).not.toContainElement( |
| 63 | + getByText('Enabled Button') |
| 64 | + ); |
| 65 | + |
| 66 | + expect(getByA11yHint('Not Empty Text')).toHaveTextContent('Not empty'); |
| 67 | + // $FlowFixMe - TODO: fix @testing-library/jest-native flow typings |
| 68 | + expect(getByA11yHint('Not Empty Text')).toHaveTextContent(/Not empty/); |
| 69 | + expect(getByA11yHint('Not Empty Text')).not.toHaveTextContent('Is empty'); |
| 70 | + |
| 71 | + expect(getByA11yHint('Empty Text')).toHaveStyle({ color: 'red' }); |
| 72 | + expect(getByA11yHint('Empty Text')).toHaveStyle({ |
| 73 | + color: 'red', |
| 74 | + backgroundColor: 'green', |
| 75 | + }); |
| 76 | + expect(getByA11yHint('Empty Text')).not.toHaveStyle({ color: 'green' }); |
| 77 | + expect(getByA11yHint('Empty Text')).not.toHaveStyle({ |
| 78 | + color: 'green', |
| 79 | + backgroundColor: 'green', |
| 80 | + }); |
| 81 | + |
| 82 | + const textInput = getByA11yHint('Text Input'); |
| 83 | + expect(textInput).toBeTruthy(); |
| 84 | + expect(textInput).toHaveProp('allowFontScaling'); |
| 85 | + expect(textInput).toHaveProp('allowFontScaling', false); |
| 86 | + expect(textInput).toHaveProp('secureTextEntry'); |
| 87 | + expect(textInput).toHaveProp('secureTextEntry', true); |
| 88 | + expect(textInput).toHaveProp('defaultValue'); |
| 89 | + expect(textInput).toHaveProp('defaultValue', '111'); |
| 90 | +}); |
0 commit comments