|
| 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 | +}); |
0 commit comments