Skip to content

Commit ce54ab3

Browse files
chore: ReactNative API assumptions early warning tests (#1143)
1 parent 932c547 commit ce54ab3

File tree

4 files changed

+360
-145
lines changed

4 files changed

+360
-145
lines changed

src/__tests__/jest-native.test.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ test('jest-native matchers work correctly', () => {
4040
expect(getByText('Disabled Button')).toBeDisabled();
4141
expect(getByText('Enabled Button')).not.toBeDisabled();
4242

43-
expect(getByA11yHint('Empty Text')).toBeEmpty();
44-
expect(getByA11yHint('Empty View')).toBeEmpty();
45-
expect(getByA11yHint('Not Empty Text')).not.toBeEmpty();
46-
expect(getByA11yHint('Not Empty View')).not.toBeEmpty();
43+
expect(getByA11yHint('Empty Text')).toBeEmptyElement();
44+
expect(getByA11yHint('Empty View')).toBeEmptyElement();
45+
expect(getByA11yHint('Not Empty Text')).not.toBeEmptyElement();
46+
expect(getByA11yHint('Not Empty View')).not.toBeEmptyElement();
4747

4848
expect(getByA11yHint('Container View')).toContainElement(
4949
// $FlowFixMe - TODO: fix @testing-library/jest-native flow typings
+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)