|
| 1 | +/* |
| 2 | +Copyright 2020 Javier Brea |
| 3 | +Copyright 2019 XbyOrange |
| 4 | +
|
| 5 | +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 |
| 6 | +
|
| 7 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +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. |
| 10 | +*/ |
| 11 | + |
| 12 | +const express = require("express"); |
| 13 | +const sinon = require("sinon"); |
| 14 | +const Boom = require("@hapi/boom"); |
| 15 | + |
| 16 | +const LibMocks = require("../Libs.mocks"); |
| 17 | +const CoreMocks = require("../Core.mocks"); |
| 18 | + |
| 19 | +const Alerts = require("../../../src/Alerts"); |
| 20 | + |
| 21 | +describe("Alerts", () => { |
| 22 | + let sandbox; |
| 23 | + let libMocks; |
| 24 | + let coreMock; |
| 25 | + let coreInstance; |
| 26 | + let resMock; |
| 27 | + let alerts; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + sandbox = sinon.createSandbox(); |
| 31 | + resMock = { |
| 32 | + status: sandbox.stub(), |
| 33 | + send: sandbox.stub(), |
| 34 | + }; |
| 35 | + libMocks = new LibMocks(); |
| 36 | + coreMock = new CoreMocks(); |
| 37 | + coreInstance = coreMock.stubs.instance; |
| 38 | + alerts = new Alerts(coreInstance); |
| 39 | + expect.assertions(1); |
| 40 | + }); |
| 41 | + |
| 42 | + afterEach(() => { |
| 43 | + sandbox.restore(); |
| 44 | + libMocks.restore(); |
| 45 | + coreMock.restore(); |
| 46 | + }); |
| 47 | + |
| 48 | + describe("when created", () => { |
| 49 | + it("should create an express Router", async () => { |
| 50 | + expect(express.Router.calledOnce).toEqual(true); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should have added get router at /", async () => { |
| 54 | + expect(libMocks.stubs.express.get.getCall(0).args[0]).toEqual("/"); |
| 55 | + }); |
| 56 | + |
| 57 | + it("should register a get router for alert ids", async () => { |
| 58 | + expect(libMocks.stubs.express.get.getCall(1).args[0]).toEqual("/:id"); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe("getCollection router", () => { |
| 63 | + it("should return the alerts parsed", () => { |
| 64 | + expect.assertions(6); |
| 65 | + coreInstance.alerts = [ |
| 66 | + { |
| 67 | + context: "plugins:foo:start", |
| 68 | + message: "Foo warning starting plugin", |
| 69 | + }, |
| 70 | + { |
| 71 | + context: "plugins:foo2:stop", |
| 72 | + message: "Foo error stopping plugin 2", |
| 73 | + error: new Error("Foo error message"), |
| 74 | + }, |
| 75 | + ]; |
| 76 | + alerts = new Alerts(coreInstance); |
| 77 | + alerts.getCollection({}, resMock); |
| 78 | + expect(resMock.send.getCall(0).args[0][0]).toEqual({ |
| 79 | + id: "plugins:foo:start", |
| 80 | + context: "plugins:foo:start", |
| 81 | + message: "Foo warning starting plugin", |
| 82 | + error: null, |
| 83 | + }); |
| 84 | + expect(resMock.send.getCall(0).args[0][1].id).toEqual("plugins:foo2:stop"); |
| 85 | + expect(resMock.send.getCall(0).args[0][1].context).toEqual("plugins:foo2:stop"); |
| 86 | + expect(resMock.send.getCall(0).args[0][1].message).toEqual("Foo error stopping plugin 2"); |
| 87 | + expect(resMock.send.getCall(0).args[0][1].error.message).toEqual("Foo error message"); |
| 88 | + expect(resMock.send.getCall(0).args[0][1].error.stack).toEqual( |
| 89 | + expect.stringContaining("test/unit/src/Alerts.spec.js:73:18") |
| 90 | + ); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + describe("getModel router", () => { |
| 95 | + it("should return the requested alert model parsed", () => { |
| 96 | + coreInstance.alerts = [ |
| 97 | + { |
| 98 | + context: "plugins:foo:start", |
| 99 | + message: "Foo warning starting plugin", |
| 100 | + }, |
| 101 | + { |
| 102 | + context: "plugins:foo2:stop", |
| 103 | + message: "Foo error stopping plugin 2", |
| 104 | + error: new Error("Foo error message"), |
| 105 | + }, |
| 106 | + ]; |
| 107 | + alerts = new Alerts(coreInstance); |
| 108 | + alerts.getModel( |
| 109 | + { |
| 110 | + params: { |
| 111 | + id: "plugins:foo:start", |
| 112 | + }, |
| 113 | + }, |
| 114 | + resMock |
| 115 | + ); |
| 116 | + expect(resMock.send.getCall(0).args[0]).toEqual({ |
| 117 | + id: "plugins:foo:start", |
| 118 | + context: "plugins:foo:start", |
| 119 | + message: "Foo warning starting plugin", |
| 120 | + error: null, |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + it("should return a not found error if alert is not found", () => { |
| 125 | + const nextStub = sandbox.stub(); |
| 126 | + sandbox.stub(Boom, "notFound").returns("foo-error"); |
| 127 | + coreInstance.alerts = [ |
| 128 | + { |
| 129 | + context: "plugins:foo:start", |
| 130 | + message: "Foo warning starting plugin", |
| 131 | + }, |
| 132 | + ]; |
| 133 | + alerts = new Alerts(coreInstance); |
| 134 | + alerts.getModel( |
| 135 | + { |
| 136 | + params: { |
| 137 | + id: "foo", |
| 138 | + }, |
| 139 | + }, |
| 140 | + resMock, |
| 141 | + nextStub |
| 142 | + ); |
| 143 | + expect(nextStub.getCall(0).args[0]).toEqual("foo-error"); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + describe("router getter", () => { |
| 148 | + it("should return express created router", async () => { |
| 149 | + expect(alerts.router).toEqual(libMocks.stubs.express); |
| 150 | + }); |
| 151 | + }); |
| 152 | +}); |
0 commit comments