|
1 | | -import { test, expect } from 'vitest' |
| 1 | +import { test, expect } from 'vitest'; |
| 2 | +import { process } from './process.js'; |
| 3 | + |
2 | 4 | import plugin from '../src/index.js'; |
3 | 5 | import posthtml from 'posthtml'; |
4 | 6 | import pull from 'lodash/pull'; |
5 | 7 |
|
6 | 8 | const clean = html => html.replace(/(\n|\t)/g, '').trim(); |
7 | 9 |
|
8 | | -test('Must merge and map attributes not props to first node', async () => { |
| 10 | +test('Maps and merges attributes (not props) to first node', async () => { |
9 | 11 | const actual = `<component src="components/component-mapped-attributes.html" data-something="Test an attribute" title="My Title" class="bg-light p-2" style="display: flex; font-size: 20px"></component>`; |
10 | 12 | const expected = `<div class="text-dark m-3 bg-light p-2" style="background: red; display: flex; font-size: 20px" data-something="Test an attribute">My Title Default body</div>`; |
11 | 13 |
|
12 | | - const html = await posthtml([plugin({root: './test/templates', tag: 'component'})]).process(actual).then(result => clean(result.html)); |
13 | | - |
14 | | - expect(html).toBe(expected); |
| 14 | + process(actual, |
| 15 | + { |
| 16 | + root: './test/templates', |
| 17 | + tag: 'component' |
| 18 | + } |
| 19 | + ) |
| 20 | + .then(html => { |
| 21 | + expect(html).toBe(expected); |
| 22 | + }); |
15 | 23 | }); |
16 | 24 |
|
17 | | -test('Must merge and map attributes not props to node with attribute "attributes" without style', async () => { |
| 25 | +test('Maps and merges attributes (not props) to node with attribute `attributes` without style', async () => { |
18 | 26 | const actual = `<component src="components/component-mapped-attributes2.html" title="My Title" class="bg-light p-2" style="display: flex; font-size: 20px"></component>`; |
19 | 27 | const expected = `<div class="mapped"><div class="text-dark m-3 bg-light p-2" style="display: flex; font-size: 20px">My Title Default body</div></div>`; |
20 | 28 |
|
21 | | - const html = await posthtml([plugin({root: './test/templates', tag: 'component'})]).process(actual).then(result => clean(result.html)); |
22 | | - |
23 | | - expect(html).toBe(expected); |
| 29 | + process(actual, |
| 30 | + { |
| 31 | + root: './test/templates', |
| 32 | + tag: 'component' |
| 33 | + } |
| 34 | + ) |
| 35 | + .then(html => { |
| 36 | + expect(html).toBe(expected); |
| 37 | + }); |
24 | 38 | }); |
25 | 39 |
|
26 | | -test('Must merge and map attributes not props to node with attribute "attributes" without class', async () => { |
| 40 | +test('Maps and merges attributes (not props) to node with attribute `attributes` without class', async () => { |
27 | 41 | const actual = `<component src="components/component-mapped-attributes3.html" title="My Title" class="bg-light p-2" style="display: block; font-size: 20px"></component>`; |
28 | 42 | const expected = `<div class="mapped"><div style="border: 1px solid black; display: block; font-size: 20px" class="bg-light p-2">My Title Default body</div></div>`; |
29 | 43 |
|
30 | | - const html = await posthtml([plugin({root: './test/templates', tag: 'component'})]).process(actual).then(result => clean(result.html)); |
31 | | - |
32 | | - expect(html).toBe(expected); |
| 44 | + process(actual, |
| 45 | + { |
| 46 | + root: './test/templates', |
| 47 | + tag: 'component' |
| 48 | + } |
| 49 | + ) |
| 50 | + .then(html => { |
| 51 | + expect(html).toBe(expected); |
| 52 | + }); |
33 | 53 | }); |
34 | 54 |
|
35 | | -test('Must not map attributes for component without any elements', async () => { |
| 55 | +test('Does not map attributes for components that have no elements', async () => { |
36 | 56 | const actual = `<div><component src="components/component-without-elements.html" title="My Title" class="bg-light p-2" style="display: flex; font-size: 20px"></component></div>`; |
37 | 57 | const expected = `<div>I am a component without any elements. Yeah, something uncommon but just for testing. My Title</div>`; |
38 | 58 |
|
39 | | - const html = await posthtml([plugin({root: './test/templates', tag: 'component'})]).process(actual).then(result => clean(result.html)); |
40 | | - |
41 | | - expect(html).toBe(expected); |
| 59 | + process(actual, |
| 60 | + { |
| 61 | + root: './test/templates', |
| 62 | + tag: 'component' |
| 63 | + } |
| 64 | + ) |
| 65 | + .then(html => { |
| 66 | + expect(html).toBe(expected); |
| 67 | + }); |
42 | 68 | }); |
43 | 69 |
|
44 | | -test('Must override class and style attributes', async () => { |
| 70 | +test('Overrides class and style attributes', async () => { |
45 | 71 | const actual = `<component src="components/component-mapped-attributes.html" override:class="override-class" override:style="background: black"></component>`; |
46 | 72 | const expected = `<div class="override-class" style="background: black">Default title Default body</div>`; |
47 | 73 |
|
48 | | - const html = await posthtml([plugin({root: './test/templates', tag: 'component'})]).process(actual).then(result => clean(result.html)); |
49 | | - |
50 | | - expect(html).toBe(expected); |
| 74 | + process(actual, |
| 75 | + { |
| 76 | + root: './test/templates', |
| 77 | + tag: 'component' |
| 78 | + } |
| 79 | + ) |
| 80 | + .then(html => { |
| 81 | + expect(html).toBe(expected); |
| 82 | + }); |
51 | 83 | }); |
52 | 84 |
|
53 | | -test('Must remove an attributes that has "undefined" or "null" value', async () => { |
| 85 | +test('Removes attributes containing `undefined` or `null` values', async () => { |
54 | 86 | const actual = `<component src="components/remove-attributes.html" tabindex="-1">My button</component>`; |
55 | 87 | const expected = `<button class="btn btn-primary" data-bs-dismiss="true" data-bs-backdrop="false" tabindex="-1">My button</button><div>works also in all nodes</div>`; |
56 | 88 |
|
57 | | - const html = await posthtml([plugin({root: './test/templates', tag: 'component'})]).process(actual).then(result => clean(result.html)); |
58 | | - |
59 | | - expect(html).toBe(expected); |
| 89 | + process(actual, |
| 90 | + { |
| 91 | + root: './test/templates', |
| 92 | + tag: 'component' |
| 93 | + } |
| 94 | + ) |
| 95 | + .then(html => { |
| 96 | + expect(html).toBe(expected); |
| 97 | + }); |
60 | 98 | }); |
61 | 99 |
|
62 | | -test('Must override valid attributes', async () => { |
| 100 | +test('Overrides known attributes', async () => { |
63 | 101 | const actual = `<component src="components/override-attributes.html" tabindex="-1" title="My button" custom-attribute="A custom attribute">My button</component>`; |
64 | 102 | const expected = `<button tabindex="-1" custom-attribute="A custom attribute">My button</button>`; |
65 | 103 |
|
66 | | - const html = await posthtml([plugin({ |
67 | | - root: './test/templates', |
68 | | - tag: 'component', |
69 | | - elementAttributes: { |
70 | | - BUTTON: attrs => { |
71 | | - // Remove title |
72 | | - pull(attrs, 'title'); |
| 104 | + process(actual, |
| 105 | + { |
| 106 | + root: './test/templates', |
| 107 | + tag: 'component', |
| 108 | + elementAttributes: { |
| 109 | + BUTTON: attrs => { |
| 110 | + // Remove title |
| 111 | + pull(attrs, 'title'); |
73 | 112 |
|
74 | | - // Add custom-attribute |
75 | | - attrs.push('custom-attribute'); |
| 113 | + // Add custom-attribute |
| 114 | + attrs.push('custom-attribute'); |
76 | 115 |
|
77 | | - return attrs; |
| 116 | + return attrs; |
| 117 | + } |
78 | 118 | } |
79 | 119 | } |
80 | | - })]).process(actual).then(result => clean(result.html)); |
81 | | - |
82 | | - expect(html).toBe(expected); |
| 120 | + ) |
| 121 | + .then(html => { |
| 122 | + expect(html).toBe(expected); |
| 123 | + }); |
83 | 124 | }); |
84 | 125 |
|
85 | | -test('Must work with tag without attributes', async () => { |
| 126 | +test('Works with tags that have no attributes', async () => { |
86 | 127 | const actual = `<component src="components/override-attributes.html" tabindex="-1" title="My button" custom-attribute="A custom attribute">My button</component>`; |
87 | 128 | const expected = `<button>My button</button>`; |
88 | 129 |
|
89 | | - const html = await posthtml([plugin({ |
90 | | - root: './test/templates', |
91 | | - tag: 'component', |
92 | | - elementAttributes: { |
93 | | - BUTTON: _ => { |
94 | | - // Return empty array means no attributes |
95 | | - return []; |
| 130 | + process(actual, |
| 131 | + { |
| 132 | + root: './test/templates', |
| 133 | + tag: 'component', |
| 134 | + elementAttributes: { |
| 135 | + BUTTON: _ => { |
| 136 | + // Return empty array means no attributes |
| 137 | + return []; |
| 138 | + } |
96 | 139 | } |
97 | 140 | } |
98 | | - })]).process(actual).then(result => clean(result.html)); |
99 | | - |
100 | | - expect(html).toBe(expected); |
| 141 | + ) |
| 142 | + .then(html => { |
| 143 | + expect(html).toBe(expected); |
| 144 | + }); |
101 | 145 | }); |
102 | 146 |
|
103 | | -test('Must use safelist and blocklist', async () => { |
| 147 | +test('Uses `safelist` and `blocklist`', async () => { |
104 | 148 | const actual = `<component src="components/override-attributes.html" tabindex="-1" title="My button" custom-attribute="A custom attribute">My button</component>`; |
105 | 149 | const expected = `<button custom-attribute="A custom attribute">My button</button>`; |
106 | 150 |
|
107 | | - const html = await posthtml([plugin({ |
108 | | - root: './test/templates', |
109 | | - tag: 'component', |
110 | | - safelistAttributes: ['custom-attribute'], |
111 | | - blocklistAttributes: ['role', 'tabindex'] |
112 | | - })]).process(actual).then(result => clean(result.html)); |
113 | | - |
114 | | - expect(html).toBe(expected); |
| 151 | + process(actual, |
| 152 | + { |
| 153 | + root: './test/templates', |
| 154 | + tag: 'component', |
| 155 | + safelistAttributes: ['custom-attribute'], |
| 156 | + blocklistAttributes: ['role', 'tabindex'], |
| 157 | + } |
| 158 | + ) |
| 159 | + .then(html => { |
| 160 | + expect(html).toBe(expected); |
| 161 | + }); |
115 | 162 | }); |
0 commit comments