-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
126 lines (109 loc) · 2.9 KB
/
vitest.setup.ts
File metadata and controls
126 lines (109 loc) · 2.9 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import "@testing-library/jest-dom";
const months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
function formatDate(date: Date, pattern: string): string {
const pad = (value: number) => value.toString().padStart(2, "0");
const year = date.getUTCFullYear();
const monthIndex = date.getUTCMonth();
const day = date.getUTCDate();
const hours = date.getUTCHours();
const minutes = date.getUTCMinutes();
const seconds = date.getUTCSeconds();
return pattern
.replace("YYYY", year.toString())
.replace("MMMM", months[monthIndex])
.replace("MM", pad(monthIndex + 1))
.replace("DD", pad(day))
.replace("HH", pad(hours))
.replace("mm", pad(minutes))
.replace("ss", pad(seconds));
}
function createMoment(dateInput?: Date | string | number) {
let date =
dateInput instanceof Date
? new Date(dateInput)
: dateInput
? new Date(dateInput)
: new Date();
const api = {
format: (pattern: string = "YYYY-MM-DD") => formatDate(date, pattern),
startOf: (unit?: string) => {
if (unit === "day") {
date = new Date(
date.getFullYear(),
date.getMonth(),
date.getDate(),
0,
0,
0,
0,
);
}
return api;
},
seconds: (seconds: number) => {
const updated = new Date(date);
updated.setSeconds(seconds);
updated.setMilliseconds(0);
date = updated;
return api;
},
};
return api;
}
(window as unknown as { moment: typeof createMoment }).moment = createMoment;
if (typeof IntersectionObserver === "undefined") {
class MockIntersectionObserver implements IntersectionObserver {
constructor(
private callback: IntersectionObserverCallback,
private _options?: IntersectionObserverInit,
) {}
readonly root: Element | Document | null = null;
readonly rootMargin: string = this._options?.rootMargin ?? "0px";
readonly thresholds: ReadonlyArray<number> = [0];
disconnect(): void {}
observe(target: Element): void {
this.callback(
[
{
isIntersecting: true,
target,
intersectionRatio: 1,
boundingClientRect: target.getBoundingClientRect(),
intersectionRect: target.getBoundingClientRect(),
rootBounds: null,
time: 0,
} as IntersectionObserverEntry,
],
this,
);
}
takeRecords(): IntersectionObserverEntry[] {
return [];
}
unobserve(): void {}
}
(globalThis as unknown as { IntersectionObserver: typeof MockIntersectionObserver })
.IntersectionObserver = MockIntersectionObserver;
}
if (!Element.prototype.scrollIntoView) {
Element.prototype.scrollIntoView = () => {};
}
if (!(HTMLElement.prototype as unknown as { setAttr?: (name: string, value: string) => void }).setAttr) {
(HTMLElement.prototype as unknown as { setAttr: (name: string, value: string) => void }).setAttr =
function (this: HTMLElement, name: string, value: string) {
this.setAttribute(name, value);
};
}