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

Commit ff51833

Browse files
committed
Add Settings api unit tests
1 parent fc0f262 commit ff51833

2 files changed

Lines changed: 137 additions & 0 deletions

File tree

test/unit/Core.mocks.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class CoreMock {
2424
stop: this._sandbox.stub().resolves(),
2525
restart: this._sandbox.stub().resolves(),
2626
settings: {
27+
getValidOptionName: this._sandbox.stub(),
2728
get: this._sandbox.stub(),
2829
set: this._sandbox.stub(),
2930
all: {}

test/unit/src/Settings.spec.js

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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 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 Settings = require("../../../src/Settings");
20+
21+
describe("Fixtures", () => {
22+
let sandbox;
23+
let libMocks;
24+
let coreMock;
25+
let coreInstance;
26+
let resMock;
27+
let nextStub;
28+
let settings;
29+
30+
beforeEach(() => {
31+
sandbox = sinon.createSandbox();
32+
nextStub = sandbox.stub();
33+
resMock = {
34+
status: sandbox.stub(),
35+
send: sandbox.stub()
36+
};
37+
libMocks = new LibMocks();
38+
coreMock = new CoreMocks();
39+
coreInstance = coreMock.stubs.instance;
40+
settings = new Settings(coreInstance);
41+
expect.assertions(1);
42+
});
43+
44+
afterEach(() => {
45+
sandbox.restore();
46+
libMocks.restore();
47+
coreMock.restore();
48+
});
49+
50+
describe("when created", () => {
51+
it("should create an express Router", async () => {
52+
expect(express.Router.calledOnce).toEqual(true);
53+
});
54+
55+
it("should have added a patch router at /", async () => {
56+
expect(libMocks.stubs.express.get.getCall(0).args[0]).toEqual("/");
57+
});
58+
59+
it("should have added a get router at /", async () => {
60+
expect(libMocks.stubs.express.get.getCall(0).args[0]).toEqual("/");
61+
});
62+
});
63+
64+
describe("patch router", () => {
65+
it("should set new settings", () => {
66+
expect.assertions(4);
67+
coreInstance.settings.getValidOptionName.withArgs("log").returns("log");
68+
coreInstance.settings.getValidOptionName.withArgs("delay").returns("delay");
69+
settings = new Settings(coreInstance);
70+
settings.patch(
71+
{
72+
body: {
73+
log: "foo",
74+
delay: 4000
75+
}
76+
},
77+
resMock,
78+
nextStub
79+
);
80+
expect(coreInstance.settings.set.getCall(0).args[0]).toEqual("log");
81+
expect(coreInstance.settings.set.getCall(0).args[1]).toEqual("foo");
82+
expect(coreInstance.settings.set.getCall(1).args[0]).toEqual("delay");
83+
expect(coreInstance.settings.set.getCall(1).args[1]).toEqual(4000);
84+
});
85+
86+
it("should send a badRequest error if there is any invalid option", () => {
87+
sandbox.stub(Boom, "badRequest").returns("foo-error");
88+
coreInstance.settings.getValidOptionName.withArgs("log").returns(null);
89+
coreInstance.settings.getValidOptionName.withArgs("delay").returns("delay");
90+
settings = new Settings(coreInstance);
91+
settings.patch(
92+
{
93+
body: {
94+
log: "foo",
95+
delay: 4000
96+
}
97+
},
98+
resMock,
99+
nextStub
100+
);
101+
expect(nextStub.getCall(0).args[0]).toEqual("foo-error");
102+
});
103+
104+
it("should not set any option if one is invalid", () => {
105+
sandbox.stub(Boom, "badRequest").returns("foo-error");
106+
coreInstance.settings.getValidOptionName.withArgs("log").returns("log");
107+
coreInstance.settings.getValidOptionName.withArgs("delay").returns(null);
108+
settings = new Settings(coreInstance);
109+
settings.patch(
110+
{
111+
body: {
112+
log: "foo",
113+
delay: 4000
114+
}
115+
},
116+
resMock,
117+
nextStub
118+
);
119+
expect(coreInstance.settings.set.callCount).toEqual(0);
120+
});
121+
});
122+
123+
describe("get router", () => {
124+
it("should return all settings", () => {
125+
settings = new Settings(coreInstance);
126+
settings.get({}, resMock, nextStub);
127+
expect(resMock.send.getCall(0).args[0]).toEqual(coreInstance.settings.all);
128+
});
129+
});
130+
131+
describe("router getter", () => {
132+
it("should return express created router", async () => {
133+
expect(settings.router).toEqual(libMocks.stubs.express);
134+
});
135+
});
136+
});

0 commit comments

Comments
 (0)