|
| 1 | +import test from 'tape'; |
| 2 | + |
| 3 | +import getAttributes from '../../../src/util/getAttributes'; |
| 4 | +import JSXElementMock from '../../../__mocks__/JSXElementMock'; |
| 5 | +import JSXAttributeMock from '../../../__mocks__/JSXAttributeMock'; |
| 6 | + |
| 7 | +const componentsSettings = { |
| 8 | + 'jsx-a11y': { |
| 9 | + components: { |
| 10 | + Button: 'button', |
| 11 | + MyInput: { |
| 12 | + component: 'input', |
| 13 | + attributes: { |
| 14 | + type: ['myType'], |
| 15 | + }, |
| 16 | + }, |
| 17 | + Link: { |
| 18 | + component: 'a', |
| 19 | + attributes: { |
| 20 | + href: ['to', 'href'], |
| 21 | + onClick: ['onClick', 'handleClick'], |
| 22 | + }, |
| 23 | + }, |
| 24 | + }, |
| 25 | + }, |
| 26 | +}; |
| 27 | + |
| 28 | +test('getAttributes', (t) => { |
| 29 | + t.test('no settings in context', (st) => { |
| 30 | + st.deepEqual( |
| 31 | + getAttributes( |
| 32 | + JSXElementMock('input').openingElement, |
| 33 | + 'input', |
| 34 | + {}, |
| 35 | + ), |
| 36 | + [], |
| 37 | + 'returns no attributes', |
| 38 | + ); |
| 39 | + |
| 40 | + st.deepEqual( |
| 41 | + getAttributes( |
| 42 | + JSXElementMock('input', [JSXAttributeMock('type', 'text')]).openingElement, |
| 43 | + 'input', |
| 44 | + {}, |
| 45 | + ), |
| 46 | + [JSXAttributeMock('type', 'text')], |
| 47 | + 'returns raw attributes if no settings are provided', |
| 48 | + ); |
| 49 | + |
| 50 | + st.end(); |
| 51 | + }); |
| 52 | + |
| 53 | + t.test('components settings in context', (st) => { |
| 54 | + st.deepEqual( |
| 55 | + getAttributes( |
| 56 | + JSXElementMock('MyInput', [JSXAttributeMock('type', 'text')]).openingElement, |
| 57 | + 'input', |
| 58 | + { settings: componentsSettings }, |
| 59 | + ), |
| 60 | + [JSXAttributeMock('myType', 'text')], |
| 61 | + 'should expect a custom attribute name mapping to `myType`', |
| 62 | + ); |
| 63 | + |
| 64 | + st.deepEqual( |
| 65 | + getAttributes( |
| 66 | + JSXElementMock('input', [JSXAttributeMock('type', 'text')]).openingElement, |
| 67 | + 'input', |
| 68 | + { settings: componentsSettings }, |
| 69 | + ), |
| 70 | + [JSXAttributeMock('type', 'text')], |
| 71 | + 'should not expect a custom attribute name when native element is used', |
| 72 | + ); |
| 73 | + |
| 74 | + st.deepEqual( |
| 75 | + getAttributes( |
| 76 | + JSXElementMock('Button', [JSXAttributeMock('type', 'text')]).openingElement, |
| 77 | + 'button', |
| 78 | + { settings: componentsSettings }, |
| 79 | + ), |
| 80 | + [JSXAttributeMock('type', 'text')], |
| 81 | + 'should not expect a custom attribute name when no custom attributes configured', |
| 82 | + ); |
| 83 | + |
| 84 | + st.deepEqual( |
| 85 | + getAttributes( |
| 86 | + JSXElementMock('Link', [JSXAttributeMock('href', '/path'), JSXAttributeMock('onClick', 'handler')]).openingElement, |
| 87 | + 'a', |
| 88 | + { settings: componentsSettings }, |
| 89 | + ), |
| 90 | + [JSXAttributeMock('to', '/path'), JSXAttributeMock('href', '/path'), JSXAttributeMock('onClick', 'handler'), JSXAttributeMock('handleClick', 'handler')], |
| 91 | + 'should map multiple attributes according to configuration', |
| 92 | + ); |
| 93 | + |
| 94 | + st.end(); |
| 95 | + }); |
| 96 | + |
| 97 | + t.end(); |
| 98 | +}); |
0 commit comments