|
| 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 | +}); |
0 commit comments