|
| 1 | +// @flow |
| 2 | +import React from 'react'; |
| 3 | +import { View, Text, TextInput, TouchableOpacity, Button } from 'react-native'; |
| 4 | +import { render } from '..'; |
| 5 | + |
| 6 | +const PLACEHOLDER_FRESHNESS = 'Add custom freshness'; |
| 7 | +const PLACEHOLDER_CHEF = 'Who inspected freshness?'; |
| 8 | +const INPUT_FRESHNESS = 'Custom Freshie'; |
| 9 | +const INPUT_CHEF = 'I inspected freshie'; |
| 10 | + |
| 11 | +class MyButton extends React.Component<any> { |
| 12 | + render() { |
| 13 | + return ( |
| 14 | + <TouchableOpacity onPress={this.props.onPress}> |
| 15 | + <Text>{this.props.children}</Text> |
| 16 | + </TouchableOpacity> |
| 17 | + ); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +class Banana extends React.Component<any, any> { |
| 22 | + state = { |
| 23 | + fresh: false, |
| 24 | + }; |
| 25 | + |
| 26 | + componentDidUpdate() { |
| 27 | + if (this.props.onUpdate) { |
| 28 | + this.props.onUpdate(); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + componentWillUnmount() { |
| 33 | + if (this.props.onUnmount) { |
| 34 | + this.props.onUnmount(); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + changeFresh = () => { |
| 39 | + this.setState((state) => ({ |
| 40 | + fresh: !state.fresh, |
| 41 | + })); |
| 42 | + }; |
| 43 | + |
| 44 | + render() { |
| 45 | + const test = 0; |
| 46 | + return ( |
| 47 | + <View> |
| 48 | + <Text>Is the banana fresh?</Text> |
| 49 | + <Text testID="bananaFresh"> |
| 50 | + {this.state.fresh ? 'fresh' : 'not fresh'} |
| 51 | + </Text> |
| 52 | + <TextInput |
| 53 | + testID="bananaCustomFreshness" |
| 54 | + placeholder={PLACEHOLDER_FRESHNESS} |
| 55 | + value={INPUT_FRESHNESS} |
| 56 | + /> |
| 57 | + <TextInput |
| 58 | + testID="bananaChef" |
| 59 | + placeholder={PLACEHOLDER_CHEF} |
| 60 | + value={INPUT_CHEF} |
| 61 | + /> |
| 62 | + <MyButton onPress={this.changeFresh} type="primary"> |
| 63 | + Change freshness! |
| 64 | + </MyButton> |
| 65 | + <Text testID="duplicateText">First Text</Text> |
| 66 | + <Text testID="duplicateText">Second Text</Text> |
| 67 | + <Text>{test}</Text> |
| 68 | + </View> |
| 69 | + ); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +const MyComponent = () => { |
| 74 | + return <Text>My Component</Text>; |
| 75 | +}; |
| 76 | + |
| 77 | +test('getByTestId returns only native elements', () => { |
| 78 | + const { getByTestId, getAllByTestId } = render( |
| 79 | + <View> |
| 80 | + <Text testID="text">Text</Text> |
| 81 | + <TextInput testID="textInput" /> |
| 82 | + <View testID="view" /> |
| 83 | + <Button testID="button" title="Button" onPress={jest.fn()} /> |
| 84 | + <MyComponent testID="myComponent" /> |
| 85 | + </View> |
| 86 | + ); |
| 87 | + |
| 88 | + expect(getByTestId('text')).toBeTruthy(); |
| 89 | + expect(getByTestId('textInput')).toBeTruthy(); |
| 90 | + expect(getByTestId('view')).toBeTruthy(); |
| 91 | + expect(getByTestId('button')).toBeTruthy(); |
| 92 | + |
| 93 | + expect(getAllByTestId('text')).toHaveLength(1); |
| 94 | + expect(getAllByTestId('textInput')).toHaveLength(1); |
| 95 | + expect(getAllByTestId('view')).toHaveLength(1); |
| 96 | + expect(getAllByTestId('button')).toHaveLength(1); |
| 97 | + |
| 98 | + expect(() => getByTestId('myComponent')).toThrowError( |
| 99 | + 'Unable to find an element with testID: myComponent' |
| 100 | + ); |
| 101 | + expect(() => getAllByTestId('myComponent')).toThrowError( |
| 102 | + 'Unable to find an element with testID: myComponent' |
| 103 | + ); |
| 104 | +}); |
| 105 | + |
| 106 | +test('supports a regex matcher', () => { |
| 107 | + const { getByTestId, getAllByTestId } = render( |
| 108 | + <View> |
| 109 | + <Text testID="text">Text</Text> |
| 110 | + <TextInput testID="textInput" /> |
| 111 | + <View testID="view" /> |
| 112 | + <Button testID="button" title="Button" onPress={jest.fn()} /> |
| 113 | + <MyComponent testID="myComponent" /> |
| 114 | + </View> |
| 115 | + ); |
| 116 | + |
| 117 | + expect(getByTestId(/view/)).toBeTruthy(); |
| 118 | + expect(getAllByTestId(/text/)).toHaveLength(2); |
| 119 | +}); |
| 120 | + |
| 121 | +test('getByTestId, queryByTestId', () => { |
| 122 | + const { getByTestId, queryByTestId } = render(<Banana />); |
| 123 | + const component = getByTestId('bananaFresh'); |
| 124 | + |
| 125 | + expect(component.props.children).toBe('not fresh'); |
| 126 | + expect(() => getByTestId('InExistent')).toThrow( |
| 127 | + 'Unable to find an element with testID: InExistent' |
| 128 | + ); |
| 129 | + |
| 130 | + expect(getByTestId('bananaFresh')).toBe(component); |
| 131 | + expect(queryByTestId('InExistent')).toBeNull(); |
| 132 | +}); |
| 133 | + |
| 134 | +test('getAllByTestId, queryAllByTestId', () => { |
| 135 | + const { getAllByTestId, queryAllByTestId } = render(<Banana />); |
| 136 | + const textElements = getAllByTestId('duplicateText'); |
| 137 | + |
| 138 | + expect(textElements.length).toBe(2); |
| 139 | + expect(textElements[0].props.children).toBe('First Text'); |
| 140 | + expect(textElements[1].props.children).toBe('Second Text'); |
| 141 | + expect(() => getAllByTestId('nonExistentTestId')).toThrow( |
| 142 | + 'Unable to find an element with testID: nonExistentTestId' |
| 143 | + ); |
| 144 | + |
| 145 | + const queriedTextElements = queryAllByTestId('duplicateText'); |
| 146 | + |
| 147 | + expect(queriedTextElements.length).toBe(2); |
| 148 | + expect(queriedTextElements[0]).toBe(textElements[0]); |
| 149 | + expect(queriedTextElements[1]).toBe(textElements[1]); |
| 150 | + expect(queryAllByTestId('nonExistentTestId')).toHaveLength(0); |
| 151 | +}); |
| 152 | + |
| 153 | +test('findByTestId and findAllByTestId work asynchronously', async () => { |
| 154 | + const options = { timeout: 10 }; // Short timeout so that this test runs quickly |
| 155 | + const { rerender, findByTestId, findAllByTestId } = render(<View />); |
| 156 | + await expect(findByTestId('aTestId', options)).rejects.toBeTruthy(); |
| 157 | + await expect(findAllByTestId('aTestId', options)).rejects.toBeTruthy(); |
| 158 | + |
| 159 | + setTimeout( |
| 160 | + () => |
| 161 | + rerender( |
| 162 | + <View testID="aTestId"> |
| 163 | + <Text>Some Text</Text> |
| 164 | + <TextInput placeholder="Placeholder Text" /> |
| 165 | + <TextInput value="Display Value" /> |
| 166 | + </View> |
| 167 | + ), |
| 168 | + 20 |
| 169 | + ); |
| 170 | + |
| 171 | + await expect(findByTestId('aTestId')).resolves.toBeTruthy(); |
| 172 | + await expect(findAllByTestId('aTestId')).resolves.toHaveLength(1); |
| 173 | +}, 20000); |
0 commit comments