|
| 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("fixtures api", () => { |
| 14 | + let server; |
| 15 | + beforeAll(async () => { |
| 16 | + server = await startServer("web-tutorial"); |
| 17 | + }); |
| 18 | + |
| 19 | + afterAll(() => { |
| 20 | + stopServer(server); |
| 21 | + }); |
| 22 | + |
| 23 | + describe("get /", () => { |
| 24 | + it("should return current fixtures", async () => { |
| 25 | + const response = await request("/admin/fixtures"); |
| 26 | + expect(response).toEqual([ |
| 27 | + { |
| 28 | + id: "e82af88532da929b0592925899eb056e", |
| 29 | + requestMatchId: "9989c8c9766561cd432c625deabca48b", |
| 30 | + handler: "mocks-server-fixture", |
| 31 | + request: { url: "/api/users/:id", method: "GET" }, |
| 32 | + response: { |
| 33 | + type: "dynamic", |
| 34 | + function: |
| 35 | + '(req, res) => {\n const userId = req.params.id;\n const user = INITIAL_USERS.find(userData => userData.id === Number(userId));\n\n if (user) {\n res.status(200);\n res.send(user);\n } else {\n res.status(404);\n res.send({\n message: "User not found"\n });\n }\n }' |
| 36 | + } |
| 37 | + }, |
| 38 | + { |
| 39 | + id: "bd5292849ee3fda9fa8383837bb908e7", |
| 40 | + requestMatchId: "9989c8c9766561cd432c625deabca48b", |
| 41 | + handler: "mocks-server-fixture", |
| 42 | + request: { url: "/api/users/:id", method: "GET" }, |
| 43 | + response: { type: "static", status: 200, body: { id: 2, name: "Jane Doe" } } |
| 44 | + }, |
| 45 | + { |
| 46 | + id: "12e5f429b92f67d4ec2bf90940ec1135", |
| 47 | + requestMatchId: "9989c8c9766561cd432c625deabca48b", |
| 48 | + handler: "mocks-server-fixture", |
| 49 | + request: { url: "/api/users/:id", method: "GET" }, |
| 50 | + response: { type: "static", status: 200, body: { id: 1, name: "John Doe" } } |
| 51 | + }, |
| 52 | + { |
| 53 | + id: "0dbc954f9d9c9f3f7996c60e63384c9e", |
| 54 | + requestMatchId: "8b4d07b38f9320be1702c093fd1daa76", |
| 55 | + handler: "mocks-server-fixture", |
| 56 | + request: { url: "/api/users", method: "GET" }, |
| 57 | + response: { |
| 58 | + type: "static", |
| 59 | + status: 200, |
| 60 | + body: [ |
| 61 | + { id: 1, name: "John Doe" }, |
| 62 | + { id: 2, name: "Jane Doe" } |
| 63 | + ] |
| 64 | + } |
| 65 | + } |
| 66 | + ]); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe("get /e82af88532da929b0592925899eb056e", () => { |
| 71 | + it("should return fixture with id e82af88532da929b0592925899eb056e", async () => { |
| 72 | + const response = await request("/admin/fixtures/e82af88532da929b0592925899eb056e"); |
| 73 | + expect(response).toEqual({ |
| 74 | + id: "e82af88532da929b0592925899eb056e", |
| 75 | + requestMatchId: "9989c8c9766561cd432c625deabca48b", |
| 76 | + handler: "mocks-server-fixture", |
| 77 | + request: { url: "/api/users/:id", method: "GET" }, |
| 78 | + response: { |
| 79 | + type: "dynamic", |
| 80 | + function: |
| 81 | + '(req, res) => {\n const userId = req.params.id;\n const user = INITIAL_USERS.find(userData => userData.id === Number(userId));\n\n if (user) {\n res.status(200);\n res.send(user);\n } else {\n res.status(404);\n res.send({\n message: "User not found"\n });\n }\n }' |
| 82 | + } |
| 83 | + }); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe("get /bd5292849ee3fda9fa8383837bb908e7", () => { |
| 88 | + it("should return fixture with id bd5292849ee3fda9fa8383837bb908e7", async () => { |
| 89 | + const response = await request("/admin/fixtures/bd5292849ee3fda9fa8383837bb908e7"); |
| 90 | + expect(response).toEqual({ |
| 91 | + id: "bd5292849ee3fda9fa8383837bb908e7", |
| 92 | + requestMatchId: "9989c8c9766561cd432c625deabca48b", |
| 93 | + handler: "mocks-server-fixture", |
| 94 | + request: { url: "/api/users/:id", method: "GET" }, |
| 95 | + response: { type: "static", status: 200, body: { id: 2, name: "Jane Doe" } } |
| 96 | + }); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe("get unexistant fixture", () => { |
| 101 | + it("should return a not found error", async () => { |
| 102 | + const response = await request("/admin/fixtures/foo", { |
| 103 | + resolveWithFullResponse: true, |
| 104 | + simple: false |
| 105 | + }); |
| 106 | + expect(response.statusCode).toEqual(404); |
| 107 | + expect(response.body.message).toEqual('Fixture with id "foo" was not found'); |
| 108 | + }); |
| 109 | + }); |
| 110 | +}); |
0 commit comments