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

Commit fc0f262

Browse files
committed
Add fixtures api unit tests
1 parent 6938bee commit fc0f262

1 file changed

Lines changed: 169 additions & 0 deletions

File tree

test/unit/src/Fixtures.spec.js

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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 Fixtures = require("../../../src/Fixtures");
20+
21+
describe("Fixtures", () => {
22+
let sandbox;
23+
let libMocks;
24+
let coreMock;
25+
let coreInstance;
26+
let resMock;
27+
let fixtures;
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+
fixtures = new Fixtures(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 fixtures ids", async () => {
58+
expect(libMocks.stubs.express.get.getCall(1).args[0]).toEqual("/:id");
59+
});
60+
});
61+
62+
describe("getCollection router", () => {
63+
it("should return the fixtures collection parsed", () => {
64+
coreInstance.fixtures = {
65+
collection: [
66+
{
67+
id: "foo-id",
68+
requestMatchId: "request-match-id",
69+
request: "foo-request",
70+
response: "foo-response"
71+
},
72+
{
73+
id: "foo-id-2",
74+
requestMatchId: "request-match-id-2",
75+
request: "foo-request-2",
76+
response: "foo-response-2"
77+
}
78+
]
79+
};
80+
fixtures = new Fixtures(coreInstance);
81+
fixtures.getCollection({}, resMock);
82+
expect(resMock.send.getCall(0).args[0]).toEqual([
83+
{
84+
id: "foo-id",
85+
requestMatchId: "request-match-id",
86+
handler: undefined,
87+
request: "foo-request",
88+
response: "foo-response"
89+
},
90+
{
91+
id: "foo-id-2",
92+
requestMatchId: "request-match-id-2",
93+
handler: undefined,
94+
request: "foo-request-2",
95+
response: "foo-response-2"
96+
}
97+
]);
98+
});
99+
});
100+
101+
describe("getModel router", () => {
102+
it("should return the requested fixture model parsed", () => {
103+
coreInstance.fixtures = {
104+
collection: [
105+
{
106+
id: "foo-id",
107+
requestMatchId: "request-match-id",
108+
request: "foo-request",
109+
response: "foo-response"
110+
},
111+
{
112+
id: "foo-id-2",
113+
requestMatchId: "request-match-id-2",
114+
request: "foo-request-2",
115+
response: "foo-response-2"
116+
}
117+
]
118+
};
119+
fixtures = new Fixtures(coreInstance);
120+
fixtures.getModel(
121+
{
122+
params: {
123+
id: "foo-id"
124+
}
125+
},
126+
resMock
127+
);
128+
expect(resMock.send.getCall(0).args[0]).toEqual({
129+
id: "foo-id",
130+
requestMatchId: "request-match-id",
131+
handler: undefined,
132+
request: "foo-request",
133+
response: "foo-response"
134+
});
135+
});
136+
137+
it("should return a not found error if fixture is not found", () => {
138+
const nextStub = sandbox.stub();
139+
sandbox.stub(Boom, "notFound").returns("foo-error");
140+
coreInstance.fixtures = {
141+
collection: [
142+
{
143+
id: "foo-id",
144+
requestMatchId: "request-match-id",
145+
request: "foo-request",
146+
response: "foo-response"
147+
}
148+
]
149+
};
150+
fixtures = new Fixtures(coreInstance);
151+
fixtures.getModel(
152+
{
153+
params: {
154+
id: "foo3"
155+
}
156+
},
157+
resMock,
158+
nextStub
159+
);
160+
expect(nextStub.getCall(0).args[0]).toEqual("foo-error");
161+
});
162+
});
163+
164+
describe("router getter", () => {
165+
it("should return express created router", async () => {
166+
expect(fixtures.router).toEqual(libMocks.stubs.express);
167+
});
168+
});
169+
});

0 commit comments

Comments
 (0)