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

Commit 9ea9bc6

Browse files
committed
Add Plugins class unit tests
1 parent 3b34c90 commit 9ea9bc6

4 files changed

Lines changed: 141 additions & 15 deletions

File tree

jest.config.js

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

2727
// The glob patterns Jest uses to detect test files
2828
testMatch: ["**/test/unit/**/?(*.)+(spec|test).js?(x)"],
29-
// testMatch: ["**/test/unit/core/Plugins.spec.js"],
29+
//testMatch: ["**/test/unit/core/Plugins.spec.js"],
3030

3131
// The test environment that will be used for testing
3232
testEnvironment: "node"

lib/core/Plugins.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Unless required by applicable law or agreed to in writing, software distributed
99
*/
1010

1111
const isPromise = require("is-promise");
12-
const { isFunction } = require("lodash");
12+
const { isObject, isFunction } = require("lodash");
1313

1414
const tracer = require("./tracer");
1515

@@ -62,38 +62,41 @@ class Plugins {
6262
return Promise.resolve();
6363
}
6464

65-
_createPluginFunction(Plugin) {
65+
_registerPluginFunction(Plugin) {
6666
let pluginInstance;
6767
try {
6868
pluginInstance = Plugin(this._core);
69+
this._pluginsRegistered++;
6970
} catch (error) {
70-
return this._catchCreateError(error);
71+
return this._catchRegisterError(error);
7172
}
7273
return pluginInstance;
7374
}
7475

75-
_createPlugin(Plugin) {
76-
if (!isFunction(Plugin)) {
76+
_registerPlugin(Plugin) {
77+
if (isObject(Plugin) && !isFunction(Plugin)) {
78+
this._pluginsRegistered++;
7779
return Plugin;
7880
}
7981
let pluginInstance;
8082
try {
8183
pluginInstance = new Plugin(this._core);
84+
this._pluginsRegistered++;
8285
} catch (error) {
8386
if (error.message.includes("is not a constructor")) {
84-
return this._createPluginFunction(Plugin);
87+
return this._registerPluginFunction(Plugin);
8588
} else {
86-
return this._catchCreateError(error);
89+
return this._catchRegisterError(error);
8790
}
8891
}
8992
return pluginInstance;
9093
}
9194

9295
_registerPlugins(pluginIndex = 0) {
93-
if (!this._plugins[pluginIndex]) {
96+
if (pluginIndex === this._plugins.length) {
9497
return Promise.resolve();
9598
}
96-
const plugin = this._createPlugin(this._plugins[pluginIndex]);
99+
const plugin = this._registerPlugin(this._plugins[pluginIndex]);
97100
this._pluginsInstances.push(plugin);
98101
return this._registerPlugins(pluginIndex + 1);
99102
}

test/unit/core/Core.spec.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/*
22
Copyright 2019 Javier Brea
3-
Copyright 2019 XbyOrange
43
54
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
65

test/unit/core/Plugins.spec.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
Copyright 2019 Javier Brea
3+
4+
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
5+
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
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.
9+
*/
10+
11+
const sinon = require("sinon");
12+
13+
const CoreMocks = require("./Core.mocks.js");
14+
15+
const Plugins = require("../../../lib/core/Plugins");
16+
const tracer = require("../../../lib/core/tracer");
17+
18+
describe("Settings", () => {
19+
let sandbox;
20+
let coreMocks;
21+
let coreInstance;
22+
let plugins;
23+
24+
beforeEach(async () => {
25+
sandbox = sinon.createSandbox();
26+
sandbox.stub(tracer, "verbose");
27+
sandbox.stub(tracer, "debug");
28+
sandbox.stub(tracer, "error");
29+
sandbox.spy(console, "log");
30+
coreMocks = new CoreMocks();
31+
coreInstance = coreMocks.stubs.instance;
32+
});
33+
34+
afterEach(() => {
35+
sandbox.restore();
36+
coreMocks.restore();
37+
});
38+
39+
describe("register method", () => {
40+
it("should do nothing if there are no plugins to register", async () => {
41+
plugins = new Plugins(null, coreInstance);
42+
await plugins.register();
43+
expect(tracer.verbose.calledWith("Registered 0 plugins")).toEqual(true);
44+
});
45+
46+
it("should register object plugins", async () => {
47+
const fooPlugin = {};
48+
plugins = new Plugins([fooPlugin], coreInstance);
49+
await plugins.register();
50+
expect(tracer.verbose.calledWith("Registered 1 plugins")).toEqual(true);
51+
});
52+
53+
it("should not register strings as plugins", async () => {
54+
expect.assertions(2);
55+
plugins = new Plugins(["foo"], coreInstance);
56+
await plugins.register();
57+
expect(console.log.calledWith("Error registering plugin")).toEqual(true);
58+
expect(tracer.verbose.calledWith("Registered 0 plugins")).toEqual(true);
59+
});
60+
61+
it("should not register booleans as plugins", async () => {
62+
expect.assertions(2);
63+
plugins = new Plugins([true], coreInstance);
64+
await plugins.register();
65+
expect(console.log.calledWith("Error registering plugin")).toEqual(true);
66+
expect(tracer.verbose.calledWith("Registered 0 plugins")).toEqual(true);
67+
});
68+
69+
it("should register function plugins", async () => {
70+
const fooPlugin = () => {};
71+
plugins = new Plugins([fooPlugin], coreInstance);
72+
await plugins.register();
73+
expect(tracer.verbose.calledWith("Registered 1 plugins")).toEqual(true);
74+
});
75+
76+
it("should register class plugins, instantiating them passing the core", async () => {
77+
expect.assertions(3);
78+
let instantiated = false;
79+
let receivedCore;
80+
class FooPlugin {
81+
constructor(core) {
82+
console.log("Created class");
83+
receivedCore = core;
84+
instantiated = true;
85+
}
86+
}
87+
plugins = new Plugins([FooPlugin], coreInstance);
88+
await plugins.register();
89+
expect(receivedCore).toEqual(coreInstance);
90+
expect(instantiated).toEqual(true);
91+
expect(tracer.verbose.calledWith("Registered 1 plugins")).toEqual(true);
92+
});
93+
94+
it("should not register class plugins if class throw an error when being created", async () => {
95+
expect.assertions(2);
96+
class FooPlugin {
97+
constructor() {
98+
throw new Error();
99+
}
100+
}
101+
plugins = new Plugins([FooPlugin], coreInstance);
102+
await plugins.register();
103+
expect(console.log.calledWith("Error registering plugin")).toEqual(true);
104+
expect(tracer.verbose.calledWith("Registered 0 plugins")).toEqual(true);
105+
});
106+
107+
it("should trace the total number of registered plugins", async () => {
108+
expect.assertions(2);
109+
class FooPlugin {
110+
constructor() {
111+
throw new Error();
112+
}
113+
}
114+
class FooPlugin2 {}
115+
plugins = new Plugins(
116+
[FooPlugin, FooPlugin2, () => {}, true, false, "foo", { foo: "foo" }],
117+
coreInstance
118+
);
119+
await plugins.register();
120+
expect(console.log.calledWith("Error registering plugin")).toEqual(true);
121+
expect(tracer.verbose.calledWith("Registered 3 plugins")).toEqual(true);
122+
});
123+
});
124+
});

0 commit comments

Comments
 (0)