Skip to content

Commit 766e215

Browse files
committed
feat: 마이페이지 테스트 코드 작성
1 parent 40ca7a2 commit 766e215

File tree

3 files changed

+150
-2
lines changed

3 files changed

+150
-2
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
2+
import { act, render, screen, waitFor } from '@testing-library/react';
3+
import userEvent from '@testing-library/user-event';
4+
5+
import { API } from '@/api';
6+
import { ModalProvider } from '@/components/ui';
7+
import { formatISO } from '@/lib/formatDateTime';
8+
import { server } from '@/mock/server';
9+
import { mockUserItems } from '@/mock/service/user/user-mock';
10+
import { AuthProvider } from '@/providers';
11+
12+
import MyPage from './page';
13+
14+
jest.mock('next/navigation', () => ({
15+
useRouter: () => ({
16+
push: jest.fn(),
17+
replace: jest.fn(),
18+
prefetch: jest.fn(),
19+
}),
20+
}));
21+
22+
const createTestQueryClient = () =>
23+
new QueryClient({
24+
defaultOptions: {
25+
queries: {
26+
retry: false, // 테스트에서 재시도 비활성화
27+
gcTime: Infinity, // Jest 환경에서 카비지 컬렉션을 위한 타이머 생성 방지
28+
},
29+
},
30+
});
31+
32+
const renderWithQueryClient = async (component: React.ReactElement) => {
33+
// 각 테스트마다 새로운 QueryClient 생성하여 독립적인 상태 유지 (useState 없이)
34+
const testQueryClient = createTestQueryClient();
35+
let renderResult;
36+
await act(async () => {
37+
renderResult = render(
38+
<QueryClientProvider client={testQueryClient}>
39+
<AuthProvider>
40+
<ModalProvider>{component}</ModalProvider>
41+
</AuthProvider>
42+
</QueryClientProvider>,
43+
);
44+
});
45+
46+
return renderResult;
47+
};
48+
49+
describe('마이 페이지 테스트', () => {
50+
beforeAll(() => {
51+
server.listen();
52+
});
53+
afterEach(() => {
54+
server.resetHandlers();
55+
});
56+
afterAll(() => server.close());
57+
58+
test('내 프로필 정보가 올바르게 표시되는지 테스트', async () => {
59+
const id = 1;
60+
const me = mockUserItems.find((item) => item.userId === id)!;
61+
62+
await renderWithQueryClient(<MyPage />);
63+
64+
await screen.findByRole('img', { name: '프로필 이미지' });
65+
66+
expect(screen.getByRole('heading', { level: 1 })).toHaveTextContent(me.nickName);
67+
expect(screen.getByText(me.profileMessage)).toBeInTheDocument();
68+
69+
expect(screen.getByText('팔로워')).toBeInTheDocument();
70+
expect(screen.getByText(me.followersCnt.toLocaleString())).toBeInTheDocument();
71+
expect(screen.getByText('팔로잉')).toBeInTheDocument();
72+
expect(screen.getByText(me.followeesCnt.toLocaleString())).toBeInTheDocument();
73+
74+
expect(screen.getByText('MBTI')).toBeInTheDocument();
75+
expect(screen.getByText(me.mbti)).toBeInTheDocument();
76+
expect(screen.getByText('가입 일자')).toBeInTheDocument();
77+
expect(screen.getByText(formatISO(me.createdAt))).toBeInTheDocument();
78+
expect(screen.getByText('모임 참여')).toBeInTheDocument();
79+
expect(screen.getByText(`${me.groupJoinedCnt}회`));
80+
expect(screen.getByText('모임 생성')).toBeInTheDocument();
81+
expect(screen.getByText(`${me.groupCreatedCnt}회`));
82+
});
83+
84+
test('설정 화면이 올바르게 표시되는지 테스트', async () => {
85+
await renderWithQueryClient(<MyPage />);
86+
87+
expect(await screen.findByText('알림 받기')).toBeInTheDocument();
88+
expect(screen.getByText('로그아웃')).toBeInTheDocument();
89+
expect(screen.getByText('회원탈퇴')).toBeInTheDocument();
90+
});
91+
92+
test('프로필 편집 모달이 정상적으로 열리는지 테스트', async () => {
93+
const user = userEvent.setup();
94+
95+
await renderWithQueryClient(<MyPage />);
96+
97+
const profileEditButton = await screen.findByRole('button', { name: '프로필 수정하기' });
98+
await user.click(profileEditButton);
99+
100+
await waitFor(() => {
101+
expect(screen.getByRole('heading', { level: 2, name: '프로필 수정' })).toBeInTheDocument();
102+
});
103+
});
104+
105+
test('알림 설정이 정상적으로 동작하는지 테스트', async () => {
106+
const user = userEvent.setup();
107+
108+
const updateNotificationSpy = jest.spyOn(API.userService, 'updateMyNotification');
109+
await renderWithQueryClient(<MyPage />);
110+
111+
const notificationButton = await screen.findByRole('button', { name: '알림 받기' });
112+
await user.click(notificationButton);
113+
114+
await waitFor(() => {
115+
expect(updateNotificationSpy).toHaveBeenCalled();
116+
});
117+
});
118+
119+
test('로그아웃이 정상적으로 동작하는지 테스트', async () => {
120+
const user = userEvent.setup();
121+
const logoutSpy = jest.spyOn(API.authService, 'logout');
122+
await renderWithQueryClient(<MyPage />);
123+
124+
const logoutButton = await screen.findByRole('button', { name: '로그아웃' });
125+
await user.click(logoutButton);
126+
127+
await waitFor(() => {
128+
expect(logoutSpy).toHaveBeenCalled();
129+
});
130+
});
131+
132+
test('회원탈퇴가 정상적으로 동작하는지 테스트', async () => {
133+
const user = userEvent.setup();
134+
const withdrawSpy = jest.spyOn(API.authService, 'withdraw');
135+
await renderWithQueryClient(<MyPage />);
136+
137+
const withdrawButton = await screen.findByRole('button', { name: '회원탈퇴' });
138+
await user.click(withdrawButton);
139+
140+
await waitFor(() => {
141+
expect(withdrawSpy).toHaveBeenCalled();
142+
});
143+
});
144+
});

src/mock/service/auth/auth-handlers.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,8 @@ const refreshMock = http.post('*/api/v1/auth/refresh', async ({ cookies }) => {
101101
return HttpResponse.json(createMockSuccessResponse<RefreshResponse>(response));
102102
});
103103

104-
export const authHandlers = [signupMock, loginMock, logoutMock, refreshMock];
104+
const withdrawMock = http.delete('*/api/v1/auth/withdraw', async () => {
105+
return HttpResponse.json(createMockSuccessResponse<void>(undefined));
106+
});
107+
108+
export const authHandlers = [signupMock, loginMock, logoutMock, refreshMock, withdrawMock];

src/mock/service/user/user-handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ const unfollowUserMock = http.delete(`*/users/unfollow`, ({ request }) => {
142142

143143
export const userHandlers = [
144144
followUserMock,
145+
getMeItemMock,
145146
getUserItemMock,
146147
updateMyNotificationMock,
147-
getMeItemMock,
148148
updateUserItemMock,
149149
getNicknameAvailabilityMock,
150150
getEmailAvailabilityMock,

0 commit comments

Comments
 (0)