Skip to content

Commit 086c586

Browse files
authored
Merge pull request uswds#230 from uswds/eg/229-provide-check-before-registering-custom-element
Web components: provide check before registering custom element
2 parents a7a491e + ade98db commit 086c586

File tree

5 files changed

+48
-4
lines changed

5 files changed

+48
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
### Patch Changes
66

77
- 3661a08: Web components: TypeScript conversion and build improvements.
8-
- Resolved vite error [#222](https://github.com/uswds/uswds-elements/issues/222#issue-3623280254)
9-
- Provided types export in package [#221](https://github.com/uswds/uswds-elements/issues/221)
8+
- Resolved vite error [#222](https://github.com/uswds/uswds-elements/issues/222#issue-3623280254)
9+
- Provided types export in package [#221](https://github.com/uswds/uswds-elements/issues/221)
1010

1111
## 1.0.0-alpha.3
1212

src/components/usa-banner/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import iconHttps from "@uswds/uswds/img/icon-https.svg";
1010
import iconClose from "../../shared/icons/close.svg";
1111
import iconExpandMore from "../../shared/icons/expand_more.svg";
1212
import iconExpandLess from "../../shared/icons/expand_less.svg";
13+
import { defineCustomElement } from "../../utils";
1314

1415
interface UsaBannerTranslations {
1516
banner: {
@@ -277,4 +278,4 @@ export class UsaBanner extends LitElement {
277278
}
278279
}
279280

280-
customElements.define("usa-banner", UsaBanner);
281+
defineCustomElement("usa-banner", UsaBanner);

src/components/usa-link/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { LitElement, html } from "lit";
22
import styles from "./usa-link.css";
3+
import { defineCustomElement } from "../../utils";
34

45
/**
56
* @summary The usa-link component.
@@ -62,4 +63,4 @@ export class UsaLink extends LitElement {
6263
}
6364
}
6465

65-
customElements.define("usa-link", UsaLink);
66+
defineCustomElement("usa-link", UsaLink);

src/utils/index.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { describe, it, expect, vi, beforeEach } from "vitest";
2+
import { defineCustomElement } from "./index";
3+
4+
describe("defineCustomElement", () => {
5+
let customElementsDefineSpy: ReturnType<typeof vi.spyOn>;
6+
let customElementsGetSpy: ReturnType<typeof vi.spyOn>;
7+
let Component: CustomElementConstructor;
8+
let tagName: string;
9+
10+
beforeEach(() => {
11+
Component = class extends HTMLElement {};
12+
tagName = "usa-test-element";
13+
14+
customElementsDefineSpy = vi.spyOn(customElements, "define");
15+
customElementsGetSpy = vi.spyOn(customElements, "get");
16+
});
17+
18+
it("should define a custom element if it does not already exist", () => {
19+
defineCustomElement(tagName, Component);
20+
21+
expect(customElementsGetSpy).toHaveBeenCalledWith(tagName);
22+
expect(customElementsDefineSpy).toHaveBeenCalledTimes(1);
23+
expect(customElementsDefineSpy).toHaveBeenCalledWith(tagName, Component);
24+
});
25+
26+
it("should not define a custom element if it already exists", () => {
27+
defineCustomElement(tagName, Component);
28+
defineCustomElement(tagName, Component);
29+
30+
expect(customElementsGetSpy).toHaveBeenCalledWith(tagName);
31+
// we still expect this to be called once because of the conditional check in our function
32+
expect(customElementsDefineSpy).toHaveBeenCalledTimes(1);
33+
});
34+
});

src/utils/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const defineCustomElement = (
2+
tag: string,
3+
className: CustomElementConstructor,
4+
) => {
5+
if (!customElements || customElements.get(tag)) return;
6+
7+
customElements.define(tag, className);
8+
};

0 commit comments

Comments
 (0)