|
| 1 | +/* |
| 2 | +Copyright 2020 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 | +const path = require("path"); |
| 11 | +const fsExtra = require("fs-extra"); |
| 12 | + |
| 13 | +const { CliRunner, request, fixturesFolder, wait } = require("./support/utils"); |
| 14 | + |
| 15 | +describe("alerts api", () => { |
| 16 | + let cli; |
| 17 | + beforeAll(async () => { |
| 18 | + fsExtra.removeSync(fixturesFolder("files-watch")); |
| 19 | + fsExtra.copySync(fixturesFolder("web-tutorial"), fixturesFolder("files-watch")); |
| 20 | + cli = new CliRunner(["node", "start.js", "--path=files-watch", "--behavior=foo"], { |
| 21 | + cwd: path.resolve(__dirname, "fixtures"), |
| 22 | + }); |
| 23 | + await wait(1000); |
| 24 | + }); |
| 25 | + |
| 26 | + afterAll(async () => { |
| 27 | + await cli.kill(); |
| 28 | + }); |
| 29 | + |
| 30 | + describe("when started", () => { |
| 31 | + it("should return behavior not found alert", async () => { |
| 32 | + const response = await request("/admin/alerts"); |
| 33 | + expect(response.length).toEqual(1); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should return specific alert when requested by id", async () => { |
| 37 | + const response = await request("/admin/alerts/mocks%3Abehaviors%3Acurrent"); |
| 38 | + expect(response).toEqual({ |
| 39 | + id: "mocks:behaviors:current", |
| 40 | + context: "mocks:behaviors:current", |
| 41 | + message: 'Defined behavior "foo" was not found. The first one found was used instead', |
| 42 | + error: null, |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + it("should serve users collection mock under the /api/users path", async () => { |
| 47 | + const users = await request("/api/users"); |
| 48 | + expect(users).toEqual([ |
| 49 | + { id: 1, name: "John Doe" }, |
| 50 | + { id: 2, name: "Jane Doe" }, |
| 51 | + ]); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe("when behavior is modified", () => { |
| 56 | + beforeAll(async () => { |
| 57 | + await request("/admin/settings", { |
| 58 | + method: "PATCH", |
| 59 | + body: { |
| 60 | + behavior: "dynamic", |
| 61 | + }, |
| 62 | + }); |
| 63 | + await wait(); |
| 64 | + }, 10000); |
| 65 | + |
| 66 | + it("should return no alerts", async () => { |
| 67 | + const response = await request("/admin/alerts"); |
| 68 | + expect(response.length).toEqual(0); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe("when files contain an error", () => { |
| 73 | + beforeAll(async () => { |
| 74 | + fsExtra.copySync(fixturesFolder("files-error"), fixturesFolder("files-watch")); |
| 75 | + await wait(6000); |
| 76 | + }, 10000); |
| 77 | + |
| 78 | + it("should return one alert", async () => { |
| 79 | + const response = await request("/admin/alerts"); |
| 80 | + expect(response.length).toEqual(1); |
| 81 | + }); |
| 82 | + |
| 83 | + it("should return specific alert when requested by id", async () => { |
| 84 | + const response = await request( |
| 85 | + "/admin/alerts/plugins%3A%40mocks-server%2Fcore%2Fplugin-files-loader%3Aload" |
| 86 | + ); |
| 87 | + expect(response.id).toEqual("plugins:@mocks-server/core/plugin-files-loader:load"); |
| 88 | + expect(response.message).toEqual(expect.stringContaining("test/e2e/fixtures/files-watch")); |
| 89 | + expect(response.error.name).toEqual("ReferenceError"); |
| 90 | + expect(response.error.message).toEqual("FOO is not defined"); |
| 91 | + expect(response.error.stack).toEqual( |
| 92 | + expect.stringContaining("test/e2e/fixtures/files-watch/fixtures/users.js:1:18") |
| 93 | + ); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe("when files error is fixed", () => { |
| 98 | + beforeAll(async () => { |
| 99 | + fsExtra.copySync(fixturesFolder("web-tutorial"), fixturesFolder("files-watch")); |
| 100 | + await wait(6000); |
| 101 | + }, 10000); |
| 102 | + |
| 103 | + it("should return no alerts", async () => { |
| 104 | + const response = await request("/admin/alerts"); |
| 105 | + expect(response.length).toEqual(0); |
| 106 | + }); |
| 107 | + |
| 108 | + it("should serve users collection mock under the /api/users path", async () => { |
| 109 | + const users = await request("/api/users"); |
| 110 | + expect(users).toEqual([ |
| 111 | + { id: 1, name: "John Doe" }, |
| 112 | + { id: 2, name: "Jane Doe" }, |
| 113 | + ]); |
| 114 | + }); |
| 115 | + }); |
| 116 | +}); |
0 commit comments