| 1 | import test from 'tape';
|
|---|
| 2 | import { elementType } from 'jsx-ast-utils';
|
|---|
| 3 |
|
|---|
| 4 | import hasAccessibleChild from '../../../src/util/hasAccessibleChild';
|
|---|
| 5 | import JSXElementMock from '../../../__mocks__/JSXElementMock';
|
|---|
| 6 | import JSXAttributeMock from '../../../__mocks__/JSXAttributeMock';
|
|---|
| 7 | import JSXExpressionContainerMock from '../../../__mocks__/JSXExpressionContainerMock';
|
|---|
| 8 |
|
|---|
| 9 | test('hasAccessibleChild', (t) => {
|
|---|
| 10 | t.equal(
|
|---|
| 11 | hasAccessibleChild(JSXElementMock('div', []), elementType),
|
|---|
| 12 | false,
|
|---|
| 13 | 'has no children and does not set dangerouslySetInnerHTML -> false',
|
|---|
| 14 | );
|
|---|
| 15 |
|
|---|
| 16 | t.equal(
|
|---|
| 17 | hasAccessibleChild(
|
|---|
| 18 | JSXElementMock('div', [JSXAttributeMock('dangerouslySetInnerHTML', true)], []),
|
|---|
| 19 | elementType,
|
|---|
| 20 | ),
|
|---|
| 21 | true,
|
|---|
| 22 | 'has no children and sets dangerouslySetInnerHTML -> true',
|
|---|
| 23 | );
|
|---|
| 24 |
|
|---|
| 25 | t.equal(
|
|---|
| 26 | hasAccessibleChild(
|
|---|
| 27 | JSXElementMock(
|
|---|
| 28 | 'div',
|
|---|
| 29 | [],
|
|---|
| 30 | [{
|
|---|
| 31 | type: 'Literal',
|
|---|
| 32 | value: 'foo',
|
|---|
| 33 | }],
|
|---|
| 34 | ),
|
|---|
| 35 | elementType,
|
|---|
| 36 | ),
|
|---|
| 37 | true,
|
|---|
| 38 | 'has children + Literal child -> true',
|
|---|
| 39 | );
|
|---|
| 40 |
|
|---|
| 41 | t.equal(
|
|---|
| 42 | hasAccessibleChild(
|
|---|
| 43 | JSXElementMock('div', [], [JSXElementMock('div', [])]),
|
|---|
| 44 | elementType,
|
|---|
| 45 | ),
|
|---|
| 46 | true,
|
|---|
| 47 | 'has children + visible JSXElement child -> true',
|
|---|
| 48 | );
|
|---|
| 49 |
|
|---|
| 50 | t.equal(
|
|---|
| 51 | hasAccessibleChild(
|
|---|
| 52 | JSXElementMock('div', [], [{
|
|---|
| 53 | type: 'JSXText',
|
|---|
| 54 | value: 'foo',
|
|---|
| 55 | }]),
|
|---|
| 56 | elementType,
|
|---|
| 57 | ),
|
|---|
| 58 | true,
|
|---|
| 59 | 'has children + JSText element -> true',
|
|---|
| 60 | );
|
|---|
| 61 |
|
|---|
| 62 | t.equal(
|
|---|
| 63 | hasAccessibleChild(
|
|---|
| 64 | JSXElementMock('div', [], [
|
|---|
| 65 | JSXElementMock('div', [
|
|---|
| 66 | JSXAttributeMock('aria-hidden', true),
|
|---|
| 67 | ]),
|
|---|
| 68 | ]),
|
|---|
| 69 | elementType,
|
|---|
| 70 | ),
|
|---|
| 71 | false,
|
|---|
| 72 | 'has children + hidden child JSXElement -> false',
|
|---|
| 73 | );
|
|---|
| 74 |
|
|---|
| 75 | t.equal(
|
|---|
| 76 | hasAccessibleChild(
|
|---|
| 77 | JSXElementMock('div', [], [
|
|---|
| 78 | JSXExpressionContainerMock({
|
|---|
| 79 | type: 'Identifier',
|
|---|
| 80 | name: 'foo',
|
|---|
| 81 | }),
|
|---|
| 82 | ]),
|
|---|
| 83 | elementType,
|
|---|
| 84 | ),
|
|---|
| 85 | true,
|
|---|
| 86 | 'defined JSXExpressionContainer -> true',
|
|---|
| 87 | );
|
|---|
| 88 |
|
|---|
| 89 | t.equal(
|
|---|
| 90 | hasAccessibleChild(
|
|---|
| 91 | JSXElementMock('div', [], [
|
|---|
| 92 | JSXExpressionContainerMock({
|
|---|
| 93 | type: 'Identifier',
|
|---|
| 94 | name: 'undefined',
|
|---|
| 95 | }),
|
|---|
| 96 | ]),
|
|---|
| 97 | elementType,
|
|---|
| 98 | ),
|
|---|
| 99 | false,
|
|---|
| 100 | 'has children + undefined JSXExpressionContainer -> false',
|
|---|
| 101 | );
|
|---|
| 102 |
|
|---|
| 103 | t.equal(
|
|---|
| 104 | hasAccessibleChild(
|
|---|
| 105 | JSXElementMock('div', [], [{
|
|---|
| 106 | type: 'Unknown',
|
|---|
| 107 | }]),
|
|---|
| 108 | elementType,
|
|---|
| 109 | ),
|
|---|
| 110 | false,
|
|---|
| 111 | 'unknown child type -> false',
|
|---|
| 112 | );
|
|---|
| 113 |
|
|---|
| 114 | t.equal(
|
|---|
| 115 | hasAccessibleChild(
|
|---|
| 116 | JSXElementMock('div', [JSXAttributeMock('children', true)], []),
|
|---|
| 117 | elementType,
|
|---|
| 118 | ),
|
|---|
| 119 | true,
|
|---|
| 120 | 'children passed as a prop -> true',
|
|---|
| 121 | );
|
|---|
| 122 |
|
|---|
| 123 | t.equal(
|
|---|
| 124 | hasAccessibleChild(
|
|---|
| 125 | JSXElementMock('div', [], [
|
|---|
| 126 | JSXElementMock('input', [JSXAttributeMock('type', 'hidden')]),
|
|---|
| 127 | ]),
|
|---|
| 128 | elementType,
|
|---|
| 129 | ),
|
|---|
| 130 | false,
|
|---|
| 131 | 'has chidren -> hidden child input JSXElement -> false',
|
|---|
| 132 | );
|
|---|
| 133 |
|
|---|
| 134 | t.equal(
|
|---|
| 135 | hasAccessibleChild(
|
|---|
| 136 | JSXElementMock('div', [], [
|
|---|
| 137 | JSXElementMock('CustomInput', [JSXAttributeMock('type', 'hidden')]),
|
|---|
| 138 | ]),
|
|---|
| 139 | elementType,
|
|---|
| 140 | ),
|
|---|
| 141 | true,
|
|---|
| 142 | 'has children + custom JSXElement of type hidden -> true',
|
|---|
| 143 | );
|
|---|
| 144 |
|
|---|
| 145 | t.equal(
|
|---|
| 146 | hasAccessibleChild(
|
|---|
| 147 | JSXElementMock('div', [], [
|
|---|
| 148 | JSXElementMock('CustomInput', [JSXAttributeMock('type', 'hidden')]),
|
|---|
| 149 | ]),
|
|---|
| 150 | () => 'input',
|
|---|
| 151 | ),
|
|---|
| 152 | false,
|
|---|
| 153 | 'custom JSXElement mapped to input if type is hidden -> false',
|
|---|
| 154 | );
|
|---|
| 155 |
|
|---|
| 156 | t.end();
|
|---|
| 157 | });
|
|---|