|
| 1 | +import React from 'react'; |
| 2 | +import { View, Text, TextInput } from 'react-native'; |
| 3 | +import { render } from '../..'; |
| 4 | +import { |
| 5 | + getHostChildren, |
| 6 | + getHostParent, |
| 7 | + getHostSelves, |
| 8 | + getHostSiblings, |
| 9 | +} from '../component-tree'; |
| 10 | + |
| 11 | +function MultipleHostChildren() { |
| 12 | + return ( |
| 13 | + <> |
| 14 | + <View testID="child1" /> |
| 15 | + <View testID="child2" /> |
| 16 | + <View testID="child3" /> |
| 17 | + </> |
| 18 | + ); |
| 19 | +} |
| 20 | + |
| 21 | +test('returns host parent for host component', () => { |
| 22 | + const view = render( |
| 23 | + <View testID="grandparent"> |
| 24 | + <View testID="parent"> |
| 25 | + <View testID="subject" /> |
| 26 | + <View testID="sibling" /> |
| 27 | + </View> |
| 28 | + </View> |
| 29 | + ); |
| 30 | + |
| 31 | + const hostParent = getHostParent(view.getByTestId('subject')); |
| 32 | + expect(hostParent).toBe(view.getByTestId('parent')); |
| 33 | + |
| 34 | + const hostGrandparent = getHostParent(hostParent); |
| 35 | + expect(hostGrandparent).toBe(view.getByTestId('grandparent')); |
| 36 | + |
| 37 | + expect(getHostParent(hostGrandparent)).toBe(null); |
| 38 | +}); |
| 39 | + |
| 40 | +test('returns host parent for composite component', () => { |
| 41 | + const view = render( |
| 42 | + <View testID="parent"> |
| 43 | + <MultipleHostChildren /> |
| 44 | + <View testID="subject" /> |
| 45 | + </View> |
| 46 | + ); |
| 47 | + |
| 48 | + const compositeComponent = view.UNSAFE_getByType(MultipleHostChildren); |
| 49 | + const hostParent = getHostParent(compositeComponent); |
| 50 | + expect(hostParent).toBe(view.getByTestId('parent')); |
| 51 | +}); |
| 52 | + |
| 53 | +test('returns host children for host component', () => { |
| 54 | + const view = render( |
| 55 | + <View testID="grandparent"> |
| 56 | + <View testID="parent"> |
| 57 | + <View testID="subject" /> |
| 58 | + <View testID="sibling" /> |
| 59 | + </View> |
| 60 | + </View> |
| 61 | + ); |
| 62 | + |
| 63 | + const hostSubject = view.getByTestId('subject'); |
| 64 | + expect(getHostChildren(hostSubject)).toEqual([]); |
| 65 | + |
| 66 | + const hostSibling = view.getByTestId('sibling'); |
| 67 | + const hostParent = view.getByTestId('parent'); |
| 68 | + expect(getHostChildren(hostParent)).toEqual([hostSubject, hostSibling]); |
| 69 | + |
| 70 | + const hostGrandparent = view.getByTestId('grandparent'); |
| 71 | + expect(getHostChildren(hostGrandparent)).toEqual([hostParent]); |
| 72 | +}); |
| 73 | + |
| 74 | +test('returns host children for composite component', () => { |
| 75 | + const view = render( |
| 76 | + <View testID="parent"> |
| 77 | + <MultipleHostChildren /> |
| 78 | + <View testID="subject" /> |
| 79 | + <View testID="sibling" /> |
| 80 | + </View> |
| 81 | + ); |
| 82 | + |
| 83 | + expect(getHostChildren(view.getByTestId('parent'))).toEqual([ |
| 84 | + view.getByTestId('child1'), |
| 85 | + view.getByTestId('child2'), |
| 86 | + view.getByTestId('child3'), |
| 87 | + view.getByTestId('subject'), |
| 88 | + view.getByTestId('sibling'), |
| 89 | + ]); |
| 90 | +}); |
| 91 | + |
| 92 | +test('returns host selves for host components', () => { |
| 93 | + const view = render( |
| 94 | + <View testID="grandparent"> |
| 95 | + <View testID="parent"> |
| 96 | + <View testID="subject" /> |
| 97 | + <View testID="sibling" /> |
| 98 | + </View> |
| 99 | + </View> |
| 100 | + ); |
| 101 | + |
| 102 | + const hostSubject = view.getByTestId('subject'); |
| 103 | + expect(getHostSelves(hostSubject)).toEqual([hostSubject]); |
| 104 | + |
| 105 | + const hostSibling = view.getByTestId('sibling'); |
| 106 | + expect(getHostSelves(hostSibling)).toEqual([hostSibling]); |
| 107 | + |
| 108 | + const hostParent = view.getByTestId('parent'); |
| 109 | + expect(getHostSelves(hostParent)).toEqual([hostParent]); |
| 110 | + |
| 111 | + const hostGrandparent = view.getByTestId('grandparent'); |
| 112 | + expect(getHostSelves(hostGrandparent)).toEqual([hostGrandparent]); |
| 113 | +}); |
| 114 | + |
| 115 | +test('returns host selves for React Native composite components', () => { |
| 116 | + const view = render( |
| 117 | + <View testID="parent"> |
| 118 | + <Text testID="text">Text</Text> |
| 119 | + <TextInput |
| 120 | + testID="textInput" |
| 121 | + defaultValue="TextInputValue" |
| 122 | + placeholder="TextInputPlaceholder" |
| 123 | + /> |
| 124 | + </View> |
| 125 | + ); |
| 126 | + |
| 127 | + const compositeText = view.getByText('Text'); |
| 128 | + const hostText = view.getByTestId('text'); |
| 129 | + expect(getHostSelves(compositeText)).toEqual([hostText]); |
| 130 | + |
| 131 | + const compositeTextInputByValue = view.getByDisplayValue('TextInputValue'); |
| 132 | + const compositeTextInputByPlaceholder = view.getByPlaceholderText( |
| 133 | + 'TextInputPlaceholder' |
| 134 | + ); |
| 135 | + const hostTextInput = view.getByTestId('textInput'); |
| 136 | + expect(getHostSelves(compositeTextInputByValue)).toEqual([hostTextInput]); |
| 137 | + expect(getHostSelves(compositeTextInputByPlaceholder)).toEqual([ |
| 138 | + hostTextInput, |
| 139 | + ]); |
| 140 | +}); |
| 141 | + |
| 142 | +test('returns host selves for custom composite components', () => { |
| 143 | + const view = render( |
| 144 | + <View testID="parent"> |
| 145 | + <MultipleHostChildren /> |
| 146 | + <View testID="sibling" /> |
| 147 | + </View> |
| 148 | + ); |
| 149 | + |
| 150 | + const compositeComponent = view.UNSAFE_getByType(MultipleHostChildren); |
| 151 | + const hostChild1 = view.getByTestId('child1'); |
| 152 | + const hostChild2 = view.getByTestId('child2'); |
| 153 | + const hostChild3 = view.getByTestId('child3'); |
| 154 | + expect(getHostSelves(compositeComponent)).toEqual([ |
| 155 | + hostChild1, |
| 156 | + hostChild2, |
| 157 | + hostChild3, |
| 158 | + ]); |
| 159 | +}); |
| 160 | + |
| 161 | +test('returns host siblings for host component', () => { |
| 162 | + const view = render( |
| 163 | + <View testID="grandparent"> |
| 164 | + <View testID="parent"> |
| 165 | + <View testID="siblingBefore" /> |
| 166 | + <View testID="subject" /> |
| 167 | + <View testID="siblingAfter" /> |
| 168 | + <MultipleHostChildren /> |
| 169 | + </View> |
| 170 | + </View> |
| 171 | + ); |
| 172 | + |
| 173 | + const hostSiblings = getHostSiblings(view.getByTestId('subject')); |
| 174 | + expect(hostSiblings).toEqual([ |
| 175 | + view.getByTestId('siblingBefore'), |
| 176 | + view.getByTestId('siblingAfter'), |
| 177 | + view.getByTestId('child1'), |
| 178 | + view.getByTestId('child2'), |
| 179 | + view.getByTestId('child3'), |
| 180 | + ]); |
| 181 | +}); |
| 182 | + |
| 183 | +test('returns host siblings for composite component', () => { |
| 184 | + const view = render( |
| 185 | + <View testID="grandparent"> |
| 186 | + <View testID="parent"> |
| 187 | + <View testID="siblingBefore" /> |
| 188 | + <View testID="subject" /> |
| 189 | + <View testID="siblingAfter" /> |
| 190 | + <MultipleHostChildren /> |
| 191 | + </View> |
| 192 | + </View> |
| 193 | + ); |
| 194 | + |
| 195 | + const compositeComponent = view.UNSAFE_getByType(MultipleHostChildren); |
| 196 | + const hostSiblings = getHostSiblings(compositeComponent); |
| 197 | + expect(hostSiblings).toEqual([ |
| 198 | + view.getByTestId('siblingBefore'), |
| 199 | + view.getByTestId('subject'), |
| 200 | + view.getByTestId('siblingAfter'), |
| 201 | + ]); |
| 202 | +}); |
0 commit comments