Skip to content

Commit 5ebaee4

Browse files
author
Damir
committed
More tests
1 parent 2163943 commit 5ebaee4

File tree

6 files changed

+68
-11
lines changed

6 files changed

+68
-11
lines changed

src/find-path.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,7 @@ const folderSeparator = '.';
1212
* @param {Object} options [posthtml options]
1313
* @return {String|boolean}
1414
*/
15-
function findPathFromTagName(node, options) {
16-
if (!node.attrs) {
17-
node.attrs = {};
18-
}
19-
20-
const {tag} = node;
21-
15+
function findPathFromTagName({tag}, options) {
2216
// Get module filename from tag name
2317
// remove prefix "x-"
2418
// replace dot "." with slash "/"
@@ -29,8 +23,8 @@ function findPathFromTagName(node, options) {
2923
.join(path.sep)
3024
.concat(folderSeparator, options.fileExtension);
3125

32-
// Find module by defined namespace in options.namespaces
33-
// or by defined roots in options.roots
26+
// Find module by defined namespace in options.namespaces when tag has '::'
27+
// otherwise by defined roots in options.roots
3428
return tag.includes(options.namespaceSeparator) ?
3529
findPathByNamespace(tag, fileNameFromTag.split(options.namespaceSeparator), options) :
3630
findPathByRoot(tag, fileNameFromTag, options);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<form>My Form</form>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<label>My Label</label>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<label class="bg-dark text-light">My Dark Label</label>

test/test-errors.js

+35-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,44 @@ const clean = html => html.replace(/(\n|\t)/g, '').trim();
88
test('Must fail when namespace path is not found without fallback root', async t => {
99
const actual = `<div><x-empty-namespace::button>Submit</x-empty-namespace::button></div>`;
1010

11-
await t.throwsAsync(async () => posthtml([plugin({root: './test/templates', namespaces: [{name: 'empty-namespace', root: './test/templates/empty-namespace'}]})]).process(actual).then(result => clean(result.html)));
11+
await t.throwsAsync(async () => posthtml([plugin({root: './test/templates', namespaceFallback: false, namespaces: [{name: 'empty-namespace', root: './test/templates/empty-namespace'}]})]).process(actual).then(result => clean(result.html)));
1212
});
1313

1414
test('Must fail when namespace path is not found with fallback root', async t => {
1515
const actual = `<div><x-empty-namespace::button>Submit</x-empty-namespace::button></div>`;
1616

17-
await t.throwsAsync(async () => posthtml([plugin({root: './test/templates', namespaces: [{name: 'empty-namespace', root: './test/templates/empty-namespace'}]})]).process(actual).then(result => clean(result.html)));
17+
await t.throwsAsync(async () => posthtml([plugin({root: './test/templates', namespaceFallback: true, namespaces: [{name: 'empty-namespace', root: './test/templates/empty-namespace'}]})]).process(actual).then(result => clean(result.html)));
18+
});
19+
20+
test('Must fail when namespace is unknown', async t => {
21+
const actual = `<div><x-unknown-namespace::button>Submit</x-unknown-namespace::button></div>`;
22+
23+
await t.throwsAsync(async () => posthtml([plugin({root: './test/templates'})]).process(actual).then(result => clean(result.html)));
24+
});
25+
26+
test('Must return node as-is when namespace is unknown with strict mode disabled', async t => {
27+
const actual = `<div><x-unknown-namespace::button>Submit</x-unknown-namespace::button></div>`;
28+
const expected = `<div><x-unknown-namespace::button>Submit</x-unknown-namespace::button></div>`;
29+
30+
const html = await posthtml([plugin({root: './test/templates', strict: false})]).process(actual).then(result => clean(result.html));
31+
32+
t.is(html, expected);
33+
});
34+
35+
test('Must return node as-is when namespace is empty with strict mode disabled', async t => {
36+
const actual = `<div><x-empty-namespace::button>Submit</x-empty-namespace::button></div>`;
37+
const expected = `<div><x-empty-namespace::button>Submit</x-empty-namespace::button></div>`;
38+
39+
const html = await posthtml([plugin({root: './test/templates', strict: false, namespaces: [{name: 'empty-namespace', root: './test/templates/empty-namespace'}]})]).process(actual).then(result => clean(result.html));
40+
41+
t.is(html, expected);
42+
});
43+
44+
test('Must return node as-is when all defined roots are empty with strict mode disabled', async t => {
45+
const actual = `<div><x-button>Submit</x-button></div>`;
46+
const expected = `<div><x-button>Submit</x-button></div>`;
47+
48+
const html = await posthtml([plugin({root: './test/templates/empty-root', strict: false})]).process(actual).then(result => clean(result.html));
49+
50+
t.is(html, expected);
1851
});

test/test-x-tag.js

+27
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ test('Must process component with namespace to html', async t => {
3232
t.is(html, expected);
3333
});
3434

35+
test('Must process component with namespace to html using index file', async t => {
36+
const actual = `<x-dark::label></x-dark::label>`;
37+
const expected = `<label class="bg-dark text-light">My Dark Label</label>`;
38+
39+
const html = await posthtml([plugin({root: './test/templates', namespaces: [{name: 'dark', root: './test/templates/dark/components'}]})]).process(actual).then(result => clean(result.html));
40+
41+
t.is(html, expected);
42+
});
43+
3544
test(`Must process component with namespace's fallback path`, async t => {
3645
const actual = `<x-dark::modal></x-dark::modal>`;
3746
const expected = `<div>Modal</div>`;
@@ -41,6 +50,15 @@ test(`Must process component with namespace's fallback path`, async t => {
4150
t.is(html, expected);
4251
});
4352

53+
test(`Must process component with namespace's fallback path using index file`, async t => {
54+
const actual = `<x-dark::form></x-dark::form>`;
55+
const expected = `<form>My Form</form>`;
56+
57+
const html = await posthtml([plugin({root: './test/templates', namespaces: [{name: 'dark', root: './test/templates/dark/components', fallback: './test/templates/components'}]})]).process(actual).then(result => clean(result.html));
58+
59+
t.is(html, expected);
60+
});
61+
4462
test(`Must process component with namespace's custom path`, async t => {
4563
const actual = `<x-dark::button><slot name="content">My button</slot></x-dark::button>`;
4664
const expected = `<button class="bg-dark-custom text-light-custom">My button</button>`;
@@ -49,3 +67,12 @@ test(`Must process component with namespace's custom path`, async t => {
4967

5068
t.is(html, expected);
5169
});
70+
71+
test(`Must process component with namespace's custom path using index file`, async t => {
72+
const actual = `<x-dark::label></x-dark::label>`;
73+
const expected = `<label>My Label</label>`;
74+
75+
const html = await posthtml([plugin({root: './test/templates', namespaces: [{name: 'dark', root: './test/templates/dark/components', custom: './test/templates/custom/dark/components'}]})]).process(actual).then(result => clean(result.html));
76+
77+
t.is(html, expected);
78+
});

0 commit comments

Comments
 (0)