-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuestRequestForm.test.tsx
177 lines (157 loc) · 4.17 KB
/
QuestRequestForm.test.tsx
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import QuestRequestForm from "@/app/request/_components/QuestRequestForm"
import { render, screen, waitFor } from "@testing-library/react"
import userEvent from "@testing-library/user-event"
import * as questApis from "@/clients/quests/apis"
import * as tagApis from "@/clients/tags/apis"
import selectEvent from "react-select-event"
import { tags } from "@/clients/tags/fixtures"
vi.mock("next/navigation", () => ({
useRouter: () => import("next-router-mock"),
}))
vi.mock("window", () => ({
alert: () => {},
}))
vi.mock("@/clients/quests/apis")
vi.mock("@/clients/tags/apis")
const user = userEvent.setup()
const setup = async () => {
const mockCreateQuestFn = vi.spyOn(questApis, "postQuest")
const mockCreateTagFn = vi.spyOn(tagApis, "postTag").mockResolvedValue([
{
id: "tag4",
name: "tag4",
createdAt: "",
},
])
render(<QuestRequestForm tags={tags} />)
const inputTitle = async () => {
const titleInput = screen.getByRole("textbox", { name: "クエスト名" })
await user.type(titleInput, "title")
}
const inputDescription = async () => {
const descriptionInput = screen.getByRole("textbox", {
name: "クエストの説明",
})
await user.type(descriptionInput, "description")
}
const selectLevel = async () => {
await selectEvent.select(screen.getByLabelText("難易度"), "3")
}
const selectTag = async () => {
await selectEvent.select(screen.getByLabelText("タグ"), "tag2")
}
const createTag = async () => {
await selectEvent.create(screen.getByLabelText("タグ"), "tag4")
}
const submit = async () => {
await user.click(screen.getByRole("button", { name: "申請を送信" }))
}
return {
mockCreateQuestFn,
mockCreateTagFn,
inputTitle,
inputDescription,
selectLevel,
selectTag,
createTag,
submit,
}
}
describe("QuestRequestForm", () => {
test("タグを指定せずに正常に送信できる", async () => {
const {
mockCreateQuestFn,
inputTitle,
inputDescription,
selectLevel,
submit,
} = await setup()
await inputTitle()
await inputDescription()
await selectLevel()
await submit()
expect(mockCreateQuestFn).toHaveBeenCalledWith({
title: "title",
description: "description",
level: 3,
tags: [],
image: undefined,
alt: undefined,
})
})
test("既存のタグを使って正常に送信できる", async () => {
const {
mockCreateQuestFn,
inputTitle,
inputDescription,
selectLevel,
selectTag,
submit,
} = await setup()
await inputTitle()
await inputDescription()
await selectLevel()
await selectTag()
await submit()
expect(mockCreateQuestFn).toHaveBeenCalledWith({
title: "title",
description: "description",
level: 3,
tags: ["tag2"],
image: undefined,
alt: undefined,
})
})
test("タグを新規作成して正常に送信できる", async () => {
const {
mockCreateQuestFn,
mockCreateTagFn,
inputTitle,
inputDescription,
selectLevel,
createTag,
submit,
} = await setup()
await inputTitle()
await inputDescription()
await selectLevel()
await createTag()
await submit()
expect(mockCreateTagFn).toHaveBeenCalledWith(["tag4"])
expect(mockCreateQuestFn).toHaveBeenCalledWith({
title: "title",
description: "description",
level: 3,
tags: ["tag4"],
image: undefined,
alt: undefined,
})
})
test("クエスト名がないバリデーションエラー", async () => {
const { inputDescription, selectLevel, selectTag, submit } = await setup()
await inputDescription()
await selectLevel()
await selectTag()
await submit()
await waitFor(() =>
expect(
screen.getByRole("textbox", { name: "クエスト名" })
).toHaveAccessibleErrorMessage("Invalid length")
)
})
test("クエストの説明がないバリデーションエラー", async () => {
const { inputTitle, selectLevel, selectTag, submit } = await setup()
await inputTitle()
await selectLevel()
await selectTag()
await submit()
await waitFor(() =>
expect(
screen.getByRole("textbox", { name: "クエストの説明" })
).toHaveAccessibleErrorMessage("Invalid length")
)
})
test("画像があるのにaltがないバリデーションエラー", async () => {
//todo: 実装したら書く
})
})