Skip to content
This repository was archived by the owner on Mar 28, 2022. It is now read-only.

Commit 88e2c33

Browse files
committed
Add e2e test for wepage guides examples
1 parent e0dfc33 commit 88e2c33

4 files changed

Lines changed: 207 additions & 0 deletions

File tree

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
Copyright 2019 Javier Brea
3+
4+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
5+
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
9+
*/
10+
11+
const { startServer, stopServer, request } = require("./utils");
12+
13+
describe("deprecated API for changing current behavior", () => {
14+
let server;
15+
16+
beforeAll(async () => {
17+
server = await startServer("web-tutorial-json");
18+
});
19+
20+
afterAll(() => {
21+
stopServer(server);
22+
});
23+
24+
describe("When started", () => {
25+
it("should have 3 behaviors available", async () => {
26+
const behaviors = await request("/admin/behaviors");
27+
expect(behaviors.length).toEqual(3);
28+
});
29+
30+
it("should serve users collection mock under the /api/users path", async () => {
31+
const users = await request("/api/users");
32+
expect(users).toEqual([
33+
{ id: 1, name: "John Doe" },
34+
{ id: 2, name: "Jane Doe" }
35+
]);
36+
});
37+
38+
it("should serve user 1 under the /api/users/1 path", async () => {
39+
const users = await request("/api/users/1");
40+
expect(users).toEqual({ id: 1, name: "John Doe" });
41+
});
42+
43+
it("should serve user 1 under the /api/users/2 path", async () => {
44+
const users = await request("/api/users/2");
45+
expect(users).toEqual({ id: 1, name: "John Doe" });
46+
});
47+
});
48+
49+
describe('When changing current behavior to "user2"', () => {
50+
beforeAll(async () => {
51+
await request("/admin/settings", {
52+
method: "PATCH",
53+
body: {
54+
behavior: "user2"
55+
}
56+
});
57+
});
58+
59+
it("should serve users collection mock under the /api/users path", async () => {
60+
const users = await request("/api/users");
61+
expect(users).toEqual([
62+
{ id: 1, name: "John Doe" },
63+
{ id: 2, name: "Jane Doe" }
64+
]);
65+
});
66+
67+
it("should serve user 2 under the /api/users/1 path", async () => {
68+
const users = await request("/api/users/1");
69+
expect(users).toEqual({ id: 2, name: "Jane Doe" });
70+
});
71+
72+
it("should serve user 2 under the /api/users/2 path", async () => {
73+
const users = await request("/api/users/2");
74+
expect(users).toEqual({ id: 2, name: "Jane Doe" });
75+
});
76+
});
77+
78+
describe('When changing current behavior to "dynamic"', () => {
79+
beforeAll(async () => {
80+
await request("/admin/settings", {
81+
method: "PATCH",
82+
body: {
83+
behavior: "dynamic"
84+
}
85+
});
86+
});
87+
88+
it("should serve users collection mock under the /api/users path", async () => {
89+
const users = await request("/api/users");
90+
expect(users).toEqual([
91+
{ id: 1, name: "John Doe" },
92+
{ id: 2, name: "Jane Doe" }
93+
]);
94+
});
95+
96+
it("should serve user 1 under the /api/users/1 path", async () => {
97+
const users = await request("/api/users/1");
98+
expect(users).toEqual({ id: 1, name: "John Doe" });
99+
});
100+
101+
it("should serve user 2 under the /api/users/2 path", async () => {
102+
const users = await request("/api/users/2");
103+
expect(users).toEqual({ id: 2, name: "Jane Doe" });
104+
});
105+
106+
it("should return not found for /api/users/3 path", async () => {
107+
const usersResponse = await request("/api/users/3", {
108+
resolveWithFullResponse: true,
109+
simple: false
110+
});
111+
expect(usersResponse.statusCode).toEqual(404);
112+
});
113+
});
114+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[
2+
{
3+
"id": "standard",
4+
"fixtures": ["get-users", "get-user"]
5+
},
6+
{
7+
"id": "user2",
8+
"from": "standard",
9+
"fixtures": ["get-user-2"]
10+
},
11+
{
12+
"id": "dynamic",
13+
"from": "standard",
14+
"fixtures": ["get-user-real"]
15+
}
16+
]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const INITIAL_USERS = [
2+
{
3+
id: 1,
4+
name: "John Doe"
5+
},
6+
{
7+
id: 2,
8+
name: "Jane Doe"
9+
}
10+
];
11+
12+
const getUserReal = {
13+
id: "get-user-real",
14+
url: "/api/users/:id",
15+
method: "GET",
16+
response: (req, res) => {
17+
const userId = req.params.id;
18+
const user = INITIAL_USERS.find(userData => userData.id === Number(userId));
19+
if (user) {
20+
res.status(200);
21+
res.send(user);
22+
} else {
23+
res.status(404);
24+
res.send({
25+
message: "User not found"
26+
});
27+
}
28+
}
29+
};
30+
31+
module.exports = {
32+
getUserReal
33+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
[
2+
{
3+
"id": "get-users",
4+
"url": "/api/users",
5+
"method": "GET",
6+
"response": {
7+
"status": 200,
8+
"body": [
9+
{
10+
"id": 1,
11+
"name": "John Doe"
12+
},
13+
{
14+
"id": 2,
15+
"name": "Jane Doe"
16+
}
17+
]
18+
}
19+
},
20+
{
21+
"id": "get-user",
22+
"url": "/api/users/:id",
23+
"method": "GET",
24+
"response": {
25+
"status": 200,
26+
"body": {
27+
"id": 1,
28+
"name": "John Doe"
29+
}
30+
}
31+
},
32+
{
33+
"id": "get-user-2",
34+
"url": "/api/users/:id",
35+
"method": "GET",
36+
"response": {
37+
"status": 200,
38+
"body": {
39+
"id": 2,
40+
"name": "Jane Doe"
41+
}
42+
}
43+
}
44+
]

0 commit comments

Comments
 (0)