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