This repository was archived by the owner on May 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathdefault_spec.js
709 lines (551 loc) · 21.9 KB
/
default_spec.js
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
const project = "default";
before(() => {
// When changing the base URL within a spec file, Cypress runs the spec twice
// To avoid rebuilding and redeployment on the second run, we check if the
// project has already been deployed.
cy.task("isDeployed").then((isDeployed) => {
// Cancel setup, if already deployed
if (isDeployed) return;
// Clear project folder
cy.task("clearProject", { project });
// Copy NextJS files
cy.task("copyFixture", {
project,
from: "pages",
to: "pages",
});
cy.task("copyFixture", {
project,
from: "next.config.js",
to: "next.config.js",
});
// Copy package.json file
cy.task("copyFixture", {
project,
from: "package.json",
to: "package.json",
});
// Copy Netlify settings
cy.task("copyFixture", {
project,
from: "netlify.toml",
to: "netlify.toml",
});
cy.task("copyFixture", {
project,
from: ".netlify",
to: ".netlify",
});
// Build
cy.task("buildProject", { project });
// Deploy
cy.task("deployProject", { project }, { timeout: 180 * 1000 });
});
// Set base URL
cy.task("getBaseUrl", { project }).then((url) => {
Cypress.config("baseUrl", url);
});
});
after(() => {
// While the before hook runs twice (it's re-run when the base URL changes),
// the after hook only runs once.
cy.task("clearDeployment");
});
describe("getInitialProps", () => {
context("with static route", () => {
it("loads TV shows", () => {
cy.visit("/");
cy.get("ul").first().children().should("have.length", 5);
});
it("loads TV shows when SSR-ing", () => {
cy.ssr("/");
cy.get("ul").first().children().should("have.length", 5);
});
});
context("with dynamic route", () => {
it("loads TV show", () => {
cy.visit("/shows/24251");
cy.get("h1").should("contain", "Show #24251");
cy.get("p").should("contain", "Animal Science");
});
it("loads TV show when SSR-ing", () => {
cy.ssr("/shows/24251");
cy.get("h1").should("contain", "Show #24251");
cy.get("p").should("contain", "Animal Science");
});
});
context("with catch-all route", () => {
it("displays all URL parameters, including query string parameters", () => {
cy.visit(
"/shows/94/this-is-all/being/captured/yay?search=dog&custom-param=cat"
);
// path parameters
cy.get("p").should("contain", "[0]: 94");
cy.get("p").should("contain", "[1]: this-is-all");
cy.get("p").should("contain", "[2]: being");
cy.get("p").should("contain", "[3]: captured");
cy.get("p").should("contain", "[4]: yay");
// query string parameters
cy.get("p").should("contain", "[search]: dog");
cy.get("p").should("contain", "[custom-param]: cat");
cy.get("h1").should("contain", "Show #94");
cy.get("p").should("contain", "Defiance");
});
it("displays all URL parameters when SSR-ing, including query string parameters", () => {
cy.visit(
"/shows/94/this-is-all/being/captured/yay?search=dog&custom-param=cat"
);
// path parameters
cy.get("p").should("contain", "[0]: 94");
cy.get("p").should("contain", "[1]: this-is-all");
cy.get("p").should("contain", "[2]: being");
cy.get("p").should("contain", "[3]: captured");
cy.get("p").should("contain", "[4]: yay");
// query string parameters
cy.get("p").should("contain", "[search]: dog");
cy.get("p").should("contain", "[custom-param]: cat");
cy.get("h1").should("contain", "Show #94");
cy.get("p").should("contain", "Defiance");
});
});
});
describe("getServerSideProps", () => {
context("with static route", () => {
it("loads TV shows", () => {
cy.visit("/getServerSideProps/static");
cy.get("h1").should("contain", "Show #42");
cy.get("p").should("contain", "Sleepy Hollow");
});
it("loads TV shows when SSR-ing", () => {
cy.ssr("/getServerSideProps/static");
cy.get("h1").should("contain", "Show #42");
cy.get("p").should("contain", "Sleepy Hollow");
});
it("loads page props from data .json file when navigating to it", () => {
cy.visit("/");
cy.window().then((w) => (w.noReload = true));
// Navigate to page and test that no reload is performed
// See: https://glebbahmutov.com/blog/detect-page-reload/
cy.contains("getServerSideProps/static").click();
cy.get("h1").should("contain", "Show #42");
cy.get("p").should("contain", "Sleepy Hollow");
cy.window().should("have.property", "noReload", true);
});
});
context("with dynamic route", () => {
it("loads TV show", () => {
cy.visit("/getServerSideProps/1337");
cy.get("h1").should("contain", "Show #1337");
cy.get("p").should("contain", "Whodunnit?");
});
it("loads TV show when SSR-ing", () => {
cy.ssr("/getServerSideProps/1337");
cy.get("h1").should("contain", "Show #1337");
cy.get("p").should("contain", "Whodunnit?");
});
it("loads page props from data .json file when navigating to it", () => {
cy.visit("/");
cy.window().then((w) => (w.noReload = true));
// Navigate to page and test that no reload is performed
// See: https://glebbahmutov.com/blog/detect-page-reload/
cy.contains("getServerSideProps/1337").click();
cy.get("h1").should("contain", "Show #1337");
cy.get("p").should("contain", "Whodunnit?");
cy.contains("Go back home").click();
cy.contains("getServerSideProps/1338").click();
cy.get("h1").should("contain", "Show #1338");
cy.get("p").should("contain", "The Whole Truth");
cy.window().should("have.property", "noReload", true);
});
});
context("with catch-all route", () => {
it("does not match base path (without params)", () => {
cy.request({
url: "/getServerSideProps/catch/all",
failOnStatusCode: false,
}).then((response) => {
expect(response.status).to.eq(404);
cy.state("document").write(response.body);
});
cy.get("h2").should("contain", "This page could not be found.");
});
it("loads TV show with one param", () => {
cy.visit("/getServerSideProps/catch/all/1337");
cy.get("h1").should("contain", "Show #1337");
cy.get("p").should("contain", "Whodunnit?");
});
it("loads TV show with multiple params", () => {
cy.visit("/getServerSideProps/catch/all/1337/multiple/params");
cy.get("h1").should("contain", "Show #1337");
cy.get("p").should("contain", "Whodunnit?");
});
it("loads page props from data .json file when navigating to it", () => {
cy.visit("/");
cy.window().then((w) => (w.noReload = true));
// Navigate to page and test that no reload is performed
// See: https://glebbahmutov.com/blog/detect-page-reload/
cy.contains("getServerSideProps/catch/all/1337").click();
cy.get("h1").should("contain", "Show #1337");
cy.get("p").should("contain", "Whodunnit?");
cy.contains("Go back home").click();
cy.contains("getServerSideProps/catch/all/1338").click();
cy.get("h1").should("contain", "Show #1338");
cy.get("p").should("contain", "The Whole Truth");
cy.window().should("have.property", "noReload", true);
});
});
});
describe("getStaticProps", () => {
context("with static route", () => {
it("loads TV show", () => {
cy.visit("/getStaticProps/static");
cy.get("h1").should("contain", "Show #71");
cy.get("p").should("contain", "Dancing with the Stars");
});
it("loads page props from data .json file when navigating to it", () => {
cy.visit("/");
cy.window().then((w) => (w.noReload = true));
// Navigate to page and test that no reload is performed
// See: https://glebbahmutov.com/blog/detect-page-reload/
cy.contains("getStaticProps/static").click();
cy.get("h1").should("contain", "Show #71");
cy.get("p").should("contain", "Dancing with the Stars");
cy.window().should("have.property", "noReload", true);
});
context("with revalidate", () => {
it("loads TV show", () => {
cy.visit("/getStaticProps/with-revalidate");
cy.get("h1").should("contain", "Show #71");
cy.get("p").should("contain", "Dancing with the Stars");
});
it("loads TV shows when SSR-ing", () => {
cy.ssr("/getStaticProps/with-revalidate");
cy.get("h1").should("contain", "Show #71");
cy.get("p").should("contain", "Dancing with the Stars");
});
});
});
context("with dynamic route", () => {
context("without fallback", () => {
it("loads shows 1 and 2", () => {
cy.visit("/getStaticProps/1");
cy.get("h1").should("contain", "Show #1");
cy.get("p").should("contain", "Under the Dome");
cy.visit("/getStaticProps/2");
cy.get("h1").should("contain", "Show #2");
cy.get("p").should("contain", "Person of Interest");
});
it("loads page props from data .json file when navigating to it", () => {
cy.visit("/");
cy.window().then((w) => (w.noReload = true));
// Navigate to page and test that no reload is performed
// See: https://glebbahmutov.com/blog/detect-page-reload/
cy.contains("getStaticProps/1").click();
cy.get("h1").should("contain", "Show #1");
cy.get("p").should("contain", "Under the Dome");
cy.contains("Go back home").click();
cy.contains("getStaticProps/2").click();
cy.get("h1").should("contain", "Show #2");
cy.get("p").should("contain", "Person of Interest");
cy.window().should("have.property", "noReload", true);
});
it("returns 404 when trying to access non-defined path", () => {
cy.request({
url: "/getStaticProps/3",
failOnStatusCode: false,
}).then((response) => {
expect(response.status).to.eq(404);
cy.state("document").write(response.body);
});
cy.get("h2").should("contain", "This page could not be found.");
});
});
context("with fallback", () => {
it("loads pre-rendered TV shows 3 and 4", () => {
cy.visit("/getStaticProps/withFallback/3");
cy.get("h1").should("contain", "Show #3");
cy.get("p").should("contain", "Bitten");
cy.visit("/getStaticProps/withFallback/4");
cy.get("h1").should("contain", "Show #4");
cy.get("p").should("contain", "Arrow");
});
it("loads non-pre-rendered TV show", () => {
cy.visit("/getStaticProps/withFallback/75");
cy.get("h1").should("contain", "Show #75");
cy.get("p").should("contain", "The Mindy Project");
});
it("loads non-pre-rendered TV shows when SSR-ing", () => {
cy.ssr("/getStaticProps/withFallback/75");
cy.get("h1").should("contain", "Show #75");
cy.get("p").should("contain", "The Mindy Project");
});
it("loads page props from data .json file when navigating to it", () => {
cy.visit("/");
cy.window().then((w) => (w.noReload = true));
// Navigate to page and test that no reload is performed
// See: https://glebbahmutov.com/blog/detect-page-reload/
cy.contains("getStaticProps/withFallback/3").click();
cy.get("h1").should("contain", "Show #3");
cy.get("p").should("contain", "Bitten");
cy.contains("Go back home").click();
cy.contains("getStaticProps/withFallback/4").click();
cy.get("h1").should("contain", "Show #4");
cy.get("p").should("contain", "Arrow");
cy.contains("Go back home").click();
cy.contains("getStaticProps/withFallback/75").click();
cy.get("h1").should("contain", "Show #75");
cy.get("p").should("contain", "The Mindy Project");
cy.window().should("have.property", "noReload", true);
});
});
context("with revalidate", () => {
it("loads TV show", () => {
cy.visit("/getStaticProps/withRevalidate/75");
cy.get("h1").should("contain", "Show #75");
cy.get("p").should("contain", "The Mindy Project");
});
it("loads TV shows when SSR-ing", () => {
cy.ssr("/getStaticProps/withRevalidate/75");
cy.get("h1").should("contain", "Show #75");
cy.get("p").should("contain", "The Mindy Project");
});
it("loads page props from data .json file when navigating to it", () => {
cy.visit("/");
cy.window().then((w) => (w.noReload = true));
// Navigate to page and test that no reload is performed
// See: https://glebbahmutov.com/blog/detect-page-reload/
cy.contains("getStaticProps/withRevalidate/3").click();
cy.get("h1").should("contain", "Show #3");
cy.get("p").should("contain", "Bitten");
cy.contains("Go back home").click();
cy.contains("getStaticProps/withRevalidate/4").click();
cy.get("h1").should("contain", "Show #4");
cy.get("p").should("contain", "Arrow");
cy.window().should("have.property", "noReload", true);
});
});
});
context("with catch-all route", () => {
context("with fallback", () => {
it("loads pre-rendered shows 1 and 2", () => {
cy.visit("/getStaticProps/withFallback/my/path/1");
cy.get("h1").should("contain", "Show #1");
cy.get("p").should("contain", "Under the Dome");
cy.visit("/getStaticProps/withFallback/my/path/2");
cy.get("h1").should("contain", "Show #2");
cy.get("p").should("contain", "Person of Interest");
});
it("loads non-pre-rendered TV show", () => {
cy.visit("/getStaticProps/withFallback/undefined/catch/all/path/75");
cy.get("h1").should("contain", "Show #75");
cy.get("p").should("contain", "The Mindy Project");
});
it("loads page props from data .json file when navigating to it", () => {
cy.visit("/");
cy.window().then((w) => (w.noReload = true));
// Navigate to page and test that no reload is performed
// See: https://glebbahmutov.com/blog/detect-page-reload/
cy.contains("getStaticProps/withFallback/my/path/1").click();
cy.get("h1").should("contain", "Show #1");
cy.get("p").should("contain", "Under the Dome");
cy.contains("Go back home").click();
cy.contains("getStaticProps/withFallback/my/path/2").click();
cy.get("h1").should("contain", "Show #2");
cy.get("p").should("contain", "Person of Interest");
cy.contains("Go back home").click();
cy.contains("getStaticProps/withFallback/my/undefined/path/75").click();
cy.get("h1").should("contain", "Show #75");
cy.get("p").should("contain", "The Mindy Project");
cy.window().should("have.property", "noReload", true);
});
});
});
});
describe("API endpoint", () => {
context("with static route", () => {
it("returns hello world, with all response headers", () => {
cy.request("/api/static").then((response) => {
expect(response.headers["content-type"]).to.include("application/json");
expect(response.headers["my-custom-header"]).to.include("header123");
expect(response.body).to.have.property("message", "hello world :)");
});
});
});
context("with dynamic route", () => {
it("returns TV show", () => {
cy.request("/api/shows/305").then((response) => {
expect(response.headers["content-type"]).to.include("application/json");
expect(response.body).to.have.property("show");
expect(response.body.show).to.have.property("id", 305);
expect(response.body.show).to.have.property("name", "Black Mirror");
});
});
});
context("with catch-all route", () => {
it("returns all URL paremeters, including query string parameters", () => {
cy.request("/api/shows/590/this/path/is/captured?metric=dog&p2=cat").then(
(response) => {
expect(response.headers["content-type"]).to.include(
"application/json"
);
// Params
expect(response.body).to.have.property("params");
expect(response.body.params).to.deep.eq([
"590",
"this",
"path",
"is",
"captured",
]);
// Query string parameters
expect(response.body).to.have.property("queryStringParams");
expect(response.body.queryStringParams).to.deep.eq({
metric: "dog",
p2: "cat",
});
// Show
expect(response.body).to.have.property("show");
expect(response.body.show).to.have.property("id", 590);
expect(response.body.show).to.have.property("name", "Pokémon");
}
);
});
});
it("redirects with res.redirect", () => {
cy.visit("/api/redirect?to=999");
cy.url().should("include", "/shows/999");
cy.get("h1").should("contain", "Show #999");
cy.get("p").should("contain", "Flash Gordon");
});
});
describe("Preview Mode", () => {
it("redirects to preview test page with dynamic route", () => {
cy.visit("/api/enterPreview?id=999");
cy.url().should("include", "/previewTest/999");
});
it("redirects to static preview test page", () => {
cy.visit("/api/enterPreviewStatic");
cy.url().should("include", "/previewTest/static");
});
it("sets cookies on client", () => {
Cypress.Cookies.debug(true);
cy.getCookie("__prerender_bypass").should("not.exist");
cy.getCookie("__next_preview_data").should("not.exist");
cy.visit("/api/enterPreview?id=999");
cy.getCookie("__prerender_bypass").should("not.be", null);
cy.getCookie("__next_preview_data").should("not.be", null);
});
it("sets cookies on client with static redirect", () => {
Cypress.Cookies.debug(true);
cy.getCookie("__prerender_bypass").should("not.exist");
cy.getCookie("__next_preview_data").should("not.exist");
cy.visit("/api/enterPreviewStatic");
cy.getCookie("__prerender_bypass").should("not.be", null);
cy.getCookie("__next_preview_data").should("not.be", null);
});
it("renders serverSideProps page in preview mode", () => {
cy.visit("/api/enterPreview?id=999");
if (Cypress.env("DEPLOY") === "local") {
cy.makeCookiesWorkWithHttpAndReload();
}
cy.get("h1").should("contain", "Person #999");
cy.get("p").should("contain", "Sebastian Lacause");
});
it("renders staticProps page in preview mode", () => {
// cypress local (aka netlify dev) doesn't support cookie-based redirects
if (Cypress.env("DEPLOY") !== "local") {
cy.visit("/api/enterPreviewStatic");
cy.get("h1").should("contain", "Number: 3");
}
});
it("can move in and out of preview mode for SSRed page", () => {
cy.visit("/api/enterPreview?id=999");
if (Cypress.env("DEPLOY") === "local") {
cy.makeCookiesWorkWithHttpAndReload();
}
cy.get("h1").should("contain", "Person #999");
cy.get("p").should("contain", "Sebastian Lacause");
cy.contains("Go back home").click();
// Verify that we're still in preview mode
cy.contains("previewTest/222").click();
cy.get("h1").should("contain", "Person #222");
cy.get("p").should("contain", "Corey Lof");
// Exit preview mode
cy.visit("/api/exitPreview");
// Verify that we're no longer in preview mode
cy.contains("previewTest/222").click();
cy.get("h1").should("contain", "Show #222");
cy.get("p").should("contain", "Happyland");
});
it("can move in and out of preview mode for static page", () => {
if (Cypress.env("DEPLOY") !== "local") {
cy.visit("/api/enterPreviewStatic");
cy.window().then((w) => (w.noReload = true));
cy.get("h1").should("contain", "Number: 3");
cy.contains("Go back home").click();
// Verify that we're still in preview mode
cy.contains("previewTest/static").click();
cy.get("h1").should("contain", "Number: 3");
cy.window().should("have.property", "noReload", true);
// Exit preview mode
cy.visit("/api/exitPreview");
// TO-DO: test if this is the static html?
// Verify that we're no longer in preview mode
cy.contains("previewTest/static").click();
cy.get("h1").should("contain", "Number: 4");
}
});
it("hits the prerendered html out of preview mode and netlify function in preview mode", () => {
if (Cypress.env("DEPLOY") !== "local") {
cy.request("/previewTest/static").then((response) => {
expect(response.headers["cache-control"]).to.include("public");
});
cy.visit("/api/enterPreviewStatic");
cy.request("/previewTest/static").then((response) => {
expect(response.headers["cache-control"]).to.include("private");
});
}
});
});
describe("pre-rendered HTML pages", () => {
context("with static route", () => {
it("renders", () => {
cy.visit("/static");
cy.get("p").should("contain", "It is a static page.");
});
it("renders when SSR-ing", () => {
cy.visit("/static");
cy.get("p").should("contain", "It is a static page.");
});
});
context("with dynamic route", () => {
it("renders", () => {
cy.visit("/static/superdynamic");
cy.get("p").should("contain", "It is a static page.");
cy.get("p").should(
"contain",
"it has a dynamic URL parameter: /static/:id."
);
});
it("renders when SSR-ing", () => {
cy.visit("/static/superdynamic");
cy.get("p").should("contain", "It is a static page.");
cy.get("p").should(
"contain",
"it has a dynamic URL parameter: /static/:id."
);
});
});
});
describe("404 page", () => {
it("renders", () => {
cy.request({
url: "/this-page-does-not-exist",
failOnStatusCode: false,
}).then((response) => {
expect(response.status).to.eq(404);
cy.state("document").write(response.body);
});
cy.get("h2").should("contain", "This page could not be found.");
});
});