|
| 1 | +import * as React from 'react'; |
| 2 | +import { View, Text, TextInput } from 'react-native'; |
| 3 | +import { render } from '..'; |
| 4 | +import { getHostSelf } from '../helpers/component-tree'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Tests in this file are intended to give us an proactive warning that React Native behavior has |
| 8 | + * changed in a way that may impact our code like queries or event handling. |
| 9 | + */ |
| 10 | + |
| 11 | +test('React Native API assumption: <View> renders single host element', () => { |
| 12 | + const view = render(<View testID="test" />); |
| 13 | + const hostView = view.getByTestId('test'); |
| 14 | + expect(getHostSelf(hostView)).toBe(hostView); |
| 15 | + |
| 16 | + expect(view.toJSON()).toMatchInlineSnapshot(` |
| 17 | + <View |
| 18 | + testID="test" |
| 19 | + /> |
| 20 | + `); |
| 21 | +}); |
| 22 | + |
| 23 | +test('React Native API assumption: <Text> renders single host element', () => { |
| 24 | + const view = render(<Text testID="test">Hello</Text>); |
| 25 | + const compositeView = view.getByText('Hello'); |
| 26 | + const hostView = view.getByTestId('test'); |
| 27 | + expect(getHostSelf(compositeView)).toBe(hostView); |
| 28 | + |
| 29 | + expect(view.toJSON()).toMatchInlineSnapshot(` |
| 30 | + <Text |
| 31 | + testID="test" |
| 32 | + > |
| 33 | + Hello |
| 34 | + </Text> |
| 35 | + `); |
| 36 | +}); |
| 37 | + |
| 38 | +test('React Native API assumption: nested <Text> renders single host element', () => { |
| 39 | + const view = render( |
| 40 | + <Text testID="test"> |
| 41 | + <Text testID="before">Before</Text> |
| 42 | + Hello |
| 43 | + <Text testID="after"> |
| 44 | + <Text testID="deeplyNested">Deeply nested</Text> |
| 45 | + </Text> |
| 46 | + </Text> |
| 47 | + ); |
| 48 | + expect(getHostSelf(view.getByText('Hello'))).toBe(view.getByTestId('test')); |
| 49 | + expect(getHostSelf(view.getByText('Before'))).toBe( |
| 50 | + view.getByTestId('before') |
| 51 | + ); |
| 52 | + expect(getHostSelf(view.getByText('Deeply nested'))).toBe( |
| 53 | + view.getByTestId('deeplyNested') |
| 54 | + ); |
| 55 | + |
| 56 | + expect(view.toJSON()).toMatchInlineSnapshot(` |
| 57 | + <Text |
| 58 | + testID="test" |
| 59 | + > |
| 60 | + <Text |
| 61 | + testID="before" |
| 62 | + > |
| 63 | + Before |
| 64 | + </Text> |
| 65 | + Hello |
| 66 | + <Text |
| 67 | + testID="after" |
| 68 | + > |
| 69 | + <Text |
| 70 | + testID="deeplyNested" |
| 71 | + > |
| 72 | + Deeply nested |
| 73 | + </Text> |
| 74 | + </Text> |
| 75 | + </Text> |
| 76 | + `); |
| 77 | +}); |
| 78 | + |
| 79 | +test('React Native API assumption: <TextInput> renders single host element', () => { |
| 80 | + const view = render( |
| 81 | + <TextInput |
| 82 | + testID="test" |
| 83 | + defaultValue="default" |
| 84 | + value="currentValue" |
| 85 | + placeholder="Placeholder" |
| 86 | + /> |
| 87 | + ); |
| 88 | + const compositeView = view.getByPlaceholderText('Placeholder'); |
| 89 | + const hostView = view.getByTestId('test'); |
| 90 | + expect(getHostSelf(compositeView)).toBe(hostView); |
| 91 | + |
| 92 | + expect(view.toJSON()).toMatchInlineSnapshot(` |
| 93 | + <TextInput |
| 94 | + defaultValue="default" |
| 95 | + placeholder="Placeholder" |
| 96 | + testID="test" |
| 97 | + value="currentValue" |
| 98 | + /> |
| 99 | + `); |
| 100 | +}); |
0 commit comments