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

Commit 5e7bdfe

Browse files
committed
Add core settings unit tests
1 parent 4e30b9e commit 5e7bdfe

4 files changed

Lines changed: 132 additions & 22 deletions

File tree

jest.config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ module.exports = {
4444
"**/test/unit/core/mocks/Mocks.spec.js",
4545
"**/test/unit/core/server/Server.spec.js",
4646
"**/test/unit/core/settings/CommandLineArguments.spec.js",
47-
"**/test/unit/core/settings/Options.spec.js"
47+
"**/test/unit/core/settings/Options.spec.js",
48+
"**/test/unit/core/settings/Settings.spec.js"
4849
],
49-
//testMatch: ["**/test/unit/core/settings/Options.spec.js"],
50+
//testMatch: ["**/test/unit/core/settings/Settings.spec.js"],
5051

5152
// The test environment that will be used for testing
5253
testEnvironment: "node"

lib/core/settings/Settings.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Settings {
1818
constructor(coreOptions, eventEmitter) {
1919
this._eventEmitter = eventEmitter;
2020
this._optionsHandler = new Options(coreOptions);
21-
this._emitChange = this._emitChange.bind(this);
21+
this._emitChange = this._emitChange.bind(this); //Add debounce here to group change events
2222
this._newSettings = {};
2323
}
2424

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

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

Lines changed: 83 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,91 @@ http://www.apache.org/licenses/LICENSE-2.0
99
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.
1010
*/
1111

12-
const Settings = require("../../../lib/core/Settings");
12+
const sinon = require("sinon");
13+
14+
const OptionsMocks = require("./Options.mocks.js");
15+
const CoreMocks = require("../Core.mocks.js");
16+
17+
const Settings = require("../../../../lib/core/settings/Settings");
18+
const tracer = require("../../../../lib/core/tracer");
1319

1420
describe("Settings", () => {
15-
describe("when instantiated", () => {
16-
describe("options", () => {
17-
it("should set the delay value", () => {
18-
const settings = new Settings({
19-
delay: 3000
20-
});
21-
expect(settings.delay).toEqual(3000);
22-
});
23-
});
24-
25-
describe("delay setter", () => {
26-
it("should set the daly value", () => {
27-
const settings = new Settings({
28-
delay: 3000
29-
});
30-
settings.delay = 2000;
31-
expect(settings.delay).toEqual(2000);
32-
});
21+
let optionsMocks;
22+
let optionsInstance;
23+
let coreMocks;
24+
let coreInstance;
25+
let sandbox;
26+
let settings;
27+
28+
beforeEach(async () => {
29+
sandbox = sinon.createSandbox();
30+
optionsMocks = new OptionsMocks();
31+
optionsInstance = optionsMocks.stubs.instance;
32+
coreMocks = new CoreMocks();
33+
coreInstance = coreMocks.stubs.instance;
34+
sandbox.stub(tracer, "set");
35+
sandbox.stub(tracer, "info");
36+
settings = new Settings({}, coreInstance._eventEmitter);
37+
await settings.init();
38+
});
39+
40+
afterEach(() => {
41+
sandbox.restore();
42+
optionsMocks.restore();
43+
coreMocks.restore();
44+
});
45+
46+
describe("when initializated", () => {
47+
it("should init options", () => {
48+
expect(optionsInstance.init.callCount).toEqual(1);
49+
});
50+
51+
it("should set current tracer level", () => {
52+
expect(tracer.set.calledWith("console", "foo-log-level")).toEqual(true);
53+
});
54+
});
55+
56+
describe("get method", () => {
57+
it("should return current option value", () => {
58+
expect(settings.get("log")).toEqual("foo-log-level");
59+
});
60+
61+
it("should return new value if set is called", () => {
62+
settings.set("log", "foo-new-value");
63+
expect(settings.get("log")).toEqual("foo-new-value");
64+
});
65+
});
66+
67+
describe("set method", () => {
68+
it("should set log level if setting is log", () => {
69+
settings.set("log", "foo-new-value");
70+
expect(tracer.set.calledWith("console", "foo-new-value")).toEqual(true);
71+
});
72+
73+
it("should emit change if setting has changed value", () => {
74+
settings.set("foo", "foo-new-value");
75+
expect(
76+
coreInstance._eventEmitter.emit.calledWith("change:settings", {
77+
foo: "foo-new-value"
78+
})
79+
).toEqual(true);
80+
});
81+
82+
it("should not emit change if setting maintains value", () => {
83+
settings.set("foo", "foo-new-value");
84+
settings.set("foo", "foo-new-value");
85+
settings.set("foo", "foo-new-value");
86+
expect(coreInstance._eventEmitter.emit.callCount).toEqual(1);
87+
});
88+
});
89+
90+
describe("addCustom method", () => {
91+
it("should pass custom option to options Class", () => {
92+
const fooOption = {
93+
foo: "foo"
94+
};
95+
settings.addCustom(fooOption);
96+
expect(optionsInstance.addCustom.calledWith(fooOption)).toEqual(true);
3397
});
3498
});
3599
});

0 commit comments

Comments
 (0)