|
1 | | -// const HTMLBuildAttributes = require('@mcaskill/html-build-attributes'); |
2 | | -const { composeAttributes } = require('@mcaskill/html-build-attributes'); |
| 1 | +const { test } = require('uvu'); |
| 2 | +const assert = require('uvu/assert'); |
| 3 | +const { |
| 4 | + composeAttribute, |
| 5 | + composeAttributes, |
| 6 | + escapeAttributeValue, |
| 7 | + filterAttributeValue, |
| 8 | +} = require('@mcaskill/html-build-attributes'); |
3 | 9 | const { filterValue: filterValueA } = require('@mcaskill/html-build-attributes/lib'); |
4 | 10 | const { filterValue: filterValueB } = require('@mcaskill/html-build-attributes/lib/filter'); |
5 | 11 | const { filterValue: filterValueC } = require('@mcaskill/html-build-attributes/lib/filter/filter-value.js'); |
|
12 | 18 | throw new Error('filterValue function not found'); |
13 | 19 | } |
14 | 20 |
|
15 | | -if ('lang="en" dir="ltr" class="has-no-js test"' !== composeAttributes({ |
16 | | - lang: 'en', |
17 | | - dir: 'ltr', |
18 | | - class: [ |
19 | | - 'has-no-js', |
20 | | - 'test', |
21 | | - ], |
22 | | -})) { |
23 | | - throw new Error('filterValue function not found'); |
24 | | -} |
| 21 | +const now = new Date(); |
| 22 | + |
| 23 | +const input = { |
| 24 | + 'type': 'file', |
| 25 | + 'id': 'avatar', |
| 26 | + 'name': 'avatar', |
| 27 | + 'class': [ 'form-control', 'form-control-sm' ], |
| 28 | + 'multiple': true, |
| 29 | + 'disabled': false, |
| 30 | + 'accept': [ 'image/png', 'image/jpeg' ], |
| 31 | + 'data-type': null, |
| 32 | + 'data-max-files': 3, |
| 33 | + 'data-datetime': now, |
| 34 | + 'data-array': [ true, false, null ], |
| 35 | + 'data-options': { |
| 36 | + a: 1, |
| 37 | + b: 0, |
| 38 | + c: null, |
| 39 | + d: true, |
| 40 | + e: false, |
| 41 | + f: [ 1, 2, 3 ], |
| 42 | + }, |
| 43 | +}; |
| 44 | + |
| 45 | +const expected = { |
| 46 | + 'type': 'type="file"', |
| 47 | + 'id': 'id="avatar"', |
| 48 | + 'name': 'name="avatar"', |
| 49 | + 'class': 'class="form-control form-control-sm"', |
| 50 | + 'multiple': 'multiple', |
| 51 | + 'disabled': null, |
| 52 | + 'accept': 'accept="image/png,image/jpeg"', |
| 53 | + 'data-type': null, |
| 54 | + 'data-max-files': 'data-max-files="3"', |
| 55 | + 'data-datetime': `data-datetime="${now.toISOString()}"`, |
| 56 | + 'data-array': `data-array="[true,false,null]"`, |
| 57 | + 'data-options': 'data-options="{"a":1,"b":0,"c":null,"d":true,"e":false,"f":[1,2,3]}"', |
| 58 | +}; |
| 59 | + |
| 60 | +test('should be composable, filterable, and escapable', () => { |
| 61 | + assert.type(composeAttribute, 'function'); |
| 62 | + assert.type(composeAttributes, 'function'); |
| 63 | + assert.type(escapeAttributeValue, 'function'); |
| 64 | + assert.type(filterAttributeValue, 'function'); |
| 65 | +}); |
| 66 | + |
| 67 | +test('should render each attribute', () => { |
| 68 | + for (const [ name, value ] of Object.entries(input)) { |
| 69 | + const output = composeAttribute(name, value); |
| 70 | + |
| 71 | + assert.is(output, expected[name]); |
| 72 | + } |
| 73 | +}); |
| 74 | + |
| 75 | +test('should render set of attributes', () => { |
| 76 | + const output = composeAttributes(input); |
| 77 | + |
| 78 | + assert.is( |
| 79 | + output, |
| 80 | + Object.values(expected).filter((attr) => attr !== null).join(' ') |
| 81 | + ); |
| 82 | +}); |
| 83 | + |
| 84 | +test.run(); |
0 commit comments