-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommands.ts
80 lines (72 loc) · 2.57 KB
/
commands.ts
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
/// <reference path="./index.d.ts" />
import { constants } from "./constants";
const url: string = Cypress.env("cypress_api_mock_url") ?? "127.0.0.1";
const port: number = Cypress.env("cypress_api_mock_port") ?? 3000;
const serverAddressWithPort = `${url}:${port}`;
Cypress.Commands.add(
"apiMock",
(pattern: string, response: string | Object, options: Partial<Cypress.Timeoutable> = {}): Cypress.Chainable<void> => {
Cypress.log({ message: [pattern] });
const data: IApiMockOptions = { pattern, response };
return (cy
.request({
log: false,
method: "POST",
url: `${serverAddressWithPort}${constants.Paths.registerMock}`,
body: data,
...options,
})
.then(() => {}) as any) as Cypress.Chainable<void>;
}
);
Cypress.Commands.add(
"apiMockRequests",
(options: Partial<Cypress.Timeoutable> = {}): Cypress.Chainable<{ [key: string]: IApiMockRequestData[] }> => {
Cypress.log({});
return cy
.request<{ [key: string]: IApiMockRequestData[] }>({
log: false,
url: `${serverAddressWithPort}${constants.Paths.getRequests}`,
body: {},
...options,
})
.then((response) => response.body);
}
);
Cypress.Commands.add(
"apiMockResponses",
(options: Partial<Cypress.Timeoutable> = {}): Cypress.Chainable<{ [key: string]: string[] }> => {
Cypress.env();
Cypress.log({});
return cy
.request<{ [key: string]: string[] }>({
log: false,
url: `${serverAddressWithPort}${constants.Paths.getResponses}`,
body: {},
...options,
})
.then((response) => response.body);
}
);
Cypress.Commands.add(
"apiMockResetCalls",
(options: Partial<Cypress.Timeoutable> = {}): Cypress.Chainable<void> => {
Cypress.log({});
return (cy
.request({
log: false,
url: `${serverAddressWithPort}${constants.Paths.resetCalls}`,
...options,
})
.then(() => {}) as any) as Cypress.Chainable<void>;
}
);
Cypress.Commands.add(
"apiMockReset",
(options: Partial<Cypress.Timeoutable> = {}): Cypress.Chainable<void> => {
Cypress.log({});
return (cy
.request({ log: false, url: `${serverAddressWithPort}${constants.Paths.resetAll}`, ...options })
.then(() => {}) as any) as Cypress.Chainable<void>;
}
);