Skip to content

Commit 0ed0fb9

Browse files
committed
test: clear query cache between runs and use async find* queries
- beforeEach now also calls queryClient.clear() so the cache does not leak across twd.visit() SPA-navigations - replace screenDom.get* with the async find* variants so assertions wait for the DOM instead of failing on the first synchronous miss
1 parent 4201a1d commit 0ed0fb9

2 files changed

Lines changed: 29 additions & 21 deletions

File tree

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
import { twd, userEvent, screenDom } from "twd-js";
2-
import { describe, it } from "twd-js/runner";
2+
import { describe, it, beforeEach } from "twd-js/runner";
3+
import { queryClient } from "#/query-client";
34

45
describe("Hello World Page", () => {
6+
beforeEach(() => {
7+
twd.clearRequestMockRules();
8+
queryClient.clear();
9+
});
10+
511
it("should display the welcome title and counter button", async () => {
612
await twd.visit("/");
7-
8-
const title = await screenDom.getByText("Welcome to TWD");
13+
14+
const title = await screenDom.findByText("Welcome to TWD");
915
twd.should(title, 'be.visible');
10-
11-
const counterButton = await screenDom.getByText("Count is 0");
16+
17+
const counterButton = await screenDom.findByText("Count is 0");
1218
twd.should(counterButton, 'be.visible');
13-
19+
1420
await userEvent.click(counterButton);
1521
twd.should(counterButton, 'have.text', 'Count is 1');
16-
22+
1723
await userEvent.click(counterButton);
1824
twd.should(counterButton, 'have.text', 'Count is 2');
19-
25+
2026
await userEvent.click(counterButton);
2127
twd.should(counterButton, 'have.text', 'Count is 3');
2228
});
23-
});
29+
});

src/twd-tests/todoList.twd.test.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { twd, expect, userEvent, screenDom } from "twd-js";
22
import { describe, it, beforeEach } from "twd-js/runner";
3+
import { queryClient } from "#/query-client";
34
import todoListMock from "./mocks/todoList.json";
45

56
describe("Todo List Page", () => {
67
beforeEach(() => {
78
twd.clearRequestMockRules();
9+
queryClient.clear();
810
});
911

1012
it("should display the todo list", async () => {
@@ -17,22 +19,22 @@ describe("Todo List Page", () => {
1719
await twd.visit("/todos");
1820
await twd.waitForRequest("getTodoList");
1921

20-
const todo1Title = await screenDom.getByText("Learn TWD");
22+
const todo1Title = await screenDom.findByText("Learn TWD");
2123
twd.should(todo1Title, "be.visible");
2224

23-
const todo2Title = await screenDom.getByText("Build Todo App");
25+
const todo2Title = await screenDom.findByText("Build Todo App");
2426
twd.should(todo2Title, "be.visible");
2527

26-
const todo1Description = await screenDom.getByText("Understand how to use TWD for testing web applications");
28+
const todo1Description = await screenDom.findByText("Understand how to use TWD for testing web applications");
2729
twd.should(todo1Description, "be.visible");
2830

29-
const todo2Description = await screenDom.getByText("Create a todo list application to demonstrate TWD features");
31+
const todo2Description = await screenDom.findByText("Create a todo list application to demonstrate TWD features");
3032
twd.should(todo2Description, "be.visible");
3133

32-
const todo1Date = await screenDom.getByText("Date: 2024-12-20");
34+
const todo1Date = await screenDom.findByText("Date: 2024-12-20");
3335
twd.should(todo1Date, "be.visible");
3436

35-
const todo2Date = await screenDom.getByText("Date: 2024-12-25");
37+
const todo2Date = await screenDom.findByText("Date: 2024-12-25");
3638
twd.should(todo2Date, "be.visible");
3739
});
3840

@@ -52,7 +54,7 @@ describe("Todo List Page", () => {
5254
await twd.visit("/todos");
5355
await twd.waitForRequest("getTodoList");
5456

55-
const noTodosMessage = await screenDom.getByText("No todos yet. Create one above!");
57+
const noTodosMessage = await screenDom.findByText("No todos yet. Create one above!");
5658
twd.should(noTodosMessage, "be.visible");
5759

5860
await twd.mockRequest("getTodoList", {
@@ -64,16 +66,16 @@ describe("Todo List Page", () => {
6466
status: 200,
6567
});
6668

67-
const titleInput = await screenDom.getByLabelText("Title");
69+
const titleInput = await screenDom.findByLabelText("Title");
6870
await userEvent.type(titleInput, "Test Todo");
6971

70-
const descriptionInput = await screenDom.getByLabelText("Description");
72+
const descriptionInput = await screenDom.findByLabelText("Description");
7173
await userEvent.type(descriptionInput, "Test Description");
7274

73-
const dateInput = await screenDom.getByLabelText("Date");
75+
const dateInput = await screenDom.findByLabelText("Date");
7476
await userEvent.type(dateInput, "2024-12-20");
7577

76-
const submitButton = await screenDom.getByRole("button", { name: "Create Todo" });
78+
const submitButton = await screenDom.findByRole("button", { name: "Create Todo" });
7779
await userEvent.click(submitButton);
7880

7981
await twd.waitForRequest("getTodoList");
@@ -84,7 +86,7 @@ describe("Todo List Page", () => {
8486
date: "2024-12-20",
8587
});
8688

87-
const todoList = await screenDom.getAllByText(/Learn TWD|Build Todo App|Test Todo/);
89+
const todoList = await screenDom.findAllByText(/Learn TWD|Build Todo App|Test Todo/);
8890
expect(todoList).to.have.length(1);
8991
});
9092

0 commit comments

Comments
 (0)