From 5ebaee4a1d9213252149025726ade7d2cfbc3e67 Mon Sep 17 00:00:00 2001 From: Damir Date: Wed, 12 Oct 2022 04:22:12 +0200 Subject: [PATCH] More tests --- src/find-path.js | 12 ++---- test/templates/components/form/index.html | 1 + .../custom/dark/components/label/index.html | 1 + .../dark/components/label/index.html | 1 + test/test-errors.js | 37 ++++++++++++++++++- test/test-x-tag.js | 27 ++++++++++++++ 6 files changed, 68 insertions(+), 11 deletions(-) create mode 100644 test/templates/components/form/index.html create mode 100644 test/templates/custom/dark/components/label/index.html create mode 100644 test/templates/dark/components/label/index.html diff --git a/src/find-path.js b/src/find-path.js index 3024c11..0a77fc5 100644 --- a/src/find-path.js +++ b/src/find-path.js @@ -12,13 +12,7 @@ const folderSeparator = '.'; * @param {Object} options [posthtml options] * @return {String|boolean} */ -function findPathFromTagName(node, options) { - if (!node.attrs) { - node.attrs = {}; - } - - const {tag} = node; - +function findPathFromTagName({tag}, options) { // Get module filename from tag name // remove prefix "x-" // replace dot "." with slash "/" @@ -29,8 +23,8 @@ function findPathFromTagName(node, options) { .join(path.sep) .concat(folderSeparator, options.fileExtension); - // Find module by defined namespace in options.namespaces - // or by defined roots in options.roots + // Find module by defined namespace in options.namespaces when tag has '::' + // otherwise by defined roots in options.roots return tag.includes(options.namespaceSeparator) ? findPathByNamespace(tag, fileNameFromTag.split(options.namespaceSeparator), options) : findPathByRoot(tag, fileNameFromTag, options); diff --git a/test/templates/components/form/index.html b/test/templates/components/form/index.html new file mode 100644 index 0000000..7505f0b --- /dev/null +++ b/test/templates/components/form/index.html @@ -0,0 +1 @@ +
My Form
diff --git a/test/templates/custom/dark/components/label/index.html b/test/templates/custom/dark/components/label/index.html new file mode 100644 index 0000000..cbe84b0 --- /dev/null +++ b/test/templates/custom/dark/components/label/index.html @@ -0,0 +1 @@ + diff --git a/test/templates/dark/components/label/index.html b/test/templates/dark/components/label/index.html new file mode 100644 index 0000000..ab70583 --- /dev/null +++ b/test/templates/dark/components/label/index.html @@ -0,0 +1 @@ + diff --git a/test/test-errors.js b/test/test-errors.js index f634f95..eb0083d 100644 --- a/test/test-errors.js +++ b/test/test-errors.js @@ -8,11 +8,44 @@ const clean = html => html.replace(/(\n|\t)/g, '').trim(); test('Must fail when namespace path is not found without fallback root', async t => { const actual = `
Submit
`; - 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))); + 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))); }); test('Must fail when namespace path is not found with fallback root', async t => { const actual = `
Submit
`; - 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))); + 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))); +}); + +test('Must fail when namespace is unknown', async t => { + const actual = `
Submit
`; + + await t.throwsAsync(async () => posthtml([plugin({root: './test/templates'})]).process(actual).then(result => clean(result.html))); +}); + +test('Must return node as-is when namespace is unknown with strict mode disabled', async t => { + const actual = `
Submit
`; + const expected = `
Submit
`; + + const html = await posthtml([plugin({root: './test/templates', strict: false})]).process(actual).then(result => clean(result.html)); + + t.is(html, expected); +}); + +test('Must return node as-is when namespace is empty with strict mode disabled', async t => { + const actual = `
Submit
`; + const expected = `
Submit
`; + + 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)); + + t.is(html, expected); +}); + +test('Must return node as-is when all defined roots are empty with strict mode disabled', async t => { + const actual = `
Submit
`; + const expected = `
Submit
`; + + const html = await posthtml([plugin({root: './test/templates/empty-root', strict: false})]).process(actual).then(result => clean(result.html)); + + t.is(html, expected); }); diff --git a/test/test-x-tag.js b/test/test-x-tag.js index d94e18d..8e7853a 100644 --- a/test/test-x-tag.js +++ b/test/test-x-tag.js @@ -32,6 +32,15 @@ test('Must process component with namespace to html', async t => { t.is(html, expected); }); +test('Must process component with namespace to html using index file', async t => { + const actual = ``; + const expected = ``; + + const html = await posthtml([plugin({root: './test/templates', namespaces: [{name: 'dark', root: './test/templates/dark/components'}]})]).process(actual).then(result => clean(result.html)); + + t.is(html, expected); +}); + test(`Must process component with namespace's fallback path`, async t => { const actual = ``; const expected = `
Modal
`; @@ -41,6 +50,15 @@ test(`Must process component with namespace's fallback path`, async t => { t.is(html, expected); }); +test(`Must process component with namespace's fallback path using index file`, async t => { + const actual = ``; + const expected = `
My Form
`; + + 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)); + + t.is(html, expected); +}); + test(`Must process component with namespace's custom path`, async t => { const actual = `My button`; const expected = ``; @@ -49,3 +67,12 @@ test(`Must process component with namespace's custom path`, async t => { t.is(html, expected); }); + +test(`Must process component with namespace's custom path using index file`, async t => { + const actual = ``; + const expected = ``; + + 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)); + + t.is(html, expected); +});