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

Commit 3249171

Browse files
committed
Add acceptance test for delay setting
1 parent 9fe6c7d commit 3249171

6 files changed

Lines changed: 87 additions & 4 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"lint": "eslint index.js lib test jest.config.js jest.acceptance.config.js",
3434
"lint-staged": "lint-staged",
3535
"test": "jest",
36-
"test-acceptance": "jest --config=jest.acceptance.config.js",
36+
"test-acceptance": "jest --config=jest.acceptance.config.js --runInBand",
3737
"test-ci": "npm run test && npm run test-acceptance",
3838
"coveralls": "cat ./coverage/lcov.info | coveralls"
3939
},
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 { startServer, stopServer, request, changeDelay, TimeCounter } = require("./utils");
12+
13+
describe("delay setting", () => {
14+
let server;
15+
16+
beforeAll(async () => {
17+
server = await startServer();
18+
19+
return server.start();
20+
});
21+
22+
afterAll(() => {
23+
stopServer(server);
24+
});
25+
26+
describe("When started", () => {
27+
it("should respond with no delay", async () => {
28+
const timeCounter = new TimeCounter();
29+
await request("/api/users");
30+
timeCounter.stop();
31+
expect(timeCounter.total).toBeLessThan(200);
32+
});
33+
});
34+
35+
describe("When delay is changed", () => {
36+
it("should respond after defined delay", async () => {
37+
await changeDelay(1000);
38+
const timeCounter = new TimeCounter();
39+
await request("/api/users");
40+
timeCounter.stop();
41+
expect(timeCounter.total).toBeGreaterThan(999);
42+
});
43+
});
44+
});

test/acceptance/mocks/fixtures/web-tutorial/fixtures/users.js renamed to test/acceptance/api/fixtures/web-tutorial/fixtures/users.js

File renamed without changes.

test/acceptance/mocks/fixtures/web-tutorial/standard.js renamed to test/acceptance/api/fixtures/web-tutorial/standard.js

File renamed without changes.
Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ const startServer = (mocksPath, options = {}) => {
3535
});
3636
};
3737

38+
const stopServer = server => {
39+
server.stop();
40+
server.switchWatch(false);
41+
};
42+
3843
const request = (uri, options = {}) => {
3944
const requestOptions = {
4045
...defaultRequestOptions,
@@ -61,9 +66,40 @@ const getBehaviors = () => {
6166
return request("/mocks/behaviors");
6267
};
6368

69+
const changeDelay = delay => {
70+
return request("/mocks/settings", {
71+
method: "PUT",
72+
body: {
73+
delay
74+
}
75+
});
76+
};
77+
78+
class TimeCounter {
79+
constructor() {
80+
this._startTime = new Date();
81+
}
82+
83+
_getMiliseconds() {
84+
this._miliseconds = this._endTime - this._startTime;
85+
}
86+
87+
get total() {
88+
return this._miliseconds;
89+
}
90+
91+
stop() {
92+
this._endTime = new Date();
93+
this._getMiliseconds();
94+
}
95+
}
96+
6497
module.exports = {
6598
startServer,
99+
stopServer,
66100
request,
67101
changeBehavior,
68-
getBehaviors
102+
getBehaviors,
103+
changeDelay,
104+
TimeCounter
69105
};

test/acceptance/mocks/web-tutorial.spec.js renamed to test/acceptance/api/web-tutorial.spec.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ http://www.apache.org/licenses/LICENSE-2.0
88
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.
99
*/
1010

11-
const { startServer, request, changeBehavior, getBehaviors } = require("./utils");
11+
const { startServer, stopServer, request, changeBehavior, getBehaviors } = require("./utils");
1212

1313
describe("web tutorial", () => {
1414
let server;
@@ -19,10 +19,13 @@ describe("web tutorial", () => {
1919
return server.start();
2020
});
2121

22+
afterAll(() => {
23+
stopServer(server);
24+
});
25+
2226
describe("When started", () => {
2327
it("should have 3 behaviors available", async () => {
2428
const behaviors = await getBehaviors();
25-
console.log(behaviors);
2629
expect(behaviors.length).toEqual(3);
2730
});
2831

0 commit comments

Comments
 (0)