Skip to content
This repository was archived by the owner on Mar 28, 2022. It is now read-only.

Commit 3b34c90

Browse files
committed
Add core unit tests
1 parent 839b4dc commit 3b34c90

6 files changed

Lines changed: 294 additions & 39 deletions

File tree

jest.config.js

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,16 @@ module.exports = {
1717
// An object that configures minimum threshold enforcement for coverage results
1818
coverageThreshold: {
1919
global: {
20-
branches: 85,
21-
functions: 82,
22-
lines: 85,
23-
statements: 85
20+
branches: 88,
21+
functions: 90,
22+
lines: 90,
23+
statements: 90
2424
}
2525
},
2626

2727
// The glob patterns Jest uses to detect test files
2828
testMatch: ["**/test/unit/**/?(*.)+(spec|test).js?(x)"],
29-
testMatch: [
30-
"**/test/unit/index.spec.js",
31-
"**/test/unit/start.spec.js",
32-
"**/test/unit/ProgrammaticCli.spec.js",
33-
"**/test/unit/ProgrammaticServer.spec.js",
34-
"**/test/unit/core/tracer.spec.js",
35-
"**/test/unit/core/server/middlewares.spec.js",
36-
"**/test/unit/cli/Inquirer.spec.js",
37-
"**/test/unit/api/Settings.spec.js",
38-
"**/test/unit/api/Behaviors.spec.js",
39-
"**/test/unit/api/Api.spec.js",
40-
"**/test/unit/cli/Cli.spec.js",
41-
"**/test/unit/core/mocks/Behavior.spec.js",
42-
"**/test/unit/core/mocks/Behaviors.spec.js",
43-
"**/test/unit/core/mocks/FilesHandler.spec.js",
44-
"**/test/unit/core/mocks/Mocks.spec.js",
45-
"**/test/unit/core/server/Server.spec.js",
46-
"**/test/unit/core/settings/CommandLineArguments.spec.js",
47-
"**/test/unit/core/settings/Options.spec.js",
48-
"**/test/unit/core/settings/Settings.spec.js"
49-
],
50-
//testMatch: ["**/test/unit/core/settings/Settings.spec.js", "**/test/unit/core/settings/Options.spec.js"],
29+
// testMatch: ["**/test/unit/core/Plugins.spec.js"],
5130

5231
// The test environment that will be used for testing
5332
testEnvironment: "node"

lib/core/Core.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ class Core {
4848
return this._plugins.init();
4949
}
5050

51+
async start() {
52+
await this.init(); // in case it has not been initializated manually before
53+
await this._mocks.start();
54+
await this._server.start();
55+
return this._startPlugins();
56+
}
57+
5158
async _startPlugins() {
5259
if (!this._startPluginsPromise) {
5360
this._startPluginsPromise = this._plugins.start();
@@ -83,13 +90,6 @@ class Core {
8390

8491
// Expose Server methods and getters
8592

86-
async start() {
87-
await this.init(); // in case it has not been initializated manually before
88-
await this._mocks.start();
89-
await this._server.start();
90-
return this._startPlugins();
91-
}
92-
9393
stop() {
9494
return this._server.stop();
9595
}

test/unit/core/Core.spec.js

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
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+
});

test/unit/core/Plugins.mocks.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
jest.mock("../../../lib/core/Plugins");
15+
16+
const Plugins = require("../../../lib/core/Plugins");
17+
18+
class Mock {
19+
constructor() {
20+
this._sandbox = sinon.createSandbox();
21+
22+
this._stubs = {
23+
init: this._sandbox.stub(),
24+
register: this._sandbox.stub(),
25+
start: this._sandbox.stub().resolves()
26+
};
27+
28+
Plugins.mockImplementation(() => this._stubs);
29+
}
30+
31+
get stubs() {
32+
return {
33+
Constructor: Plugins,
34+
instance: this._stubs
35+
};
36+
}
37+
38+
restore() {
39+
this._sandbox.restore();
40+
}
41+
42+
reset() {
43+
this._sandbox.reset();
44+
}
45+
}
46+
47+
module.exports = Mock;

test/unit/core/server/Server.mocks.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ class Mock {
2121

2222
this._stubs = {
2323
error: null,
24-
start: this._sandbox.stub(),
25-
restart: this._sandbox.stub(),
26-
stop: this._sandbox.stub()
24+
start: this._sandbox.stub().resolves(),
25+
restart: this._sandbox.stub().resolves(),
26+
stop: this._sandbox.stub().resolves(),
27+
init: this._sandbox.stub().resolves(),
28+
addCustomRouter: this._sandbox.stub().resolves()
2729
};
2830

2931
Server.mockImplementation(() => this._stubs);

test/unit/core/settings/Settings.mocks.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@ Unless required by applicable law or agreed to in writing, software distributed
1111

1212
const sinon = require("sinon");
1313

14-
jest.mock("../../../lib/core/Settings");
14+
jest.mock("../../../../lib/core/settings/Settings");
1515

16-
const Settings = require("../../../lib/core/Settings");
16+
const Settings = require("../../../../lib/core/settings/Settings");
1717

1818
class Mock {
1919
constructor() {
2020
this._sandbox = sinon.createSandbox();
2121

2222
this._stubs = {
23-
delay: 200
23+
get: this._sandbox.stub(),
24+
set: this._sandbox.stub(),
25+
init: this._sandbox.stub().resolves(),
26+
addCustom: this._sandbox.stub()
2427
};
2528

2629
Settings.mockImplementation(() => this._stubs);

0 commit comments

Comments
 (0)