Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Damir committed Oct 12, 2022
1 parent 2163943 commit 5ebaee4
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 11 deletions.
12 changes: 3 additions & 9 deletions src/find-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 "/"
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions test/templates/components/form/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<form>My Form</form>
1 change: 1 addition & 0 deletions test/templates/custom/dark/components/label/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<label>My Label</label>
1 change: 1 addition & 0 deletions test/templates/dark/components/label/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<label class="bg-dark text-light">My Dark Label</label>
37 changes: 35 additions & 2 deletions test/test-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<div><x-empty-namespace::button>Submit</x-empty-namespace::button></div>`;

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 = `<div><x-empty-namespace::button>Submit</x-empty-namespace::button></div>`;

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 = `<div><x-unknown-namespace::button>Submit</x-unknown-namespace::button></div>`;

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 = `<div><x-unknown-namespace::button>Submit</x-unknown-namespace::button></div>`;
const expected = `<div><x-unknown-namespace::button>Submit</x-unknown-namespace::button></div>`;

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 = `<div><x-empty-namespace::button>Submit</x-empty-namespace::button></div>`;
const expected = `<div><x-empty-namespace::button>Submit</x-empty-namespace::button></div>`;

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 = `<div><x-button>Submit</x-button></div>`;
const expected = `<div><x-button>Submit</x-button></div>`;

const html = await posthtml([plugin({root: './test/templates/empty-root', strict: false})]).process(actual).then(result => clean(result.html));

t.is(html, expected);
});
27 changes: 27 additions & 0 deletions test/test-x-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<x-dark::label></x-dark::label>`;
const expected = `<label class="bg-dark text-light">My Dark Label</label>`;

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 = `<x-dark::modal></x-dark::modal>`;
const expected = `<div>Modal</div>`;
Expand All @@ -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 = `<x-dark::form></x-dark::form>`;
const expected = `<form>My Form</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 = `<x-dark::button><slot name="content">My button</slot></x-dark::button>`;
const expected = `<button class="bg-dark-custom text-light-custom">My button</button>`;
Expand All @@ -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 = `<x-dark::label></x-dark::label>`;
const expected = `<label>My Label</label>`;

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

0 comments on commit 5ebaee4

Please sign in to comment.