-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathjest.setup.ts
More file actions
153 lines (137 loc) · 3.69 KB
/
jest.setup.ts
File metadata and controls
153 lines (137 loc) · 3.69 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import "@testing-library/jest-dom";
// Shadcn Select component를 열기 위해 필요한 mock
class MockPointerEvent extends Event {
button: number;
ctrlKey: boolean;
pointerType: string;
constructor(type: string, props: PointerEventInit) {
super(type, props);
this.button = props.button || 0;
this.ctrlKey = props.ctrlKey || false;
this.pointerType = props.pointerType || "mouse";
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
window.PointerEvent = MockPointerEvent as any;
window.HTMLElement.prototype.scrollIntoView = jest.fn();
window.HTMLElement.prototype.releasePointerCapture = jest.fn();
window.HTMLElement.prototype.hasPointerCapture = jest.fn();
beforeEach(() => {
jest.restoreAllMocks(); // 테스트 전 원래 구현으로 스파이 복원
});
jest.mock("next/navigation", () => ({
...jest.requireActual("next/navigation"),
usePathname: jest.fn(() => "/"),
useRouter: jest.fn(() => ({
push: jest.fn(),
replace: jest.fn(),
back: jest.fn(),
prefetch: jest.fn(),
})),
useSearchParams: jest.fn(() => ({
get: jest.fn(() => null),
getAll: jest.fn(() => []),
forEach: jest.fn(),
entries: jest.fn(() => []),
keys: jest.fn(() => []),
values: jest.fn(() => []),
has: jest.fn(() => false),
})),
}));
interface Cookie {
name: string;
value: string;
[key: string]: unknown;
}
interface CookieStore {
cookies: Record<string, Cookie>;
set: (options: {
name: string;
value: string;
[key: string]: unknown;
}) => void;
get: (name: string) => Cookie | undefined;
delete: (name: string) => void;
getAll: () => Cookie[];
}
const cookieStore: CookieStore = {
cookies: {} as Record<string, { name: string; value: string }>,
get: jest.fn((name: string) => {
return cookieStore.cookies[name];
}),
getAll: jest.fn(() => {
return Object.values(cookieStore.cookies);
}),
delete: jest.fn((name: string) => {
delete cookieStore.cookies[name];
}),
set: jest.fn(({ name, value }) => {
cookieStore.cookies[name] = { name, value };
}),
};
const mockCookies = jest.fn(() => cookieStore);
jest.mock("next/headers", () => {
return {
cookies: mockCookies,
};
});
jest.mock("@/stores/useUserStore", () => {
const state = {
user: null,
isLoggedIn: false,
isHydrated: false,
clearUserState: jest.fn(() => {
state.user = null;
state.isLoggedIn = false;
state.isHydrated = false;
}),
updateUserState: jest.fn(user => {
state.user = user;
}),
setUserState: jest.fn(user => {
state.user = user;
state.isLoggedIn = true;
}),
setHydrated: jest.fn(isHydrated => {
state.isHydrated = isHydrated;
}),
};
const storeFunction = jest.fn(() => state);
return {
__esModule: true,
default: Object.assign(storeFunction, {
getState: () => state,
setState: (newState: unknown) => Object.assign(state, newState),
}),
useShallow: jest.fn(fn => fn),
};
});
jest.mock("@/stores/useSideBarStore", () => {
const state = {
isOpen: false,
onToggleSideBar: jest.fn((flag: boolean) => {
state.isOpen = flag;
}),
};
const storeFunction = jest.fn(() => state);
return {
__esModule: true,
default: Object.assign(storeFunction, {
getState: () => state,
setState: (newState: unknown) => Object.assign(state, newState),
subscribe: jest.fn(),
}),
useShallow: jest.fn(selector => selector(state)),
};
});
jest.mock("swiper/react", () => ({
useSwiper: jest.fn(),
Swiper: jest.fn(),
SwiperSlide: jest.fn(),
Navigation: jest.fn(),
Pagination: jest.fn(),
Scrollbar: jest.fn(),
A11y: jest.fn(),
Autoplay: jest.fn(),
EffectFade: jest.fn(),
}));