-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdemo.spec.js
128 lines (106 loc) · 3.49 KB
/
demo.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
context("Demo", () => {
/**
For demo purpose, all tests are in the same file. Feel free to dispatch
the following contexts in multiple *.spec.js files.
*/
context("Visit the homepage", () => {
beforeEach(() => {
cy.visit("/");
});
it("display the content related to this URL", () => {
/**
The Elm app uses data-* attributes to provide context to the selectors
and insulate them from CSS or JS changes.
https://docs.cypress.io/guides/references/best-practices.html
*/
cy.get("[data-test=content]").contains("Elm Batteries Included");
});
context("Open the demo page from the menu", () => {
beforeEach(() => {
cy.get("[data-test=menu]")
.contains("Demo")
.click();
});
it("change the URL", () => {
cy.url().should("eq", Cypress.config().baseUrl + "/demo");
});
it("update the page with new content", () => {
cy.get("[data-test=content]").contains("Demo");
});
it("go back to the homepage", () => {
cy.get("[data-test=logo]").click();
cy.get("[data-test=content]").contains("Elm Batteries Included");
});
});
});
context("Visit a page that doesn't exist", () => {
beforeEach(() => {
cy.visit("/xyz");
});
it("display the not found page", () => {
cy.get("[data-test=content]").contains(
"Sorry, we could not find this page."
);
});
});
context("Visit the demo page", () => {
beforeEach(() => {
cy.visit("/demo");
cy.get("[data-test=package]").should(
"have.attr",
"data-result",
"not-asked"
);
});
it("display the content related to this URL", () => {
cy.get("[data-test=content]").contains("Demo");
});
it("decode and show the response", () => {
cy.server();
// We stub the call with another author
cy.route("/.netlify/functions/demo", {
name: "elm-batteries",
url: "https://elm-batteries.netlify.com",
author: "John Doe",
license: "MIT"
}).as("demo");
cy.get("[data-action=fetch-package]").click();
cy.wait("@demo");
cy.get("[data-value=author]").contains("John");
});
it("show an error if the response is invalid", () => {
cy.server();
// We stub the call with an invalid empty response
cy.route("/.netlify/functions/demo", {}).as("demo");
cy.get("[data-action=fetch-package]").click();
cy.wait("@demo");
cy.get("[data-test=package]").should("have.attr", "data-result", "error");
});
/**
The request in the following test is not stubbed: it guarantees that the
contract between your Elm app and Netlify is working correctly.
https://docs.cypress.io/guides/guides/network-requests.html#Testing-Strategies
*/
it("reach Netlify and retrieve data", () => {
cy.server();
cy.route("/.netlify/functions/demo").as("demo");
cy.get("[data-action=fetch-package]").click();
cy.get("[data-test=package]").should(
"have.attr",
"data-result",
"loading"
);
cy.wait("@demo")
.its("status")
.should("equal", 200);
cy.get("[data-test=package]").should(
"have.attr",
"data-result",
"success"
);
cy.get("[data-value=name]").contains("elm-batteries");
cy.get("[data-value=author]").contains("Cédric Soulas");
cy.get("[data-value=license]").contains("MIT");
});
});
});