Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web components - Input: Create unit tests #79

Open
wants to merge 7 commits into
base: al-text-input-alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import js from "@eslint/js";
import eslintConfigPrettierRecommended from "eslint-config-prettier";
import vitest from "@vitest/eslint-plugin";

export default [js.configs.recommended, eslintConfigPrettierRecommended];
export default [js.configs.recommended, eslintConfigPrettierRecommended, vitest];
199 changes: 199 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"@storybook/theming": "^8.2.9",
"@storybook/web-components": "^8.2.9",
"@storybook/web-components-vite": "^8.2.9",
"@vitest/eslint-plugin": "^1.1.7",
"@vitest/ui": "^1.6.0",
"axe-playwright": "^2.0.2",
"concurrently": "^8.2.2",
Expand Down
36 changes: 36 additions & 0 deletions src/components/usa-text-input/usa-text-input.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { beforeEach, describe, expect, it } from "vitest";

import "./index.js";

function getInsideInput() {
return document.body
.querySelector("usa-text-input")
.shadowRoot;
}

function getLabelContext() {
return getInsideInput().querySelector("label")?.shadowRoot?.getAttribute("for")
}

function getInputElement() {
return getInsideInput().querySelector("input");
}

describe("usa-text-input component", async () => {
beforeEach(async () => {
document.body.innerHTML = `
<usa-text-input>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: For discussion, are there any props we want to support in this custom element?

For example: disabled, error, success, width?

Last three are from USWDS text input variants.

image

<label for="input-type-text">Text input label</label>
<input id="input-type-text" name="input-type-text">
</usa-text-input>
`
});

it("Should show props", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: This test might only be necessary if we progressively enhance the component.

expect(getInputElement().getAttribute("id")).toContain("input-type-text");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: Might not be necessary if we're not progressively enhancing or doing validation.

})

it("Should have an associated label", () => {
expect(getInputElement().getAttribute("id")).toMatch(getLabelContext());
})
})
Loading