Skip to content

Commit c28a601

Browse files
rubennortefacebook-github-bot
authored andcommitted
Add tests for refs in Text (#52980)
Summary: Pull Request resolved: #52980 Changelog: [internal] Adds Fantom tests for the behavior of `refs` in `Text` components. Reviewed By: rshest Differential Revision: D79436148 fbshipit-source-id: 6d357740303f4868d176af7b4559891af77b79f8
1 parent e8c6a53 commit c28a601

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

packages/react-native/Libraries/Text/__tests__/Text-itest.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@
1010

1111
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
1212

13+
import type {HostInstance} from 'react-native';
14+
15+
import ensureInstance from '../../../src/private/__tests__/utilities/ensureInstance';
1316
import * as Fantom from '@react-native/fantom';
1417
import * as React from 'react';
18+
import {createRef} from 'react';
1519
import {Text} from 'react-native';
20+
import ReactNativeElement from 'react-native/src/private/webapis/dom/nodes/ReactNativeElement';
21+
import ReadOnlyText from 'react-native/src/private/webapis/dom/nodes/ReadOnlyText';
1622

1723
describe('<Text>', () => {
1824
describe('props', () => {
@@ -60,4 +66,68 @@ describe('<Text>', () => {
6066
});
6167
});
6268
});
69+
70+
describe('ref', () => {
71+
it('is a element node', () => {
72+
const elementRef = createRef<HostInstance>();
73+
74+
const root = Fantom.createRoot();
75+
76+
Fantom.runTask(() => {
77+
root.render(<Text ref={elementRef}>Some text</Text>);
78+
});
79+
80+
const element = ensureInstance(elementRef.current, ReactNativeElement);
81+
expect(element.tagName).toBe('RN:Paragraph');
82+
});
83+
84+
it('has a single text child node when not nested', () => {
85+
const elementRef = createRef<HostInstance>();
86+
87+
const root = Fantom.createRoot();
88+
89+
Fantom.runTask(() => {
90+
root.render(<Text ref={elementRef}>Some text</Text>);
91+
});
92+
93+
const element = ensureInstance(elementRef.current, ReactNativeElement);
94+
expect(element.childNodes.length).toBe(1);
95+
96+
const textChild = ensureInstance(element.childNodes[0], ReadOnlyText);
97+
expect(textChild.textContent).toBe('Some text');
98+
});
99+
100+
it('has text and element child nodes when nested', () => {
101+
const elementRef = createRef<HostInstance>();
102+
103+
const root = Fantom.createRoot();
104+
105+
Fantom.runTask(() => {
106+
root.render(
107+
<Text ref={elementRef}>
108+
Some text <Text style={{fontWeight: 'bold'}}>also in bold</Text>
109+
</Text>,
110+
);
111+
});
112+
113+
const element = ensureInstance(elementRef.current, ReactNativeElement);
114+
expect(element.childNodes.length).toBe(2);
115+
116+
const firstChild = ensureInstance(element.childNodes[0], ReadOnlyText);
117+
expect(firstChild.textContent).toBe('Some text ');
118+
119+
const secondChild = ensureInstance(
120+
element.childNodes[1],
121+
ReactNativeElement,
122+
);
123+
expect(secondChild.tagName).toBe('RN:Text');
124+
expect(secondChild.childNodes.length).toBe(1);
125+
126+
const secondChildText = ensureInstance(
127+
secondChild.childNodes[0],
128+
ReadOnlyText,
129+
);
130+
expect(secondChildText.textContent).toBe('also in bold');
131+
});
132+
});
63133
});

0 commit comments

Comments
 (0)