|
10 | 10 |
|
11 | 11 | import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; |
12 | 12 |
|
| 13 | +import type {HostInstance} from 'react-native'; |
| 14 | + |
| 15 | +import ensureInstance from '../../../src/private/__tests__/utilities/ensureInstance'; |
13 | 16 | import * as Fantom from '@react-native/fantom'; |
14 | 17 | import * as React from 'react'; |
| 18 | +import {createRef} from 'react'; |
15 | 19 | 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'; |
16 | 22 |
|
17 | 23 | describe('<Text>', () => { |
18 | 24 | describe('props', () => { |
@@ -60,4 +66,68 @@ describe('<Text>', () => { |
60 | 66 | }); |
61 | 67 | }); |
62 | 68 | }); |
| 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 | + }); |
63 | 133 | }); |
0 commit comments