|
| 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 | +}); |
0 commit comments