feat(scoped-elements): add getScopedTagName function

This commit is contained in:
Manuel Martin Prieto
2020-02-24 17:16:46 +01:00
committed by Lars den Bakker
parent 253816d51c
commit 21132e00f7
4 changed files with 27 additions and 28 deletions

View File

@@ -1,2 +1,2 @@
export { ScopedElementsMixin } from './src/ScopedElementsMixin.js';
export { createScopedElement } from './src/createScopedElement.js';
export { getScopedTagName } from './src/getScopedTagName.js';

View File

@@ -1,14 +1,14 @@
import { registerElement } from './registerElement.js';
/**
* Creates a scoped element
* Gets the scoped tag name
*
* @param {string} tagName
* @param {Object.<string, typeof HTMLElement>} scopedElements
* @throws Will throw an error if the tag name is unknown
* @returns {HTMLElement}
* @returns {string}
*/
export function createScopedElement(tagName, scopedElements = {}) {
export function getScopedTagName(tagName, scopedElements = {}) {
const klass = scopedElements[tagName];
if (!klass) {
@@ -17,5 +17,5 @@ export function createScopedElement(tagName, scopedElements = {}) {
);
}
return document.createElement(registerElement(tagName, klass));
return registerElement(tagName, klass);
}

View File

@@ -1,23 +0,0 @@
import { expect } from '@open-wc/testing';
import { createScopedElement } from '../src/createScopedElement.js';
describe('createScopedElement', () => {
it('should create the scoped HTML element specified by the tag name', () => {
class FeatureA extends HTMLElement {}
const scopedElements = {
'feature-a': FeatureA,
};
const el = createScopedElement('feature-a', scopedElements);
expect(el).to.be.an.instanceOf(FeatureA);
expect(el.tagName.toLowerCase()).to.match(new RegExp(`feature-a-\\d{1,5}`));
});
it("should throw an error if tagName isn't recognized", () => {
expect(() => createScopedElement('feature-a', {})).to.throw(
"The tag 'feature-a' is not a registered scoped element. Make sure you add it via static get scopedElements().",
);
});
});

View File

@@ -0,0 +1,22 @@
import { expect } from '@open-wc/testing';
import { getScopedTagName } from '../src/getScopedTagName.js';
describe('getScopedTagName', () => {
it('should return the scoped tag name used to define the HTML Element specified by the user tag name', () => {
class FeatureA extends HTMLElement {}
const scopedElements = {
'feature-a': FeatureA,
};
const scopedTagName = getScopedTagName('feature-a', scopedElements);
expect(scopedTagName).to.match(new RegExp(`feature-a-\\d{1,5}`));
});
it("should throw an error if tagName isn't recognized", () => {
expect(() => getScopedTagName('feature-a', {})).to.throw(
"The tag 'feature-a' is not a registered scoped element. Make sure you add it via static get scopedElements().",
);
});
});