-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathall.cy.ts
269 lines (233 loc) · 8.49 KB
/
all.cy.ts
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/// <reference types="cypress" />
/// <reference types="../../cypress/support" />
describe("form-mantine-mutation-mode", () => {
const mockPost = {
title:
"Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
content: `Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.`,
status: "draft",
};
const fillForm = () => {
cy.get("#title").clear().type(mockPost.title);
cy.get("#content textarea").clear({ force: true }).type(mockPost.content);
cy.fillMantineStatus("Draft");
cy.get("#categoryId").click().type("{downArrow}{enter}", { force: true });
};
const assertSuccessResponse = (response: any) => {
const body = response?.body;
expect(response?.statusCode).to.eq(200);
expect(body).to.have.property("id");
expect(body).to.have.property("category");
expect(body?.title).to.eq(mockPost.title);
expect(body?.status?.toLowerCase()).to.eq(mockPost?.status?.toLowerCase());
};
const submitForm = () => {
return cy.getSaveButton().click();
};
const changeMutationMode = (
mode: "pessimistic" | "optimistic" | "undoable",
) => {
// uppercase first letter
const uppercaseMode = mode.charAt(0).toUpperCase() + mode.slice(1);
return cy.get(".mantine-Radio-label").contains(uppercaseMode).click({
force: true,
});
};
const waitForLoading = () => {
cy.getMantineLoadingOverlay().should("not.exist");
cy.getSaveButton().should("not.be.disabled");
};
beforeEach(() => {
cy.clearAllCookies();
cy.clearAllLocalStorage();
cy.clearAllSessionStorage();
cy.visit("/");
});
it("should edit record when mutation mode is pessimistic", () => {
changeMutationMode("pessimistic");
cy.wait("@getPosts");
cy.getEditButton().first().click();
cy.wait("@getPost").then((interception) => {
const response = interception?.response;
const body = response?.body;
// wait loading state and render to be finished
waitForLoading();
cy.get("#title").should("have.value", body?.title);
cy.get("#content textarea").should("have.value", body?.content);
});
fillForm();
submitForm();
cy.wait("@patchPost").then((interception) => {
const response = interception?.response;
assertSuccessResponse(response);
cy.getMantineNotification().contains(/success/gi);
// should redirect to list page
cy.location().should((loc) => {
expect(loc.pathname).to.eq("/posts");
});
});
});
it("should edit record when mutation mode is undoable", () => {
changeMutationMode("undoable");
cy.wait("@getPosts");
cy.getEditButton().first().click();
// wait loading state and render to be finished
cy.wait("@getPost");
waitForLoading();
fillForm();
submitForm();
// should redirect to list page
cy.location().should((loc) => {
expect(loc.pathname).to.eq("/posts");
});
cy.getMantineNotification().contains(/seconds to undo/gi);
cy.getMantineNotification()
.contains("seconds to undo", { timeout: 10000 })
.should("not.exist");
cy.wait("@patchPost").then((interception) => {
const response = interception?.response;
assertSuccessResponse(response);
cy.getMantineNotification().contains(/success/gi);
});
});
it("should undo editing when mutation mode is undoable", () => {
changeMutationMode("undoable");
cy.wait("@getPosts");
cy.getEditButton().first().click();
// wait loading state and render to be finished
cy.wait("@getPost");
cy.wait("@getCategories");
waitForLoading();
submitForm();
// should redirect to list page
cy.location().should((loc) => {
expect(loc.pathname).to.eq("/posts");
});
cy.getMantineNotification()
.contains(/seconds to undo/gi)
.parent()
.siblings("button")
.click({ force: true });
cy.getMantineNotification().should("not.exist");
cy.get("@patchPost").should("be.null");
});
it("should create a record when mutation mode is optimistic", () => {
changeMutationMode("optimistic");
cy.getCreateButton().click();
// wait loading state and render to be finished
waitForLoading();
fillForm();
submitForm();
// should redirect to list page without waiting for response
cy.location().should((loc) => {
expect(loc.pathname).to.eq("/posts");
});
cy.wait("@postPost").then((interception) => {
const response = interception?.response;
assertSuccessResponse(response);
cy.getMantineNotification().contains(/success/gi);
});
});
it("should edit a record when mutation mode is optimistic", () => {
changeMutationMode("optimistic");
cy.wait("@getPosts");
cy.getEditButton().first().click();
// wait loading state and render to be finished
cy.wait("@getPost");
waitForLoading();
fillForm();
submitForm();
// should redirect to list page without waiting for response
cy.location().should((loc) => {
expect(loc.pathname).to.eq("/posts");
});
cy.wait("@patchPost").then((interception) => {
const response = interception?.response;
assertSuccessResponse(response);
cy.getMantineNotification().contains(/success/gi);
});
});
it("should delete record when mutation mode is pessimistic", () => {
changeMutationMode("pessimistic");
cy.wait("@getPosts");
cy.getEditButton().first().click();
// wait loading state and render to be finished
cy.wait("@getPost");
waitForLoading();
cy.getDeleteButton().click().getMantinePopoverDeleteButton().click();
cy.wait("@deletePost").then((interception) => {
const response = interception?.response;
expect(response?.statusCode).to.eq(200);
cy.getMantineNotification().contains(/success/gi);
cy.location().should((loc) => {
expect(loc.pathname).to.eq("/posts");
});
});
});
it("should delete record when mutation mode is undoable", () => {
changeMutationMode("undoable");
cy.wait("@getPosts");
cy.getEditButton().first().click();
// wait loading state and render to be finished
cy.wait("@getPost");
waitForLoading();
cy.getDeleteButton().click().getMantinePopoverDeleteButton().click();
// should show notification
cy.getMantineNotification().contains(/seconds to undo/gi);
// notification should disappear after certain time
cy.getMantineNotification()
.contains("seconds to undo", { timeout: 10000 })
.should("not.exist");
// should sent a PATCH request after certain time
cy.wait("@deletePost").then((interception) => {
const response = interception?.response;
expect(response?.statusCode).to.eq(200);
cy.getMantineNotification().contains(/success/gi);
cy.location().should((loc) => {
expect(loc.pathname).to.eq("/posts");
});
});
});
it("should undo deleting record when mutation mode is undoable", () => {
changeMutationMode("undoable");
cy.wait("@getPosts");
cy.getEditButton().first().click();
// wait loading state and render to be finished
cy.wait("@getPost");
waitForLoading();
cy.getDeleteButton().click().getMantinePopoverDeleteButton().click();
cy.getDeleteButton().should("be.disabled");
// should show notification
cy.getMantineNotification().contains(/seconds to undo/gi);
// click undo button
cy.getMantineNotification()
.contains(/seconds to undo/gi)
.parent()
.siblings("button")
.click();
cy.getDeleteButton().should("not.be.disabled");
cy.get("@deletePost").should("be.null");
});
it("should delete record when mutation mode is optimistic", () => {
changeMutationMode("optimistic");
cy.wait("@getPosts");
cy.getEditButton().first().click();
// wait loading state and render to be finished
cy.wait("@getPost");
waitForLoading();
cy.getDeleteButton().click().getMantinePopoverDeleteButton().click();
// should redirect to list page without waiting for response
cy.location().should((loc) => {
expect(loc.pathname).to.eq("/posts");
});
// should sent a DELETE request after certain time
cy.wait("@deletePost").then((interception) => {
const response = interception?.response;
expect(response?.statusCode).to.eq(200);
cy.getMantineNotification().contains(/success/gi);
cy.location().should((loc) => {
expect(loc.pathname).to.eq("/posts");
});
});
});
});