|
| 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 Behaviors = require("../../../src/Behaviors"); |
| 20 | + |
| 21 | +describe("Behavior", () => { |
| 22 | + let sandbox; |
| 23 | + let libMocks; |
| 24 | + let coreMock; |
| 25 | + let coreInstance; |
| 26 | + let resMock; |
| 27 | + let behaviors; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + sandbox = sinon.createSandbox(); |
| 31 | + resMock = { |
| 32 | + status: sandbox.stub(), |
| 33 | + send: sandbox.stub() |
| 34 | + }; |
| 35 | + libMocks = new LibMocks(); |
| 36 | + coreMock = new CoreMocks(); |
| 37 | + coreInstance = coreMock.stubs.instance; |
| 38 | + behaviors = new Behaviors(coreInstance); |
| 39 | + expect.assertions(1); |
| 40 | + }); |
| 41 | + |
| 42 | + afterEach(() => { |
| 43 | + sandbox.restore(); |
| 44 | + libMocks.restore(); |
| 45 | + coreMock.restore(); |
| 46 | + }); |
| 47 | + |
| 48 | + describe("when created", () => { |
| 49 | + it("should create an express Router", async () => { |
| 50 | + expect(express.Router.calledOnce).toEqual(true); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should have added get router at /", async () => { |
| 54 | + expect(libMocks.stubs.express.get.getCall(0).args[0]).toEqual("/"); |
| 55 | + }); |
| 56 | + |
| 57 | + it("should register a get router for behavior names", async () => { |
| 58 | + expect(libMocks.stubs.express.get.getCall(1).args[0]).toEqual("/:name"); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe("getCollection router", () => { |
| 63 | + it("should return the behaviors collection parsed", () => { |
| 64 | + coreInstance.behaviors = { |
| 65 | + collection: [ |
| 66 | + { |
| 67 | + name: "foo", |
| 68 | + fixtures: [{ id: "foo-fixture-id-1" }, { id: "foo-fixture-id-2" }], |
| 69 | + extendedFrom: "foo-base-behavior" |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "foo2", |
| 73 | + fixtures: [{ id: "foo-fixture-id-3" }], |
| 74 | + extendedFrom: null |
| 75 | + } |
| 76 | + ] |
| 77 | + }; |
| 78 | + behaviors = new Behaviors(coreInstance); |
| 79 | + behaviors.getCollection({}, resMock); |
| 80 | + expect(resMock.send.getCall(0).args[0]).toEqual([ |
| 81 | + { |
| 82 | + name: "foo", |
| 83 | + fixtures: ["foo-fixture-id-1", "foo-fixture-id-2"], |
| 84 | + extendedFrom: "foo-base-behavior" |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "foo2", |
| 88 | + fixtures: ["foo-fixture-id-3"], |
| 89 | + extendedFrom: null |
| 90 | + } |
| 91 | + ]); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + describe("getModel router", () => { |
| 96 | + it("should return the requested behavior model parsed", () => { |
| 97 | + coreInstance.behaviors = { |
| 98 | + collection: [ |
| 99 | + { |
| 100 | + name: "foo", |
| 101 | + fixtures: [{ id: "foo-fixture-id-1" }, { id: "foo-fixture-id-2" }], |
| 102 | + extendedFrom: "foo-base-behavior" |
| 103 | + }, |
| 104 | + { |
| 105 | + name: "foo2", |
| 106 | + fixtures: [{ id: "foo-fixture-id-3" }], |
| 107 | + extendedFrom: null |
| 108 | + } |
| 109 | + ] |
| 110 | + }; |
| 111 | + behaviors = new Behaviors(coreInstance); |
| 112 | + behaviors.getModel( |
| 113 | + { |
| 114 | + params: { |
| 115 | + name: "foo" |
| 116 | + } |
| 117 | + }, |
| 118 | + resMock |
| 119 | + ); |
| 120 | + expect(resMock.send.getCall(0).args[0]).toEqual({ |
| 121 | + name: "foo", |
| 122 | + fixtures: ["foo-fixture-id-1", "foo-fixture-id-2"], |
| 123 | + extendedFrom: "foo-base-behavior" |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + it("should return a not found error if behavior is not found", () => { |
| 128 | + const nextStub = sandbox.stub(); |
| 129 | + sandbox.stub(Boom, "notFound").returns("foo-error"); |
| 130 | + coreInstance.behaviors = { |
| 131 | + collection: [ |
| 132 | + { |
| 133 | + name: "foo", |
| 134 | + fixtures: [{ id: "foo-fixture-id-1" }, { id: "foo-fixture-id-2" }], |
| 135 | + extendedFrom: "foo-base-behavior" |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "foo2", |
| 139 | + fixtures: [{ id: "foo-fixture-id-3" }], |
| 140 | + extendedFrom: null |
| 141 | + } |
| 142 | + ] |
| 143 | + }; |
| 144 | + behaviors = new Behaviors(coreInstance); |
| 145 | + behaviors.getModel( |
| 146 | + { |
| 147 | + params: { |
| 148 | + name: "foo3" |
| 149 | + } |
| 150 | + }, |
| 151 | + resMock, |
| 152 | + nextStub |
| 153 | + ); |
| 154 | + expect(nextStub.getCall(0).args[0]).toEqual("foo-error"); |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + describe("router getter", () => { |
| 159 | + it("should return express created router", async () => { |
| 160 | + expect(behaviors.router).toEqual(libMocks.stubs.express); |
| 161 | + }); |
| 162 | + }); |
| 163 | +}); |
0 commit comments