mirror of
https://github.com/jlengrand/open-wc.git
synced 2026-03-10 08:31:19 +00:00
feat(scoped-elements): add getScopedTagName function
This commit is contained in:
committed by
Lars den Bakker
parent
253816d51c
commit
21132e00f7
@@ -1,2 +1,2 @@
|
||||
export { ScopedElementsMixin } from './src/ScopedElementsMixin.js';
|
||||
export { createScopedElement } from './src/createScopedElement.js';
|
||||
export { getScopedTagName } from './src/getScopedTagName.js';
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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().",
|
||||
);
|
||||
});
|
||||
});
|
||||
22
packages/scoped-elements/test/getScopedTagName.test.js
Normal file
22
packages/scoped-elements/test/getScopedTagName.test.js
Normal 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().",
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user