Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
DOMのテストを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
takahash committed Oct 9, 2023
1 parent ca3fb9e commit 2944c74
Show file tree
Hide file tree
Showing 7 changed files with 874 additions and 13 deletions.
2 changes: 1 addition & 1 deletion dom/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
</section>

</body>
<script src="./script.js"></script>
<script type="module" src="./script.js"></script>
</html>

24 changes: 14 additions & 10 deletions dom/script.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
const link = document.querySelector("a");
link.textContent = "REDIMPULZ";
link.href = "https://redimpulz.com/";
export const main = () => {
const link = document.querySelector("a");
link.textContent = "REDIMPULZ";
link.href = "https://redimpulz.com/";

const section = document.querySelector("section");
const p = document.createElement("p");
p.textContent = "We hope you enjoy JavaScript.";
section.appendChild(p);
p.setAttribute("class", "highlight");
const section = document.querySelector("section");
const p = document.createElement("p");
p.textContent = "We hope you enjoy JavaScript.";
section.appendChild(p);
p.setAttribute("class", "highlight");

const temp = document.querySelector("#temp");
section.removeChild(temp);
const temp = document.querySelector("#temp");
section.removeChild(temp);
};

window.addEventListener("load", main);
35 changes: 35 additions & 0 deletions dom/script.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { test, expect, describe, beforeEach } from "vitest";
import { screen } from "@testing-library/dom";

import { main } from "./script";

beforeEach(() => {
document.body.innerHTML = `
<a data-testid="a" href=""></a>
<section data-testid="section">
<div data-testid="temp" id="temp">temp content</div>
</section>
`;
});

describe("", () => {
test("リンクが正しく追加されている", () => {
main();
const a = screen.queryByTestId<HTMLAnchorElement>("a");
expect(a?.textContent).toEqual("REDIMPULZ");
expect(a?.href).toEqual("https://redimpulz.com/");
});

test("section要素の下に指定の要素が追加されている", () => {
main();
const section = screen.queryByTestId("section");
expect(section).toContainHTML(
'<p class="highlight">We hope you enjoy JavaScript.</p>'
);
});

test("IDがtempの要素が削除されている", () => {
main();
expect(screen.queryByTestId("temp")).toEqual(null);
});
});
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"author": "Ken Takahashi <[email protected]>",
"license": "MIT",
"devDependencies": {
"@testing-library/dom": "^9.3.3",
"@testing-library/jest-dom": "^6.1.3",
"@vitest/coverage-v8": "^0.34.1",
"jest": "^29.6.2",
"vitest": "^0.34.1"
Expand All @@ -17,5 +19,8 @@
"volta": {
"node": "18.17.1",
"yarn": "1.22.19"
},
"dependencies": {
"happy-dom": "^12.9.0"
}
}
1 change: 1 addition & 0 deletions testSetup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "@testing-library/jest-dom/vitest";
8 changes: 8 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
environment: "happy-dom",
setupFiles: "./testSetup.ts",
},
});
Loading

0 comments on commit 2944c74

Please sign in to comment.