|
| 1 | +import test from 'tape'; |
| 2 | + |
| 3 | +import getChildComponent from '../../../src/util/getChildComponent'; |
| 4 | +import JSXAttributeMock from '../../../__mocks__/JSXAttributeMock'; |
| 5 | +import JSXElementMock from '../../../__mocks__/JSXElementMock'; |
| 6 | +import JSXExpressionContainerMock from '../../../__mocks__/JSXExpressionContainerMock'; |
| 7 | + |
| 8 | +test('mayContainChildComponent', (t) => { |
| 9 | + t.equal( |
| 10 | + getChildComponent( |
| 11 | + JSXElementMock('div', [], [ |
| 12 | + JSXElementMock('div', [], [ |
| 13 | + JSXElementMock('span', [], []), |
| 14 | + JSXElementMock('span', [], [ |
| 15 | + JSXElementMock('span', [], []), |
| 16 | + JSXElementMock('span', [], [ |
| 17 | + JSXElementMock('span', [], []), |
| 18 | + ]), |
| 19 | + ]), |
| 20 | + ]), |
| 21 | + JSXElementMock('span', [], []), |
| 22 | + JSXElementMock('img', [ |
| 23 | + JSXAttributeMock('src', 'some/path'), |
| 24 | + ]), |
| 25 | + ]), |
| 26 | + 'FancyComponent', |
| 27 | + 5, |
| 28 | + ), |
| 29 | + undefined, |
| 30 | + 'no FancyComponent returns undefined', |
| 31 | + ); |
| 32 | + |
| 33 | + t.test('contains an indicated component', (st) => { |
| 34 | + const inputMock = JSXElementMock('input'); |
| 35 | + st.equal( |
| 36 | + getChildComponent( |
| 37 | + JSXElementMock('div', [], [ |
| 38 | + inputMock, |
| 39 | + ]), |
| 40 | + 'input', |
| 41 | + ), |
| 42 | + inputMock, |
| 43 | + 'returns input', |
| 44 | + ); |
| 45 | + |
| 46 | + const FancyComponentMock = JSXElementMock('FancyComponent'); |
| 47 | + st.equal( |
| 48 | + getChildComponent( |
| 49 | + JSXElementMock('div', [], [ |
| 50 | + FancyComponentMock, |
| 51 | + ]), |
| 52 | + 'FancyComponent', |
| 53 | + ), |
| 54 | + FancyComponentMock, |
| 55 | + 'returns FancyComponent', |
| 56 | + ); |
| 57 | + |
| 58 | + st.equal( |
| 59 | + getChildComponent( |
| 60 | + JSXElementMock('div', [], [ |
| 61 | + JSXElementMock('div', [], [ |
| 62 | + JSXElementMock('FancyComponent'), |
| 63 | + ]), |
| 64 | + ]), |
| 65 | + 'FancyComponent', |
| 66 | + ), |
| 67 | + undefined, |
| 68 | + 'FancyComponent is outside of default depth, should return undefined', |
| 69 | + ); |
| 70 | + |
| 71 | + st.equal( |
| 72 | + getChildComponent( |
| 73 | + JSXElementMock('div', [], [ |
| 74 | + JSXElementMock('div', [], [ |
| 75 | + FancyComponentMock, |
| 76 | + ]), |
| 77 | + ]), |
| 78 | + 'FancyComponent', |
| 79 | + 2, |
| 80 | + ), |
| 81 | + FancyComponentMock, |
| 82 | + 'FancyComponent is inside of custom depth, should return FancyComponent', |
| 83 | + ); |
| 84 | + |
| 85 | + st.equal( |
| 86 | + getChildComponent( |
| 87 | + JSXElementMock('div', [], [ |
| 88 | + JSXElementMock('div', [], [ |
| 89 | + JSXElementMock('span', [], []), |
| 90 | + JSXElementMock('span', [], [ |
| 91 | + JSXElementMock('span', [], []), |
| 92 | + JSXElementMock('span', [], [ |
| 93 | + JSXElementMock('span', [], [ |
| 94 | + JSXElementMock('span', [], [ |
| 95 | + FancyComponentMock, |
| 96 | + ]), |
| 97 | + ]), |
| 98 | + ]), |
| 99 | + ]), |
| 100 | + ]), |
| 101 | + JSXElementMock('span', [], []), |
| 102 | + JSXElementMock('img', [ |
| 103 | + JSXAttributeMock('src', 'some/path'), |
| 104 | + ]), |
| 105 | + ]), |
| 106 | + 'FancyComponent', |
| 107 | + 6, |
| 108 | + ), |
| 109 | + FancyComponentMock, |
| 110 | + 'deep nesting, returns FancyComponent', |
| 111 | + ); |
| 112 | + |
| 113 | + st.end(); |
| 114 | + }); |
| 115 | + |
| 116 | + const MysteryBox = JSXExpressionContainerMock('mysteryBox'); |
| 117 | + t.equal( |
| 118 | + getChildComponent( |
| 119 | + JSXElementMock('div', [], [ |
| 120 | + MysteryBox, |
| 121 | + ]), |
| 122 | + 'FancyComponent', |
| 123 | + ), |
| 124 | + MysteryBox, |
| 125 | + 'Indeterminate situations + expression container children - returns component', |
| 126 | + ); |
| 127 | + |
| 128 | + t.test('Glob name matching - component name contains question mark ? - match any single character', (st) => { |
| 129 | + const FancyComponentMock = JSXElementMock('FancyComponent'); |
| 130 | + st.equal( |
| 131 | + getChildComponent( |
| 132 | + JSXElementMock('div', [], [ |
| 133 | + FancyComponentMock, |
| 134 | + ]), |
| 135 | + 'Fanc?Co??onent', |
| 136 | + ), |
| 137 | + FancyComponentMock, |
| 138 | + 'returns component', |
| 139 | + ); |
| 140 | + |
| 141 | + st.equal( |
| 142 | + getChildComponent( |
| 143 | + JSXElementMock('div', [], [ |
| 144 | + JSXElementMock('FancyComponent'), |
| 145 | + ]), |
| 146 | + 'FancyComponent?', |
| 147 | + ), |
| 148 | + undefined, |
| 149 | + 'returns undefined', |
| 150 | + ); |
| 151 | + |
| 152 | + st.test('component name contains asterisk * - match zero or more characters', (s2t) => { |
| 153 | + s2t.equal( |
| 154 | + getChildComponent( |
| 155 | + JSXElementMock('div', [], [ |
| 156 | + FancyComponentMock, |
| 157 | + ]), |
| 158 | + 'Fancy*', |
| 159 | + ), |
| 160 | + FancyComponentMock, |
| 161 | + 'returns component', |
| 162 | + ); |
| 163 | + |
| 164 | + s2t.equal( |
| 165 | + getChildComponent( |
| 166 | + JSXElementMock('div', [], [ |
| 167 | + FancyComponentMock, |
| 168 | + ]), |
| 169 | + '*Component', |
| 170 | + ), |
| 171 | + FancyComponentMock, |
| 172 | + 'returns component', |
| 173 | + ); |
| 174 | + |
| 175 | + s2t.equal( |
| 176 | + getChildComponent( |
| 177 | + JSXElementMock('div', [], [ |
| 178 | + FancyComponentMock, |
| 179 | + ]), |
| 180 | + 'Fancy*C*t', |
| 181 | + ), |
| 182 | + FancyComponentMock, |
| 183 | + 'returns component', |
| 184 | + ); |
| 185 | + |
| 186 | + s2t.end(); |
| 187 | + }); |
| 188 | + |
| 189 | + st.end(); |
| 190 | + }); |
| 191 | + |
| 192 | + t.test('using a custom elementType function', (st) => { |
| 193 | + const CustomInputMock = JSXElementMock('CustomInput'); |
| 194 | + st.equal( |
| 195 | + getChildComponent( |
| 196 | + JSXElementMock('div', [], [ |
| 197 | + CustomInputMock, |
| 198 | + ]), |
| 199 | + 'input', |
| 200 | + 2, |
| 201 | + () => 'input', |
| 202 | + ), |
| 203 | + CustomInputMock, |
| 204 | + 'returns the component when the custom elementType returns the proper name', |
| 205 | + ); |
| 206 | + |
| 207 | + st.equal( |
| 208 | + getChildComponent( |
| 209 | + JSXElementMock('div', [], [ |
| 210 | + JSXElementMock('CustomInput'), |
| 211 | + ]), |
| 212 | + 'input', |
| 213 | + 2, |
| 214 | + () => 'button', |
| 215 | + ), |
| 216 | + undefined, |
| 217 | + 'returns undefined when the custom elementType returns a wrong name', |
| 218 | + ); |
| 219 | + |
| 220 | + st.end(); |
| 221 | + }); |
| 222 | + |
| 223 | + t.end(); |
| 224 | +}); |
0 commit comments