|
| 1 | +import * as React from 'react'; |
| 2 | +import { View, TextInput } from 'react-native'; |
| 3 | + |
| 4 | +import { render } from '../..'; |
| 5 | +import { configureInternal } from '../../config'; |
| 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 | +beforeEach(() => configureInternal({ useBreakingChanges: true })); |
| 15 | + |
| 16 | +const Banana = () => ( |
| 17 | + <View> |
| 18 | + <TextInput |
| 19 | + testID="bananaCustomFreshness" |
| 20 | + placeholder={PLACEHOLDER_FRESHNESS} |
| 21 | + value={INPUT_FRESHNESS} |
| 22 | + /> |
| 23 | + <TextInput |
| 24 | + testID="bananaChef" |
| 25 | + placeholder={PLACEHOLDER_CHEF} |
| 26 | + value={INPUT_CHEF} |
| 27 | + defaultValue={DEFAULT_INPUT_CHEF} |
| 28 | + /> |
| 29 | + <TextInput defaultValue={DEFAULT_INPUT_CUSTOMER} /> |
| 30 | + <TextInput defaultValue={'hello'} value="" /> |
| 31 | + </View> |
| 32 | +); |
| 33 | + |
| 34 | +test('getByDisplayValue, queryByDisplayValue', () => { |
| 35 | + const { getByDisplayValue, queryByDisplayValue } = render(<Banana />); |
| 36 | + const input = getByDisplayValue(/custom/i); |
| 37 | + |
| 38 | + expect(input.props.value).toBe(INPUT_FRESHNESS); |
| 39 | + |
| 40 | + const sameInput = getByDisplayValue(INPUT_FRESHNESS); |
| 41 | + |
| 42 | + expect(sameInput.props.value).toBe(INPUT_FRESHNESS); |
| 43 | + expect(() => getByDisplayValue('no value')).toThrow( |
| 44 | + 'Unable to find an element with displayValue: no value' |
| 45 | + ); |
| 46 | + |
| 47 | + expect(queryByDisplayValue(/custom/i)).toBe(input); |
| 48 | + expect(queryByDisplayValue('no value')).toBeNull(); |
| 49 | + expect(() => queryByDisplayValue(/fresh/i)).toThrow( |
| 50 | + 'Found multiple elements with display value: /fresh/i' |
| 51 | + ); |
| 52 | +}); |
| 53 | + |
| 54 | +test('getByDisplayValue, queryByDisplayValue get element by default value only when value is undefined', () => { |
| 55 | + const { getByDisplayValue, queryByDisplayValue } = render(<Banana />); |
| 56 | + expect(() => |
| 57 | + getByDisplayValue(DEFAULT_INPUT_CHEF) |
| 58 | + ).toThrowErrorMatchingInlineSnapshot( |
| 59 | + `"Unable to find an element with displayValue: What did you inspect?"` |
| 60 | + ); |
| 61 | + expect(queryByDisplayValue(DEFAULT_INPUT_CHEF)).toBeNull(); |
| 62 | + |
| 63 | + expect(() => getByDisplayValue('hello')).toThrowErrorMatchingInlineSnapshot( |
| 64 | + `"Unable to find an element with displayValue: hello"` |
| 65 | + ); |
| 66 | + expect(queryByDisplayValue('hello')).toBeNull(); |
| 67 | + |
| 68 | + expect(getByDisplayValue(DEFAULT_INPUT_CUSTOMER)).toBeTruthy(); |
| 69 | + expect(queryByDisplayValue(DEFAULT_INPUT_CUSTOMER)).toBeTruthy(); |
| 70 | +}); |
| 71 | + |
| 72 | +test('getAllByDisplayValue, queryAllByDisplayValue', () => { |
| 73 | + const { getAllByDisplayValue, queryAllByDisplayValue } = render(<Banana />); |
| 74 | + const inputs = getAllByDisplayValue(/fresh/i); |
| 75 | + |
| 76 | + expect(inputs).toHaveLength(2); |
| 77 | + expect(() => getAllByDisplayValue('no value')).toThrow( |
| 78 | + 'Unable to find an element with displayValue: no value' |
| 79 | + ); |
| 80 | + |
| 81 | + expect(queryAllByDisplayValue(/fresh/i)).toEqual(inputs); |
| 82 | + expect(queryAllByDisplayValue('no value')).toHaveLength(0); |
| 83 | +}); |
| 84 | + |
| 85 | +test('findBy queries work asynchronously', async () => { |
| 86 | + const options = { timeout: 10 }; // Short timeout so that this test runs quickly |
| 87 | + const { rerender, findByDisplayValue, findAllByDisplayValue } = render( |
| 88 | + <View /> |
| 89 | + ); |
| 90 | + |
| 91 | + await expect( |
| 92 | + findByDisplayValue('Display Value', {}, options) |
| 93 | + ).rejects.toBeTruthy(); |
| 94 | + await expect( |
| 95 | + findAllByDisplayValue('Display Value', {}, options) |
| 96 | + ).rejects.toBeTruthy(); |
| 97 | + |
| 98 | + setTimeout( |
| 99 | + () => |
| 100 | + rerender( |
| 101 | + <View> |
| 102 | + <TextInput value="Display Value" /> |
| 103 | + </View> |
| 104 | + ), |
| 105 | + 20 |
| 106 | + ); |
| 107 | + |
| 108 | + await expect(findByDisplayValue('Display Value')).resolves.toBeTruthy(); |
| 109 | + await expect(findAllByDisplayValue('Display Value')).resolves.toHaveLength(1); |
| 110 | +}, 20000); |
| 111 | + |
| 112 | +test('byDisplayValue queries support hidden option', () => { |
| 113 | + const { getByDisplayValue, queryByDisplayValue } = render( |
| 114 | + <TextInput value="hidden" style={{ display: 'none' }} /> |
| 115 | + ); |
| 116 | + |
| 117 | + expect(getByDisplayValue('hidden')).toBeTruthy(); |
| 118 | + expect( |
| 119 | + getByDisplayValue('hidden', { includeHiddenElements: true }) |
| 120 | + ).toBeTruthy(); |
| 121 | + |
| 122 | + expect( |
| 123 | + queryByDisplayValue('hidden', { includeHiddenElements: false }) |
| 124 | + ).toBeFalsy(); |
| 125 | + expect(() => |
| 126 | + getByDisplayValue('hidden', { includeHiddenElements: false }) |
| 127 | + ).toThrowErrorMatchingInlineSnapshot( |
| 128 | + `"Unable to find an element with displayValue: hidden"` |
| 129 | + ); |
| 130 | +}); |
| 131 | + |
| 132 | +test('byDisplayValue should return host component', () => { |
| 133 | + const { getByDisplayValue } = render(<TextInput value="value" />); |
| 134 | + |
| 135 | + expect(getByDisplayValue('value').type).toBe('TextInput'); |
| 136 | +}); |
0 commit comments