Skip to content

Commit e657663

Browse files
committed
Move default module tests
Moved tests to CJS and ESM integration tests to quickly validate if CJS and ESM are functional.
1 parent e462cdd commit e657663

File tree

3 files changed

+144
-92
lines changed

3 files changed

+144
-92
lines changed

tests/functional/default.js

Lines changed: 0 additions & 68 deletions
This file was deleted.

tests/integration/cjs/index.js

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
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');
39
const { filterValue: filterValueA } = require('@mcaskill/html-build-attributes/lib');
410
const { filterValue: filterValueB } = require('@mcaskill/html-build-attributes/lib/filter');
511
const { filterValue: filterValueC } = require('@mcaskill/html-build-attributes/lib/filter/filter-value.js');
@@ -12,13 +18,67 @@ if (
1218
throw new Error('filterValue function not found');
1319
}
1420

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();

tests/integration/esm/index.js

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
// import HTMLBuildAttributes from '@mcaskill/html-build-attributes';
2-
import { composeAttributes } from '@mcaskill/html-build-attributes';
1+
import { test } from 'uvu';
2+
import * as assert from 'uvu/assert';
3+
import {
4+
composeAttribute,
5+
composeAttributes,
6+
escapeAttributeValue,
7+
filterAttributeValue,
8+
} from '@mcaskill/html-build-attributes';
39
import { filterValue as filterValueA } from '@mcaskill/html-build-attributes/lib';
410
import { filterValue as filterValueB } from '@mcaskill/html-build-attributes/lib/filter';
511
import { filterValue as filterValueC } from '@mcaskill/html-build-attributes/lib/filter/filter-value.js';
@@ -12,13 +18,67 @@ if (
1218
throw new Error('filterValue function not found');
1319
}
1420

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

Comments
 (0)