Skip to content

Commit

Permalink
Add TypeScript tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomodwyer committed Jan 22, 2025
1 parent 60ba98c commit 2bef76b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
38 changes: 38 additions & 0 deletions assets/src/scripts/__tests__/components/DescendantToggle.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import "@testing-library/jest-dom";
import { render, screen } from "@testing-library/react";
import { userEvent } from "@testing-library/user-event";
import React from "react";
import { expect, it, vi } from "vitest";
import DescendantToggle from "../../components/DescendantToggle";

it("Show a minus symbol if expanded", async () => {
render(
<DescendantToggle isExpanded path="/" toggleVisibility={() => null} />,
);
expect(screen.queryByText("⊟")).toBeVisible;
expect(screen.queryByText("⊞")).toBeNull;
});

it("Show a plus symbol if not expanded", async () => {
render(
<DescendantToggle
isExpanded={false}
path="/"
toggleVisibility={() => null}
/>,
);
expect(screen.queryByText("⊞")).toBeVisible;
expect(screen.queryByText("⊟")).toBeNull;
});

it("Triggers the toggle visibility function", async () => {
const fn = vi.fn();

render(
<DescendantToggle isExpanded path="/test-path/" toggleVisibility={fn} />,
);

await userEvent.click(screen.getByText("⊟"));

expect(fn.mock.calls[0][0]).toBe("/test-path/");
});
20 changes: 20 additions & 0 deletions assets/src/scripts/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expect, it } from "vitest";
import { getCookie, readValueFromPage } from "../_utils";

it("gets the cookie value", () => {
const name = "csrftoken";
const value = "01JJ6V33G19M1ZFEB4JAQMMTQW";
document.cookie = `${name}=${value || ""};`;
expect(getCookie(name)).toBe(value);
});

it("reads the values of a JSON script on the page", () => {
const blob = {
hello: "world",
};
document.body.innerHTML = `
<script id="all-codes">${JSON.stringify(blob)}</script>
`;

expect(readValueFromPage("all-codes")).toEqual(blob);
});

0 comments on commit 2bef76b

Please sign in to comment.