forked from kakao-tech-campus-2nd-step3/Team18_FE
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kakao-tech-campus-2nd-step3#150 from KimJi-An/test/k…
…akao-tech-campus-2nd-step3#148 Test/kakao-tech-campus-2nd-step3#148 외국인 등록 번호 및 비자 발급 일자 등록 폼, Header 테스트 코드 구현
- Loading branch information
Showing
6 changed files
with
142 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { renderWithProviders } from '@/__test__/test-utils'; | ||
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
import Header from '..'; | ||
import { fireEvent, screen } from '@testing-library/react'; | ||
import { server } from '@/mocks/server'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import ROUTE_PATH from '@/routes/path'; | ||
import { userLocalStorage } from '@/utils/storage'; | ||
import { UserData } from '@/types'; | ||
|
||
const mockEmployeeUser = { type: 'employee', profileImage: 'userProfileImage', name: 'userName' } as UserData; | ||
const mockEmployerUser = { type: 'employer', profileImage: 'employerProfileImage', name: 'employerName' } as UserData; | ||
|
||
vi.mock('react-router-dom', async () => { | ||
const actual = await vi.importActual('react-router-dom'); | ||
return { | ||
...actual, | ||
useNavigate: vi.fn(), | ||
}; | ||
}); | ||
|
||
describe('Header 테스트', () => { | ||
const mockNavigate = vi.fn(); | ||
|
||
beforeAll(() => { | ||
server.listen(); | ||
vi.mocked(useNavigate).mockImplementation(() => mockNavigate); | ||
}); | ||
afterEach(() => { | ||
server.resetHandlers(); | ||
vi.clearAllMocks(); | ||
}); | ||
afterAll(() => server.close()); | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('로고 클릭 시 홈페이지로 이동한다', () => { | ||
renderWithProviders(<Header />); | ||
const logo = document.querySelector('.logo'); | ||
expect(logo).not.toBeNull(); | ||
fireEvent.click(logo!); | ||
expect(mockNavigate).toHaveBeenCalledWith(ROUTE_PATH.HOME); | ||
}); | ||
|
||
it('로컬 스토리지에 저장된 사용자의 타입이 employee이면 근로자 마이페이지로 이동한다', () => { | ||
vi.spyOn(userLocalStorage, 'getUser').mockReturnValue(mockEmployeeUser); | ||
|
||
renderWithProviders(<Header />); | ||
|
||
const profileImage = screen.getByTestId('profile'); | ||
fireEvent.click(profileImage); | ||
|
||
expect(mockNavigate).toHaveBeenCalledWith(ROUTE_PATH.EMPLOYEE.EMPLOYEE_PAGE); | ||
}); | ||
|
||
it('로컬 스토리지에 저장된 사용자의 타입이 employer이면 고용주 마이페이지로 이동한다', () => { | ||
vi.spyOn(userLocalStorage, 'getUser').mockReturnValue(mockEmployerUser); | ||
|
||
renderWithProviders(<Header />); | ||
|
||
const profileImage = screen.getByTestId('profile'); | ||
fireEvent.click(profileImage); | ||
|
||
expect(mockNavigate).toHaveBeenCalledWith(ROUTE_PATH.MY_PAGE.EMPLOYER); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/features/registerVisa/__test__/VisaRegistrationForm.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { renderWithProviders } from '@/__test__/test-utils'; | ||
import { server } from '@/mocks/server'; | ||
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
import VisaRegistrationForm from '../VisaRegistrationForm'; | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import { fireEvent, screen, waitFor } from '@testing-library/react'; | ||
|
||
const queryClient = new QueryClient(); | ||
|
||
describe('VisaRegistrationForm 테스트', () => { | ||
beforeAll(() => server.listen()); | ||
afterEach(() => server.resetHandlers()); | ||
afterAll(() => server.close()); | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
const renderVisaRegistrationForm = () => { | ||
return renderWithProviders( | ||
<QueryClientProvider client={queryClient}> | ||
<VisaRegistrationForm /> | ||
</QueryClientProvider>, | ||
); | ||
}; | ||
|
||
it('외국인 등록 번호와 비자 발급 일자를 올바르게 입력하고 폼을 제출한다', async () => { | ||
const { getByLabelText, getByRole } = renderVisaRegistrationForm(); | ||
|
||
const foreignerNumberInput = getByLabelText('registerVisa.labels.foreigner_number'); | ||
const visaDateInput = getByLabelText('registerVisa.labels.visa_generate_date'); | ||
|
||
fireEvent.change(foreignerNumberInput, { target: { value: '123456-1234567' } }); | ||
fireEvent.change(visaDateInput, { target: { value: '2024-01-01' } }); | ||
fireEvent.submit(getByRole('button', { name: 'registerVisa.submit' })); | ||
|
||
const completeMessage = await screen.findByText('registerVisa.complete_message'); | ||
expect(completeMessage).toBeInTheDocument(); | ||
}); | ||
|
||
it('잘못된 외국인 등록 번호를 입력하면 에러 메시지를 표시하고 버튼이 비활성화된다', async () => { | ||
const { getByLabelText, getByRole } = renderVisaRegistrationForm(); | ||
const foreignerNumberInput = getByLabelText('registerVisa.labels.foreigner_number'); | ||
const submitButton = getByRole('button', { name: 'registerVisa.submit' }); | ||
|
||
fireEvent.change(foreignerNumberInput, { target: { value: 'wrong-format' } }); | ||
fireEvent.submit(submitButton); | ||
|
||
const errorMessage = await screen.findByText('registerVisa.error_message'); | ||
expect(errorMessage).toBeInTheDocument(); | ||
|
||
await waitFor(() => { | ||
expect(submitButton).toBeDisabled(); | ||
}); | ||
}); | ||
}); |