-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjest.utils.ts
More file actions
74 lines (66 loc) · 1.54 KB
/
Copy pathjest.utils.ts
File metadata and controls
74 lines (66 loc) · 1.54 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
import { ResponseMessageType } from "@/consts/requests";
expect.extend({
toBeControlledBy(
area: HTMLElement,
trigger: HTMLElement
): jest.CustomMatcherResult {
const areaId = area?.getAttribute("id");
const triggerControls = trigger?.getAttribute("aria-controls");
return {
message: () => "Test failure",
pass:
Boolean(areaId) &&
Boolean(triggerControls) &&
areaId === triggerControls,
};
},
});
function mock200Json<T>(data: T) {
return {
ok: true,
status: 200,
json: async () => ({
message: ResponseMessageType.SUCCESS,
data,
}),
};
}
function mockDownloadFile() {
return {
ok: true,
blob: () =>
Promise.resolve(new Blob(["mock file content"], { type: "text/plain" })),
headers: {
get: (key: string) =>
key === "Content-Disposition"
? 'attachment; filename="mockfile.txt"'
: null,
},
};
}
function mockFailedJson<T>(data: T, status = 404) {
return {
ok: false,
status,
json: async () => ({
message: ResponseMessageType.FAILURE,
data,
}),
};
}
function mockPagedResults<T>(
data: T[],
page: number = 1,
perPage: number = 25
) {
const startIndex = (page - 1) * perPage;
const endIndex = startIndex + perPage;
const paginatedData = data.slice(startIndex, endIndex);
return {
total: data.length,
last_page: data.length / perPage,
current_page: page,
data: paginatedData,
};
}
export { mock200Json, mockDownloadFile, mockFailedJson, mockPagedResults };