|
| 1 | +// @flow |
| 2 | +import * as React from 'react'; |
| 3 | +import { View, TextInput } from 'react-native'; |
| 4 | + |
| 5 | +import { render } from '..'; |
| 6 | + |
| 7 | +const PLACEHOLDER_FRESHNESS = 'Add custom freshness'; |
| 8 | +const PLACEHOLDER_CHEF = 'Who inspected freshness?'; |
| 9 | +const INPUT_FRESHNESS = 'Custom Freshie'; |
| 10 | +const INPUT_CHEF = 'I inspected freshie'; |
| 11 | +const DEFAULT_INPUT_CHEF = 'What did you inspect?'; |
| 12 | +const DEFAULT_INPUT_CUSTOMER = 'What banana?'; |
| 13 | + |
| 14 | +const Banana = () => ( |
| 15 | + <View> |
| 16 | + <TextInput |
| 17 | + testID="bananaCustomFreshness" |
| 18 | + placeholder={PLACEHOLDER_FRESHNESS} |
| 19 | + value={INPUT_FRESHNESS} |
| 20 | + /> |
| 21 | + <TextInput |
| 22 | + testID="bananaChef" |
| 23 | + placeholder={PLACEHOLDER_CHEF} |
| 24 | + value={INPUT_CHEF} |
| 25 | + defaultValue={DEFAULT_INPUT_CHEF} |
| 26 | + /> |
| 27 | + <TextInput defaultValue={DEFAULT_INPUT_CUSTOMER} /> |
| 28 | + <TextInput defaultValue={'hello'} value="" /> |
| 29 | + </View> |
| 30 | +); |
| 31 | + |
| 32 | +test('getByDisplayValue, queryByDisplayValue', () => { |
| 33 | + const { getByDisplayValue, queryByDisplayValue } = render(<Banana />); |
| 34 | + const input = getByDisplayValue(/custom/i); |
| 35 | + |
| 36 | + expect(input.props.value).toBe(INPUT_FRESHNESS); |
| 37 | + |
| 38 | + const sameInput = getByDisplayValue(INPUT_FRESHNESS); |
| 39 | + |
| 40 | + expect(sameInput.props.value).toBe(INPUT_FRESHNESS); |
| 41 | + expect(() => getByDisplayValue('no value')).toThrow( |
| 42 | + 'Unable to find an element with displayValue: no value' |
| 43 | + ); |
| 44 | + |
| 45 | + expect(queryByDisplayValue(/custom/i)).toBe(input); |
| 46 | + expect(queryByDisplayValue('no value')).toBeNull(); |
| 47 | + expect(() => queryByDisplayValue(/fresh/i)).toThrow( |
| 48 | + 'Found multiple elements with display value: /fresh/i' |
| 49 | + ); |
| 50 | +}); |
| 51 | + |
| 52 | +test('getByDisplayValue, queryByDisplayValue get element by default value only when value is undefined', () => { |
| 53 | + const { getByDisplayValue, queryByDisplayValue } = render(<Banana />); |
| 54 | + expect(() => getByDisplayValue(DEFAULT_INPUT_CHEF)).toThrow(); |
| 55 | + expect(queryByDisplayValue(DEFAULT_INPUT_CHEF)).toBeNull(); |
| 56 | + |
| 57 | + expect(() => getByDisplayValue('hello')).toThrow(); |
| 58 | + expect(queryByDisplayValue('hello')).toBeNull(); |
| 59 | + |
| 60 | + expect(getByDisplayValue(DEFAULT_INPUT_CUSTOMER)).toBeTruthy(); |
| 61 | + expect(queryByDisplayValue(DEFAULT_INPUT_CUSTOMER)).toBeTruthy(); |
| 62 | +}); |
| 63 | + |
| 64 | +test('getAllByDisplayValue, queryAllByDisplayValue', () => { |
| 65 | + const { getAllByDisplayValue, queryAllByDisplayValue } = render(<Banana />); |
| 66 | + const inputs = getAllByDisplayValue(/fresh/i); |
| 67 | + |
| 68 | + expect(inputs).toHaveLength(2); |
| 69 | + expect(() => getAllByDisplayValue('no value')).toThrow( |
| 70 | + 'Unable to find an element with displayValue: no value' |
| 71 | + ); |
| 72 | + |
| 73 | + expect(queryAllByDisplayValue(/fresh/i)).toEqual(inputs); |
| 74 | + expect(queryAllByDisplayValue('no value')).toHaveLength(0); |
| 75 | +}); |
| 76 | + |
| 77 | +test('findBy queries work asynchronously', async () => { |
| 78 | + const options = { timeout: 10 }; // Short timeout so that this test runs quickly |
| 79 | + const { rerender, findByDisplayValue, findAllByDisplayValue } = render( |
| 80 | + <View /> |
| 81 | + ); |
| 82 | + |
| 83 | + await expect( |
| 84 | + findByDisplayValue('Display Value', options) |
| 85 | + ).rejects.toBeTruthy(); |
| 86 | + await expect( |
| 87 | + findAllByDisplayValue('Display Value', options) |
| 88 | + ).rejects.toBeTruthy(); |
| 89 | + |
| 90 | + setTimeout( |
| 91 | + () => |
| 92 | + rerender( |
| 93 | + <View> |
| 94 | + <TextInput value="Display Value" /> |
| 95 | + </View> |
| 96 | + ), |
| 97 | + 20 |
| 98 | + ); |
| 99 | + |
| 100 | + await expect(findByDisplayValue('Display Value')).resolves.toBeTruthy(); |
| 101 | + await expect(findAllByDisplayValue('Display Value')).resolves.toHaveLength(1); |
| 102 | +}, 20000); |
0 commit comments