-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodoList.twd.test.ts
More file actions
127 lines (105 loc) · 4.04 KB
/
todoList.twd.test.ts
File metadata and controls
127 lines (105 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { twd, expect, userEvent, screenDom } from "twd-js";
import { describe, it, beforeEach } from "twd-js/runner";
import todoListMock from "./mocks/todoList.json";
describe("Todo List Page", () => {
beforeEach(() => {
twd.clearRequestMockRules();
});
it("should display the todo list", async () => {
await twd.mockRequest("getTodoList", {
method: "GET",
url: "/api/todos",
response: todoListMock,
status: 200,
});
await twd.visit("/todos");
await twd.waitForRequest("getTodoList");
const todo1Title = await screenDom.getByText("Learn TWD");
twd.should(todo1Title, "be.visible");
const todo2Title = await screenDom.getByText("Build Todo App");
twd.should(todo2Title, "be.visible");
const todo1Description = await screenDom.getByText("Understand how to use TWD for testing web applications");
twd.should(todo1Description, "be.visible");
const todo2Description = await screenDom.getByText("Create a todo list application to demonstrate TWD features");
twd.should(todo2Description, "be.visible");
const todo1Date = await screenDom.getByText("Date: 2024-12-20");
twd.should(todo1Date, "be.visible");
const todo2Date = await screenDom.getByText("Date: 2024-12-25");
twd.should(todo2Date, "be.visible");
});
it("should create a todo", async () => {
await twd.mockRequest("createTodo", {
method: "POST",
url: "/api/todos",
response: todoListMock[0],
status: 200,
});
await twd.mockRequest("getTodoList", {
method: "GET",
url: "/api/todos",
response: [],
status: 200,
});
await twd.visit("/todos");
await twd.waitForRequest("getTodoList");
const noTodosMessage = await screenDom.getByText("No todos yet. Create one above!");
twd.should(noTodosMessage, "be.visible");
await twd.mockRequest("getTodoList", {
method: "GET",
url: "/api/todos",
response: [
todoListMock[0]
],
status: 200,
});
const titleInput = await screenDom.getByLabelText("Title");
await userEvent.type(titleInput, "Test Todo");
const descriptionInput = await screenDom.getByLabelText("Description");
await userEvent.type(descriptionInput, "Test Description");
const dateInput = await screenDom.getByLabelText("Date");
await userEvent.type(dateInput, "2024-12-20");
const submitButton = await screenDom.getByRole("button", { name: "Create Todo" });
await userEvent.click(submitButton);
await twd.waitForRequest("getTodoList");
const rule = await twd.waitForRequest("createTodo");
expect(rule.request).to.deep.equal({
title: "Test Todo",
description: "Test Description",
date: "2024-12-20",
});
const todoList = await screenDom.getAllByText(/Learn TWD|Build Todo App|Test Todo/);
expect(todoList).to.have.length(1);
});
it("should delete a todo", async () => {
await twd.mockRequest("deleteTodo", {
method: "DELETE",
url: "/api/todos/1",
response: null,
status: 204,
});
await twd.mockRequest("getTodoList", {
method: "GET",
url: "/api/todos",
response: todoListMock,
status: 200,
});
await twd.visit("/todos");
// Find the delete button for the first todo (Learn TWD)
// Since there are multiple delete buttons, we'll get all and use the first one
// which corresponds to the first todo item
const deleteButtons = await screenDom.getAllByRole("button", { name: "Delete" });
const deleteButton = deleteButtons[0];
await twd.mockRequest("getTodoList", {
method: "GET",
url: "/api/todos",
response: todoListMock.filter((todo) => todo.id !== "1"),
status: 200,
});
await userEvent.click(deleteButton);
await twd.waitForRequest("deleteTodo");
await twd.waitForRequest("getTodoList");
const todoList = await screenDom.getAllByText(/Learn TWD|Build Todo App/);
expect(todoList).to.have.length(1);
twd.should(todoList[0], "be.visible");
});
});