This repository was archived by the owner on Feb 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrequestMocking.test.js
More file actions
80 lines (66 loc) · 2.83 KB
/
requestMocking.test.js
File metadata and controls
80 lines (66 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { RequestMocker, Element, Helpers } from "test-juggler";
const fs = require("fs");
describe("Request mocking by recording and replaying specific requests", () => {
let mocker = new RequestMocker();
let helpers = new Helpers();
it("Recording all requests and replaying it on the second test run google", async () => {
//Arrange
await mocker.start();
//Act
await helpers.goToUrlAndLoad("https://downforeveryoneorjustme.com/google");
await mocker.stop();
//Assert
expect(fs.existsSync("mockData\\Request mocking by recording and replaying and indercetipt specific requests\\Recording all requests and replaying it on the second test run google\\interceptors\\get.json")).toBe(true);
});
it("Interceptor response 404 to all requests", async () => {
//Arrange
const errorTextElement = new Element("div.humane.humane-jackedup-error.humane-animate");
const notFoundInterceptor = {
response: { status: 404 }
};
//Act
await mocker.start(notFoundInterceptor);
await helpers.goToUrlAndLoad("https://www.cheapshark.com/");
await mocker.stop();
const actualText = await errorTextElement.text();
//Assert
expect(actualText).toMatch("Error - 404 Not Found");
});
it("Interceptor changing response body", async () => {
//Arrange
const errorTextElement = new Element("#json > span:nth-child(42)");
const responseBodyChanged = {
url: "https://api.ratesapi.io/api/latest",
response: {
status: 200,
body: { "base": "EUR", "rates": { "This": 0.89448, "Response": 8.7809, "is": 15882.4, "mocked": 3.9172, "!": 7.4564 }, "date": "2020-06-05" }
}
};
//Act
await mocker.start(responseBodyChanged);
await helpers.goToUrlAndLoad("https://ratesapi.io/");
await mocker.stop();
const actualText = await errorTextElement.text();
//Assert
expect(actualText).toMatch("\"mocked\"");
});
it("Interceptor delaying response by 3s (ttfb)", async () => {
//Arrange
const timeStamp = Date.now();
const responseBodyChanged = {
url: "https://api.ratesapi.io/api/latest",
response: {
status: 200,
body: { "base": "EUR", "rates": { "This": 0.89448, "Response": 8.7809, "is": 15882.4, "mocked": 3.9172, "!": 7.4564 }, "date": "2020-06-05" },
ttfb: 3000
}
};
//Act
await mocker.start(responseBodyChanged);
await helpers.goToUrlAndLoad("https://ratesapi.io/");
const pageLoadTimeinMs = Date.now() - timeStamp;
await mocker.stop();
//Assert
expect(pageLoadTimeinMs).toBeGreaterThan(3000);
});
});