Skip to content

Commit 59e4481

Browse files
committed
test: 비밀번호 변경 폼 테스트 코드 작성
1 parent 44744e2 commit 59e4481

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed

src/tests/ChangePassword.test.tsx

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import supabase from '@/supabase';
2+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
3+
import { updateUserResponseFixture } from './fixtures/userFixture';
4+
import { AuthError } from '@supabase/supabase-js';
5+
import { act, render, screen, waitFor } from '@testing-library/react';
6+
import ChangePasswordForm from '@/components/ChangePassword/ChangePasswordForm';
7+
import userEvent from '@testing-library/user-event';
8+
import { sessionFixture } from './fixtures/sessionFixture';
9+
import wrapper from './helpers/wrapper';
10+
11+
vi.mock('@/supabase');
12+
13+
describe('ChangePassword', async () => {
14+
beforeEach(() => {
15+
render(<ChangePasswordForm />, { wrapper });
16+
});
17+
afterEach(() => {
18+
vi.resetAllMocks();
19+
});
20+
21+
it('비밀번호 변경 요청을 보낼 수 있어야 한다.', async () => {
22+
const user = userEvent.setup();
23+
vi.mocked(supabase.auth.updateUser).mockResolvedValue({
24+
data: {
25+
user: updateUserResponseFixture,
26+
},
27+
error: null,
28+
});
29+
vi.mocked(supabase.auth.getSession).mockResolvedValue({
30+
data: {
31+
session: sessionFixture,
32+
},
33+
error: null,
34+
});
35+
36+
const passwordInput = screen.getByPlaceholderText(//);
37+
const submitButton = screen.getByRole('button', { name: // });
38+
39+
await act(async () => {
40+
await user.type(passwordInput, 'someNewPassword');
41+
await user.click(submitButton);
42+
});
43+
44+
expect(supabase.auth.updateUser).toBeCalled();
45+
});
46+
47+
it('비밀번호 변경 성공시 로그인 되었다면 로그아웃 요청을 호출해야 한다.', async () => {
48+
const user = userEvent.setup();
49+
50+
vi.mocked(supabase.auth.updateUser).mockResolvedValue({
51+
data: {
52+
user: updateUserResponseFixture,
53+
},
54+
error: null,
55+
});
56+
vi.mocked(supabase.auth.getSession).mockResolvedValue({
57+
data: {
58+
session: sessionFixture,
59+
},
60+
error: null,
61+
});
62+
vi.mocked(supabase.auth.signOut).mockResolvedValue({
63+
error: new Error('some Error') as AuthError,
64+
});
65+
66+
const passwordInput = screen.getByPlaceholderText(//);
67+
const submitButton = screen.getByRole('button', { name: // });
68+
69+
await act(async () => {
70+
await user.type(passwordInput, 'someNewPassword');
71+
await user.click(submitButton);
72+
});
73+
74+
await waitFor(() => {});
75+
76+
expect(supabase.auth.updateUser).toBeCalled();
77+
expect(supabase.auth.signOut).toBeCalled();
78+
});
79+
80+
it('비밀번호 변경 실패시 에러 다이얼로그를 보여줄 수있어야 한다.', async () => {
81+
const user = userEvent.setup();
82+
vi.mocked(supabase.auth.updateUser).mockResolvedValue({
83+
data: {
84+
user: null,
85+
},
86+
error: new Error('some error') as AuthError,
87+
});
88+
vi.mocked(supabase.auth.getSession).mockResolvedValue({
89+
data: {
90+
session: sessionFixture,
91+
},
92+
error: null,
93+
});
94+
95+
const passwordInput = screen.getByPlaceholderText(//);
96+
const submitButton = screen.getByRole('button', { name: // });
97+
98+
await act(async () => {
99+
await user.type(passwordInput, 'somePassword');
100+
await user.click(submitButton);
101+
});
102+
103+
await waitFor(() => {});
104+
105+
const errorDialog = screen.queryByText('some error');
106+
107+
expect(errorDialog).toBeInTheDocument();
108+
});
109+
});
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Session } from '@supabase/supabase-js';
2+
3+
export const sessionFixture: Session = {
4+
access_token: 'some access token',
5+
expires_in: new Date().getTime(),
6+
refresh_token: 'some refresh token',
7+
token_type: 'jwt',
8+
user: {
9+
id: '1d7a5662-922e-4167-932d-4dda46f67ef2',
10+
aud: 'authenticated',
11+
role: 'authenticated',
12+
13+
email_confirmed_at: '2024-05-04 04:34:02.297625+00',
14+
invited_at: undefined,
15+
confirmation_sent_at: '2024-05-04 04:33:46.652098+00',
16+
recovery_sent_at: undefined,
17+
email_change_sent_at: undefined,
18+
last_sign_in_at: '2024-05-04 04:34:31.223951+00',
19+
app_metadata: {
20+
provider: 'email',
21+
providers: ['email'],
22+
},
23+
user_metadata: {
24+
sub: '1d7a5662-922e-4167-932d-4dda46f67ef2',
25+
name: '최기환',
26+
27+
phone: '01066069806',
28+
nickName: 'gihwan-dev',
29+
email_verified: false,
30+
phone_verified: false,
31+
},
32+
created_at: '2024-05-04 04:33:46.644465+00',
33+
updated_at: '2024-05-04 07:38:32.864951+00',
34+
phone: undefined,
35+
phone_confirmed_at: undefined,
36+
confirmed_at: '2024-05-04 04:34:02.297625+00',
37+
is_anonymous: false,
38+
},
39+
expires_at: new Date().getTime(),
40+
provider_refresh_token: 'some provider refresh token',
41+
provider_token: 'some provider token',
42+
};

src/tests/fixtures/userFixture.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { User } from '@supabase/supabase-js';
2+
3+
export const updateUserResponseFixture: User = {
4+
id: '1d7a5662-922e-4167-932d-4dda46f67ef2',
5+
aud: 'authenticated',
6+
role: 'authenticated',
7+
8+
email_confirmed_at: '2024-05-04 04:34:02.297625+00',
9+
invited_at: undefined,
10+
confirmation_sent_at: '2024-05-04 04:33:46.652098+00',
11+
recovery_sent_at: undefined,
12+
email_change_sent_at: undefined,
13+
last_sign_in_at: '2024-05-04 04:34:31.223951+00',
14+
app_metadata: {
15+
provider: 'email',
16+
providers: ['email'],
17+
},
18+
user_metadata: {
19+
sub: '1d7a5662-922e-4167-932d-4dda46f67ef2',
20+
name: '최기환',
21+
22+
phone: '01066069806',
23+
nickName: 'gihwan-dev',
24+
email_verified: false,
25+
phone_verified: false,
26+
},
27+
created_at: '2024-05-04 04:33:46.644465+00',
28+
updated_at: '2024-05-04 07:38:32.864951+00',
29+
phone: undefined,
30+
phone_confirmed_at: undefined,
31+
confirmed_at: '2024-05-04 04:34:02.297625+00',
32+
is_anonymous: false,
33+
};

0 commit comments

Comments
 (0)