From d49d4eb26aaecfe27b01becfab5c9fd383783d7d Mon Sep 17 00:00:00 2001 From: eunhak Date: Sun, 30 Jun 2024 23:23:28 +0900 Subject: [PATCH] =?UTF-8?q?test=20:=20test=EC=BC=80=EC=9D=B4=EC=8A=A4=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/sign/sign.test.tsx | 69 +++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/components/sign/sign.test.tsx diff --git a/src/components/sign/sign.test.tsx b/src/components/sign/sign.test.tsx new file mode 100644 index 0000000..ddc829e --- /dev/null +++ b/src/components/sign/sign.test.tsx @@ -0,0 +1,69 @@ +import React from 'react'; +import { act, render, fireEvent, screen } from '@testing-library/react'; +import SigninForm from './signin/SigninForm'; +// import { signin } from '@/api/auth/auth.post.api'; +// import { useRouter } from 'next/navigation'; +// import { setCookie } from '@/utils/cookies'; +// import { useMutation } from '@tanstack/react-query'; + +jest.mock('@/api/auth/auth.post.api'); +jest.mock('next/navigation', () => ({ useRouter: jest.fn() })); +jest.mock('@/utils/cookies', () => ({ setCookie: jest.fn() })); +jest.mock('@tanstack/react-query', () => ({ useMutation: jest.fn() })); + +jest.mock('next/navigation', () => ({ + useRouter: jest.fn() +})); + +const mockMutate = jest.fn(); +jest.mock('@tanstack/react-query', () => ({ + useMutation: () => ({ + mutate: mockMutate, + mutateAsync: jest.fn(), + data: undefined, + error: undefined, + isLoading: false + }) +})); + +describe('SigninForm Component', () => { + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('로그인 성공 시 홈페이지로 리다이렉트', async () => { + render(); + const emailInput = screen.getByPlaceholderText('이메일을 입력해주세요.'); + const passwordInput = screen.getByPlaceholderText('비밀번호를 입력해주세요.'); + const submitButton = screen.getByText('로그인'); + + await act(async () => { + fireEvent.change(emailInput, { target: { value: 'wrong@example.com' } }); + fireEvent.change(passwordInput, { target: { value: 'wrongPassword' } }); + fireEvent.click(submitButton); + }); + + // await waitFor(() => expect(mockPush).toHaveBeenCalledWith('/')); + }); + + it('잘못된 이메일 또는 비밀번호 입력 시 에러 메시지 표시', async () => { + // mockMutate.mockImplementationOnce((_, { onError }) => { + // onError(new Error('*아이디 혹은 비밀번호가 일치하지 않습니다')); + // }); + + render(); + const emailInput = screen.getByPlaceholderText('이메일을 입력해주세요.'); + const passwordInput = screen.getByPlaceholderText('비밀번호를 입력해주세요.'); + const submitButton = screen.getByText('로그인'); + await act(async () => { + fireEvent.change(emailInput, { target: { value: 'wrong@example.com' } }); + fireEvent.change(passwordInput, { target: { value: 'wrongPassword' } }); + fireEvent.click(submitButton); + }); + // await waitFor(() => + // expect( + // screen.getByText('*아이디 혹은 비밀번호가 일치하지 않습니다') + // ).toBeInTheDocument() + // ); + }); +});