|
1 | 1 | import * as React from 'react'; |
2 | | -import { View } from 'react-native'; |
3 | | -import TestRenderer from 'react-test-renderer'; |
4 | | -import { configureInternal, getConfig } from '../config'; |
| 2 | +import { Image, Modal, ScrollView, Switch, Text, TextInput } from 'react-native'; |
5 | 3 | import { |
6 | | - getHostComponentNames, |
7 | | - configureHostComponentNamesIfNeeded, |
| 4 | + isHostImage, |
| 5 | + isHostModal, |
| 6 | + isHostScrollView, |
| 7 | + isHostSwitch, |
| 8 | + isHostText, |
| 9 | + isHostTextInput, |
8 | 10 | } from '../helpers/host-component-names'; |
9 | | -import { act, render } from '..'; |
| 11 | +import { render, screen } from '..'; |
10 | 12 |
|
11 | | -describe('getHostComponentNames', () => { |
12 | | - test('returns host component names from internal config', () => { |
13 | | - configureInternal({ |
14 | | - hostComponentNames: { |
15 | | - text: 'banana', |
16 | | - textInput: 'banana', |
17 | | - image: 'banana', |
18 | | - switch: 'banana', |
19 | | - scrollView: 'banana', |
20 | | - modal: 'banana', |
21 | | - }, |
22 | | - }); |
23 | | - |
24 | | - expect(getHostComponentNames()).toEqual({ |
25 | | - text: 'banana', |
26 | | - textInput: 'banana', |
27 | | - image: 'banana', |
28 | | - switch: 'banana', |
29 | | - scrollView: 'banana', |
30 | | - modal: 'banana', |
31 | | - }); |
32 | | - }); |
33 | | - |
34 | | - test('detects host component names if not present in internal config', () => { |
35 | | - expect(getConfig().hostComponentNames).toBeUndefined(); |
36 | | - |
37 | | - const hostComponentNames = getHostComponentNames(); |
38 | | - |
39 | | - expect(hostComponentNames).toEqual({ |
40 | | - text: 'Text', |
41 | | - textInput: 'TextInput', |
42 | | - image: 'Image', |
43 | | - switch: 'RCTSwitch', |
44 | | - scrollView: 'RCTScrollView', |
45 | | - modal: 'Modal', |
46 | | - }); |
47 | | - expect(getConfig().hostComponentNames).toBe(hostComponentNames); |
48 | | - }); |
49 | | - |
50 | | - // Repro test for case when user indirectly triggers `getHostComponentNames` calls from |
51 | | - // explicit `act` wrapper. |
52 | | - // See: https://github.com/callstack/react-native-testing-library/issues/1302 |
53 | | - // and https://github.com/callstack/react-native-testing-library/issues/1305 |
54 | | - test('does not throw when wrapped in act after render has been called', () => { |
55 | | - render(<View />); |
56 | | - expect(() => |
57 | | - act(() => { |
58 | | - getHostComponentNames(); |
59 | | - }), |
60 | | - ).not.toThrow(); |
61 | | - }); |
| 13 | +test('detects host Text component', () => { |
| 14 | + render(<Text>Hello</Text>); |
| 15 | + expect(isHostText(screen.root)).toBe(true); |
62 | 16 | }); |
63 | 17 |
|
64 | | -describe('configureHostComponentNamesIfNeeded', () => { |
65 | | - test('updates internal config with host component names when they are not defined', () => { |
66 | | - expect(getConfig().hostComponentNames).toBeUndefined(); |
67 | | - |
68 | | - configureHostComponentNamesIfNeeded(); |
69 | | - |
70 | | - expect(getConfig().hostComponentNames).toEqual({ |
71 | | - text: 'Text', |
72 | | - textInput: 'TextInput', |
73 | | - image: 'Image', |
74 | | - switch: 'RCTSwitch', |
75 | | - scrollView: 'RCTScrollView', |
76 | | - modal: 'Modal', |
77 | | - }); |
78 | | - }); |
79 | | - |
80 | | - test('does not update internal config when host component names are already configured', () => { |
81 | | - configureInternal({ |
82 | | - hostComponentNames: { |
83 | | - text: 'banana', |
84 | | - textInput: 'banana', |
85 | | - image: 'banana', |
86 | | - switch: 'banana', |
87 | | - scrollView: 'banana', |
88 | | - modal: 'banana', |
89 | | - }, |
90 | | - }); |
91 | | - |
92 | | - configureHostComponentNamesIfNeeded(); |
93 | | - |
94 | | - expect(getConfig().hostComponentNames).toEqual({ |
95 | | - text: 'banana', |
96 | | - textInput: 'banana', |
97 | | - image: 'banana', |
98 | | - switch: 'banana', |
99 | | - scrollView: 'banana', |
100 | | - modal: 'banana', |
101 | | - }); |
102 | | - }); |
103 | | - |
104 | | - test('throw an error when auto-detection fails', () => { |
105 | | - const mockCreate = jest.spyOn(TestRenderer, 'create') as jest.Mock; |
106 | | - const renderer = TestRenderer.create(<View />); |
| 18 | +// Some users might use the raw RCTText component directly for performance reasons. |
| 19 | +// See: https://blog.theodo.com/2023/10/native-views-rn-performance/ |
| 20 | +test('detects raw RCTText component', () => { |
| 21 | + render(React.createElement('RCTText', { testID: 'text' }, 'Hello')); |
| 22 | + expect(isHostText(screen.root)).toBe(true); |
| 23 | +}); |
107 | 24 |
|
108 | | - mockCreate.mockReturnValue({ |
109 | | - root: renderer.root, |
110 | | - }); |
| 25 | +test('detects host TextInput component', () => { |
| 26 | + render(<TextInput />); |
| 27 | + expect(isHostTextInput(screen.root)).toBe(true); |
| 28 | +}); |
111 | 29 |
|
112 | | - expect(() => configureHostComponentNamesIfNeeded()).toThrowErrorMatchingInlineSnapshot(` |
113 | | - "Trying to detect host component names triggered the following error: |
| 30 | +test('detects host Image component', () => { |
| 31 | + render(<Image />); |
| 32 | + expect(isHostImage(screen.root)).toBe(true); |
| 33 | +}); |
114 | 34 |
|
115 | | - Unable to find an element with testID: text |
| 35 | +test('detects host Switch component', () => { |
| 36 | + render(<Switch />); |
| 37 | + expect(isHostSwitch(screen.root)).toBe(true); |
| 38 | +}); |
116 | 39 |
|
117 | | - There seems to be an issue with your configuration that prevents React Native Testing Library from working correctly. |
118 | | - Please check if you are using compatible versions of React Native and React Native Testing Library." |
119 | | - `); |
| 40 | +test('detects host ScrollView component', () => { |
| 41 | + render(<ScrollView />); |
| 42 | + expect(isHostScrollView(screen.root)).toBe(true); |
| 43 | +}); |
120 | 44 |
|
121 | | - mockCreate.mockReset(); |
122 | | - }); |
| 45 | +test('detects host Modal component', () => { |
| 46 | + render(<Modal />); |
| 47 | + expect(isHostModal(screen.root)).toBe(true); |
123 | 48 | }); |
0 commit comments