|
| 1 | +/* |
| 2 | +Copyright 2019 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 sinon = require("sinon"); |
| 13 | + |
| 14 | +const SettingsMocks = require("./settings/Settings.mocks.js"); |
| 15 | +const MocksMocks = require("./mocks/Mocks.mocks.js"); |
| 16 | +const ServerMocks = require("./server/Server.mocks.js"); |
| 17 | +const PluginsMocks = require("./Plugins.mocks.js"); |
| 18 | + |
| 19 | +const Core = require("../../../lib/core/Core"); |
| 20 | +const tracer = require("../../../lib/core/tracer"); |
| 21 | + |
| 22 | +describe("Settings", () => { |
| 23 | + let sandbox; |
| 24 | + let settingsMocks; |
| 25 | + let settingsInstance; |
| 26 | + let mocksMocks; |
| 27 | + let mocksInstance; |
| 28 | + let serverMocks; |
| 29 | + let serverInstance; |
| 30 | + let pluginsMocks; |
| 31 | + let pluginsInstance; |
| 32 | + let core; |
| 33 | + |
| 34 | + beforeEach(async () => { |
| 35 | + sandbox = sinon.createSandbox(); |
| 36 | + settingsMocks = new SettingsMocks(); |
| 37 | + settingsInstance = settingsMocks.stubs.instance; |
| 38 | + mocksMocks = new MocksMocks(); |
| 39 | + mocksInstance = mocksMocks.stubs.instance; |
| 40 | + serverMocks = new ServerMocks(); |
| 41 | + serverInstance = serverMocks.stubs.instance; |
| 42 | + pluginsMocks = new PluginsMocks(); |
| 43 | + pluginsInstance = pluginsMocks.stubs.instance; |
| 44 | + |
| 45 | + core = new Core(); |
| 46 | + await core.init(); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + sandbox.restore(); |
| 51 | + settingsMocks.restore(); |
| 52 | + mocksMocks.restore(); |
| 53 | + serverMocks.restore(); |
| 54 | + pluginsMocks.restore(); |
| 55 | + }); |
| 56 | + |
| 57 | + describe("init method", () => { |
| 58 | + it("should init only once", async () => { |
| 59 | + await core.init(); |
| 60 | + await core.init(); |
| 61 | + expect(pluginsInstance.register.callCount).toEqual(1); |
| 62 | + }); |
| 63 | + |
| 64 | + it("should register plugins", () => { |
| 65 | + expect(pluginsInstance.register.callCount).toEqual(1); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should init settings with received options", async () => { |
| 69 | + const fooOptions = { |
| 70 | + foo: "foo" |
| 71 | + }; |
| 72 | + core = new Core(); |
| 73 | + await core.init(fooOptions); |
| 74 | + expect(settingsInstance.init.calledWith(fooOptions)).toEqual(true); |
| 75 | + }); |
| 76 | + |
| 77 | + it("should init mocks", () => { |
| 78 | + expect(mocksInstance.init.callCount).toEqual(1); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should init server", () => { |
| 82 | + expect(serverInstance.init.callCount).toEqual(1); |
| 83 | + }); |
| 84 | + |
| 85 | + it("should init plugins", () => { |
| 86 | + expect(pluginsInstance.init.callCount).toEqual(1); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe("start method", () => { |
| 91 | + it("should init if it has not been done before", async () => { |
| 92 | + pluginsMocks.reset(); |
| 93 | + core = new Core(); |
| 94 | + await core.start(); |
| 95 | + expect(pluginsInstance.register.callCount).toEqual(1); |
| 96 | + }); |
| 97 | + |
| 98 | + it("should not init if it has been done before", async () => { |
| 99 | + await core.start(); |
| 100 | + expect(pluginsInstance.register.callCount).toEqual(1); |
| 101 | + }); |
| 102 | + |
| 103 | + it("should start mocks", async () => { |
| 104 | + await core.start(); |
| 105 | + expect(mocksInstance.start.callCount).toEqual(1); |
| 106 | + }); |
| 107 | + |
| 108 | + it("should start server", async () => { |
| 109 | + await core.start(); |
| 110 | + expect(serverInstance.start.callCount).toEqual(1); |
| 111 | + }); |
| 112 | + |
| 113 | + it("should start plugins", async () => { |
| 114 | + await core.start(); |
| 115 | + expect(pluginsInstance.start.callCount).toEqual(1); |
| 116 | + }); |
| 117 | + |
| 118 | + it("should start plugins only once", async () => { |
| 119 | + core.start(); |
| 120 | + core.start(); |
| 121 | + core.start(); |
| 122 | + await core.start(); |
| 123 | + expect(pluginsInstance.start.callCount).toEqual(1); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + describe("addCustomRouter method", () => { |
| 128 | + it("should add custom router to server", () => { |
| 129 | + core.addCustomRouter(); |
| 130 | + expect(serverInstance.addCustomRouter.callCount).toEqual(1); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + describe("addCustomSetting method", () => { |
| 135 | + it("should add custom setting to settings", () => { |
| 136 | + core.addCustomSetting(); |
| 137 | + expect(settingsInstance.addCustom.callCount).toEqual(1); |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + describe("onLoadMocks method", () => { |
| 142 | + it("should add listener to eventEmitter", () => { |
| 143 | + const spy = sandbox.spy(); |
| 144 | + core.onLoadMocks(spy); |
| 145 | + core._eventEmitter.emit("load:mocks"); |
| 146 | + expect(spy.callCount).toEqual(1); |
| 147 | + }); |
| 148 | + |
| 149 | + it("should return a function to remove listener", () => { |
| 150 | + expect.assertions(2); |
| 151 | + const spy = sandbox.spy(); |
| 152 | + const removeCallback = core.onLoadMocks(spy); |
| 153 | + core._eventEmitter.emit("load:mocks"); |
| 154 | + expect(spy.callCount).toEqual(1); |
| 155 | + removeCallback(); |
| 156 | + core._eventEmitter.emit("load:mocks"); |
| 157 | + expect(spy.callCount).toEqual(1); |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + describe("onChangeSettings method", () => { |
| 162 | + it("should add listener to eventEmitter", () => { |
| 163 | + const spy = sandbox.spy(); |
| 164 | + core.onChangeSettings(spy); |
| 165 | + core._eventEmitter.emit("change:settings"); |
| 166 | + expect(spy.callCount).toEqual(1); |
| 167 | + }); |
| 168 | + |
| 169 | + it("should return a function to remove listener", () => { |
| 170 | + expect.assertions(2); |
| 171 | + const spy = sandbox.spy(); |
| 172 | + const removeCallback = core.onChangeSettings(spy); |
| 173 | + core._eventEmitter.emit("change:settings"); |
| 174 | + expect(spy.callCount).toEqual(1); |
| 175 | + removeCallback(); |
| 176 | + core._eventEmitter.emit("change:settings"); |
| 177 | + expect(spy.callCount).toEqual(1); |
| 178 | + }); |
| 179 | + }); |
| 180 | + |
| 181 | + describe("stop method", () => { |
| 182 | + it("should stop server", async () => { |
| 183 | + await core.stop(); |
| 184 | + expect(serverInstance.stop.callCount).toEqual(1); |
| 185 | + }); |
| 186 | + }); |
| 187 | + |
| 188 | + describe("restart method", () => { |
| 189 | + it("should restart server", async () => { |
| 190 | + await core.restart(); |
| 191 | + expect(serverInstance.restart.callCount).toEqual(1); |
| 192 | + }); |
| 193 | + }); |
| 194 | + |
| 195 | + describe("tracer getter", () => { |
| 196 | + it("should return tracer instance", () => { |
| 197 | + expect(core.tracer).toEqual(tracer); |
| 198 | + }); |
| 199 | + }); |
| 200 | + |
| 201 | + describe("serverError getter", () => { |
| 202 | + it("should return server error", () => { |
| 203 | + expect(core.serverError).toEqual(serverInstance.error); |
| 204 | + }); |
| 205 | + }); |
| 206 | + |
| 207 | + describe("settings getter", () => { |
| 208 | + it("should return settings", () => { |
| 209 | + expect(core.settings).toEqual(settingsInstance); |
| 210 | + }); |
| 211 | + }); |
| 212 | + |
| 213 | + describe("behaviors getter", () => { |
| 214 | + it("should return mocks behaviors", () => { |
| 215 | + expect(core.behaviors).toEqual(mocksInstance.behaviors); |
| 216 | + }); |
| 217 | + }); |
| 218 | + |
| 219 | + describe("features getter", () => { |
| 220 | + it("should return mocks behaviors", () => { |
| 221 | + expect(core.features).toEqual(mocksInstance.behaviors); |
| 222 | + }); |
| 223 | + }); |
| 224 | +}); |
0 commit comments