feat(tests): add fresh-testing-library tests (#1990)

This commit is contained in:
Craig Doremus
2023-11-15 18:25:13 +01:00
committed by GitHub
parent 5d690bede1
commit b5816cd001
20 changed files with 447 additions and 8 deletions
+25
View File
@@ -0,0 +1,25 @@
import { assertExists, assertFalse } from "$std/testing/asserts.ts";
import { cleanup, fireEvent, render, setup } from "$fresh-testing-library";
import { afterEach, beforeAll, describe, it } from "$std/testing/bdd.ts";
import Counter from "../../../www/islands/Counter.tsx";
describe("islands/Counter.tsx", () => {
beforeAll(setup);
afterEach(cleanup);
it("should display count and increment/decrement it correctly", async () => {
const count = 9;
const screen = render(<Counter start={count} />);
const plusOne = screen.getByTitle("Add 1");
const minusOne = screen.getByTitle("Subtract 1");
assertExists(screen.queryByText("9"));
await fireEvent.click(plusOne);
assertFalse(screen.queryByText("9"));
assertExists(screen.queryByText("10"));
await fireEvent.click(minusOne);
assertExists(screen.queryByText("9"));
assertFalse(screen.queryByText("10"));
});
});
+32
View File
@@ -0,0 +1,32 @@
import { assert, assertExists, assertFalse } from "$std/testing/asserts.ts";
import {
cleanup,
fireEvent,
render,
setup,
} from "$fresh-testing-library/components.ts";
import { fn } from "$fresh-testing-library/expect.ts";
import { afterEach, beforeAll, describe, it } from "$std/testing/bdd.ts";
import SearchButton from "../../../www/islands/SearchButton.tsx";
import docsearch from "https://esm.sh/@docsearch/js@3?target=es2020";
describe("islands/SearchButton.tsx", () => {
beforeAll(setup);
afterEach(cleanup);
it("should contain class applied in props", () => {
// create mock implementation of docsearch
// @ts-ignore mock impl
const dsearch = fn(docsearch).mockImplementation((
applId: string,
apiKey: string,
indexName: string,
container: HTMLElement | string,
) => {});
const { getByTitle } = render(
<SearchButton class="font-bold" docsearch={dsearch} />,
);
const search = getByTitle("Search Button");
assert(search.classList.contains("font-bold"));
});
});