-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cy.ts
More file actions
473 lines (403 loc) · 19.1 KB
/
test.cy.ts
File metadata and controls
473 lines (403 loc) · 19.1 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/* eslint-disable quotes */
// NOTE! This app was rebranded from "Notes app" to "Memories app" during development.
// Some fields and navigation links might still contain the old name.
describe("Memories app", function () {
let CYPRESS_TEST_EMAIL = Cypress.env("CYPRESS_TEST_EMAIL");
// if CYPRESS_TEST_EMAIL is undefined, try to get it from environment variables
if (!CYPRESS_TEST_EMAIL) {
CYPRESS_TEST_EMAIL = process.env.CYPRESS_TEST_EMAIL;
}
// if CYPRESS_TEST_EMAIL is still undefined, throw an error
if (!CYPRESS_TEST_EMAIL) {
throw new Error("CYPRESS_TEST_EMAIL environment variable is not set.");
}
function registerUser(email: string, name: string, password: string) {
cy.visit("/register");
// Wait for the form to load
cy.get("#email", { timeout: 20000 }).should("be.visible");
cy.get("#name").should("be.visible");
cy.get("#password").should("be.visible");
cy.get("#confirmPassword").should("be.visible");
// clear the form
cy.get("#email").clear();
cy.get("#name").clear();
cy.get("#password").clear();
cy.get("#confirmPassword").clear();
// Fill in the form
if (email) cy.get("#email").type(email);
if (name) cy.get("#name").type(name);
if (password) {
cy.get("#password").type(password);
cy.get("#confirmPassword").type(password);
}
// Click the register button
cy.get("#register").click();
}
function loginUser(email: string, password: string) {
// clear the form
cy.get("#email", { timeout: 10000 }).clear();
cy.get("#password").clear();
// Fill in the form
if (email) cy.get("#email").type(email);
if (password) cy.get("#password").type(password);
// Click the login button
cy.get("#signin").click();
}
function logoutUser() {
cy.get("body").then(($body) => {
if ($body.find("#dropdownMenu").length > 0) {
// Open the dropdown menu
cy.get("#dropdownMenu").click();
// Wait for the dropdown to open and the logout button to become visible
cy.get(".logoutButton").should("be.visible");
// Click the logout button
cy.get(".logoutButton").click();
cy.contains("Logged out successfully, see you soon! 👋", { timeout: 6000 }).should(
"be.visible"
);
}
});
}
beforeEach(function () {
// check that we are in testing env
cy.visit("/");
cy.contains("Using testing environment", { timeout: 6000 }).should("be.visible");
// logout user
logoutUser();
// reset the database
cy.request("http://localhost:3000/api/testing/reset")
.its("status")
.should("eq", 200)
.clearAllCookies()
.clearLocalStorage()
.clearAllSessionStorage()
.reload();
// wait for page to fully reload before next test, github cicd is slow
cy.contains("Using testing environment", { timeout: 6000 }).should("be.visible");
});
describe("LandingPage", () => {
it("front page requires to login/registeration", function () {
cy.visit("/");
cy.contains("Create an account to start saving your memories");
cy.contains("Sign in");
cy.contains("Create an account");
cy.get("#addNoteButton").should("not.exist");
});
it("you can dismiss the announcement banner", () => {
cy.get("#dismissBannerButton").click();
cy.get("#dismissBannerButton").should("not.exist");
});
it("you can dismiss the cookies banner", () => {
cy.get("#cookieAcceptButton").click();
cy.get("#cookieAcceptButton").should("not.exist");
});
describe("RegisterPage", () => {
beforeEach(() => {
// Visit the register page before each test
cy.visit("/register");
});
it("it renders", () => {
cy.get('input[type="email"]');
cy.get('input[type="text"]');
cy.get('input[type="password"]');
cy.get("button").contains("Register");
});
it("registeration works", () => {
registerUser(CYPRESS_TEST_EMAIL, "Test User", "password123");
cy.contains("Registered successfully!", { timeout: 6000 }).should("be.visible");
cy.contains("No memories yet");
});
it("registeration fails if email is already taken", () => {
registerUser(CYPRESS_TEST_EMAIL, "Test User", "password123");
cy.contains("Registered successfully!", { timeout: 6000 }).should("be.visible");
cy.contains("No memories yet");
logoutUser();
cy.contains("Create an account to start saving your memories");
cy.contains("Sign in");
cy.contains("Create an account");
cy.get("#addNoteButton").should("not.exist");
cy.visit("/register");
registerUser(CYPRESS_TEST_EMAIL, "Test User", "password123");
cy.contains("Email is already in use. Please try again.");
});
it("registeration fails if email is invalid", () => {
registerUser("testuser", "Test User", "password123");
cy.contains("Invalid email address. Please try again.");
});
it("registeration fails if password is weak", () => {
registerUser(CYPRESS_TEST_EMAIL, "Test User", "pass");
cy.contains("Weak password.");
});
it("registeration fails if email, name or password is empty", () => {
registerUser("", "Test user", "password123");
cy.contains("Please fill in all the fields.");
registerUser(CYPRESS_TEST_EMAIL, "", "password123");
cy.contains("Please fill in all the fields.");
registerUser(CYPRESS_TEST_EMAIL, "Test user", "");
cy.contains("Please fill in all the fields.");
});
});
describe("LoginPage", () => {
beforeEach(() => {
// create user before each test
cy.visit("/register");
registerUser(CYPRESS_TEST_EMAIL, "Test user", "password123");
cy.contains("Registered successfully!", { timeout: 6000 }).should("be.visible");
cy.wait(1000);
cy.contains("No memories yet");
logoutUser();
// Visit the login page before each test
cy.visit("/login");
});
it("it renders", () => {
cy.contains("Sign in to your account");
cy.get('input[type="email"]');
cy.get('input[type="password"]');
cy.get("button").contains("Sign in");
});
it("login works", () => {
loginUser(CYPRESS_TEST_EMAIL, "password123");
cy.contains("Logged in successfully!", { timeout: 6000 }).should("be.visible");
cy.contains("No memories yet");
});
it("login fails if email is invalid", () => {
loginUser("testuser", "password123");
cy.contains("Invalid email address. Please try again.");
});
it("login fails if password is weak", () => {
loginUser(CYPRESS_TEST_EMAIL, "pass");
cy.contains("Invalid credentials.");
});
it("login fails if password is incorrect", () => {
loginUser(CYPRESS_TEST_EMAIL, "password1234");
cy.contains("Invalid credentials", { timeout: 6000 }).should("be.visible");
});
it("login fails if email or password is empty", () => {
loginUser("", "password123");
cy.contains("Invalid email address. Please try again.");
loginUser(CYPRESS_TEST_EMAIL, "");
cy.contains("Please fill in all the fields.");
});
it("login fails if user does not exist", () => {
loginUser("notregistered@gmail.com", "password123");
cy.contains("Invalid credentials", { timeout: 6000 }).should("be.visible");
});
it("user can reset password", () => {
cy.get('input[type="email"]').clear();
cy.get('input[type="email"]').type(CYPRESS_TEST_EMAIL);
cy.contains("Reset password").click();
cy.contains("Password reset email sent.", { timeout: 6000 }).should("be.visible");
});
});
});
describe("When logged in", () => {
beforeEach(() => {
// create user before each test
registerUser(CYPRESS_TEST_EMAIL, "Test user", "password123");
cy.wait(1000);
cy.contains("Registered successfully!", { timeout: 6000 }).should("be.visible");
});
// NotePage has list of memories
describe("NotePage)", () => {
it("it renders", () => {
cy.contains("No memories yet");
cy.get("#addNoteButton").should("be.visible");
});
it("user can add a memory", () => {
cy.get("#addNoteButton").click();
cy.get("#noteTitle").type("Test memory");
cy.get("#noteContent").type("Test memory content");
// has to wait for the search results to load
cy.wait(1000);
cy.get(".mapboxgl-ctrl-geocoder--input").type("Tampere").wait(1000).type("{enter}");
cy.get("#saveNoteButton").click();
cy.contains("You have 1 memories", { timeout: 10000 });
cy.contains("Test memory");
cy.contains("Test memory content");
});
describe("when there is a memory", () => {
beforeEach(() => {
cy.get("#addNoteButton").click();
cy.get("#noteTitle", { timeout: 10000 }).type("Test memory by test user");
cy.get("#noteContent").type("Test memory content");
// has to wait for the search results to load
cy.wait(1000);
cy.get(".mapboxgl-ctrl-geocoder--input")
.type("Tampere")
.wait(1000)
.type("{enter}");
cy.get("#saveNoteButton").click();
cy.contains("You have 1 memories", { timeout: 10000 });
cy.contains("Test memory by test user");
cy.contains("Test memory content");
// qait for the note card button to be ready, github cicd is slow
cy.get('[id^="toNoteButton"]', { timeout: 10000 }).should("be.visible");
});
it("you can add another memory", () => {
cy.get("#addNoteButton").click();
cy.get("#noteTitle").type("Another memory by test user");
cy.get("#noteContent").type("Another memory content");
// has to wait for the search results to load
cy.wait(1000);
cy.get(".mapboxgl-ctrl-geocoder--input")
.type("Tampere")
.wait(1000)
.type("{enter}");
cy.get("#saveNoteButton").click();
cy.contains("You have 2 memories");
cy.contains("Another memory by test user");
cy.contains("Another memory content");
});
it("you can open the memory", () => {
cy.get('[id^="toNoteButton"]').should("be.visible").click();
cy.contains("Test memory by test user");
cy.contains("Test memory content");
});
it("you can edit the memory", () => {
cy.get('[id^="toNoteButton"]').should("be.visible").click();
cy.contains("Edit").click();
cy.wait(1000);
cy.get("#noteTitle").clear().type("Edited memory by test user");
cy.get("#noteContent").clear().type("Edited memory content");
cy.get("#editNoteButton").click();
cy.visit("/");
cy.contains("You have 1 memories", { timeout: 10000 });
cy.contains("Edited memory by test user");
cy.contains("Edited memory content");
});
it("you can delete the memory", () => {
cy.get('[id^="toNoteButton"]').should("be.visible").click();
cy.contains("Delete").click();
// cy.contains("Are you sure you want to delete this memory?");
// cy.contains("Yes").click();
cy.contains("Memory deleted successfully", { timeout: 6000 }).should(
"be.visible"
);
cy.contains("No memories yet");
});
it("memories don't show after user logs out", () => {
logoutUser();
cy.contains("Create an account to start saving your memories");
cy.contains("Sign in");
cy.contains("Create an account");
cy.get("#addNoteButton").should("not.exist");
});
});
});
// ProfilePage has user's profile information
describe("ProfilePage", () => {
beforeEach(() => {
cy.visit("/profile");
});
it("it renders", () => {
cy.contains("Profile");
cy.contains("Test user");
cy.contains(CYPRESS_TEST_EMAIL);
cy.get("#defaultLocation").should("not.exist");
cy.get("#editNameButton").should("be.visible");
cy.get("#deleteAccountButton").should("be.visible");
});
it("user can change their name", () => {
cy.get("#editNameButton").click();
cy.get("#name").clear().type("Edited name");
cy.get("#updateNameButton").click();
cy.contains("Name updated successfully!", { timeout: 6000 }).should("be.visible");
cy.contains("Edited name");
});
it("user can delete their account", () => {
cy.get("#deleteAccountButton").click();
// alert doesn't popup with cypress so we have to click the button
cy.contains("Account deleted successfully.", { timeout: 6000 }).should(
"be.visible"
);
cy.contains("Create an account to start saving your memories");
cy.contains("Sign in");
cy.contains("Create an account");
cy.get("#addNoteButton").should("not.exist");
});
});
describe("TimelinePage", () => {
beforeEach(() => {
cy.get("#addNoteButton").click();
cy.get("#noteTitle", { timeout: 10000 }).type("Test memory by test user");
cy.get("#noteContent").type("Test memory content");
// has to wait for the search results to load
cy.wait(1000);
cy.get(".mapboxgl-ctrl-geocoder--input").type("Tampere").wait(1000).type("{enter}");
cy.get("#saveNoteButton").click();
cy.contains("You have 1 memories", { timeout: 10000 });
cy.contains("Test memory by test user");
cy.contains("Test memory content");
// ensure the memory card is fully rendered before navigating, github cicd is slow
cy.get('[id^="toNoteButton"]', { timeout: 5000 }).should("exist");
cy.visit("/notes/timeline");
});
it("it renders", () => {
cy.contains("Timeline");
});
it("user can see their memories in timeline", () => {
cy.contains("Test memory by test user");
});
it("user can open the memory", () => {
cy.contains("Test memory by test user").should("be.visible").click();
cy.contains("Test memory content");
});
});
// SettingsPage has user's settings, like language and default location
describe("SettingsPage", () => {
beforeEach(() => {
cy.visit("/settings");
});
it("it renders", () => {
cy.contains("Settings");
// has to wait for the search results to load
cy.wait(1000);
cy.get(".mapboxgl-ctrl-geocoder--input").should("be.visible");
cy.get("#saveLocationButton").should("be.visible");
});
it("user can change their default location", () => {
cy.wait(1000);
cy.get(".mapboxgl-ctrl-geocoder--input").type("Tampere").wait(1000).type("{enter}");
cy.get("#saveLocationButton").click();
cy.contains("Default map location updated successfully!", { timeout: 6000 }).should(
"be.visible"
);
});
it("user can change their language", () => {
cy.get("#changeLanguageButton").click();
cy.contains("button", "Finnish").click();
cy.contains("Vaihda kieli").should("be.visible");
});
});
});
describe("Redirected to front page (LandingPage) when not logged in", () => {
it("NotePage redirects to front page", () => {
cy.visit("/notes");
cy.contains("Create an account to start saving your memories");
cy.contains("Sign in");
cy.contains("Create an account");
cy.get("#addNoteButton").should("not.exist");
});
it("AddNote page redirects to front page", () => {
cy.visit("/notes/add");
cy.contains("Create an account to start saving your memories");
cy.contains("Sign in");
cy.contains("Create an account");
cy.get("#addNoteButton").should("not.exist");
});
it("ProfilePage redirects to front page", () => {
cy.visit("/profile");
cy.contains("Create an account to start saving your memories");
cy.contains("Sign in");
cy.contains("Create an account");
cy.get("#addNoteButton").should("not.exist");
});
it("SettingsPage redirects to front page", () => {
cy.visit("/settings");
cy.contains("Create an account to start saving your memories");
cy.contains("Sign in");
cy.contains("Create an account");
cy.get("#addNoteButton").should("not.exist");
});
});
});