-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for default init in shadow dom
- Loading branch information
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { beforeEach, describe, expect, it } from "vitest"; | ||
|
||
import "./index.js"; | ||
|
||
function getInsideInput() { | ||
return document.body.querySelector("usa-text-input").shadowRoot; | ||
} | ||
|
||
function getLabelElement() { | ||
return getInsideInput().querySelector("label"); | ||
} | ||
|
||
function getLabelContext() { | ||
return getLabelElement().getAttribute("for"); | ||
} | ||
|
||
function getInputElement() { | ||
return getInsideInput().querySelector("input"); | ||
} | ||
|
||
describe("usa-text-input component", async () => { | ||
beforeEach(async () => { | ||
document.body.innerHTML = ` | ||
<usa-text-input> | ||
<label for="input-type-text">Text input label</label> | ||
<input id="input-type-text" name="input-type-text"> | ||
</usa-text-input> | ||
`; | ||
}); | ||
|
||
it("adds input element to shadow DOM", () => { | ||
expect(getInputElement()); | ||
}); | ||
|
||
it("adds the input id in shadow DOM", () => { | ||
expect(getInputElement().getAttribute("id")).toContain("input-type-text"); | ||
}); | ||
|
||
it("adds the input name in shadow DOM", () => { | ||
expect(getInputElement().getAttribute("name")).toContain("input-type-text"); | ||
}); | ||
|
||
it("adds the usa-input class in shadow DOM", () => { | ||
expect(getInputElement().getAttribute("class")).toContain("usa-input"); | ||
}); | ||
|
||
it("adds label element to shadow DOM", () => { | ||
expect(getLabelElement()); | ||
}); | ||
|
||
it("adds the usa-label class in shadow DOM", () => { | ||
expect(getLabelElement().getAttribute("class")).toContain("usa-label"); | ||
}); | ||
|
||
it("connects the label and input with the for attribute in shadow DOM", () => { | ||
expect(getInputElement().getAttribute("id")).toMatch(getLabelContext()); | ||
}); | ||
}); | ||
|