|
| 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