|
| 1 | +import EditGroupForm from '@/components/GroupForm/GroupForm'; |
| 2 | +import { dateToYYMMDD } from '@/utils/dateUtils'; |
| 3 | +import { act, render, screen } from '@testing-library/react'; |
| 4 | +import userEvent from '@testing-library/user-event'; |
| 5 | +import { describe, expect, it } from 'vitest'; |
| 6 | + |
| 7 | +describe('EditGroupForm', () => { |
| 8 | + it('수정하는 경우 기본값을 전달할 수 있어야 한다.', () => { |
| 9 | + const startDate = new Date().toString(); |
| 10 | + const endDate = new Date().toString(); |
| 11 | + |
| 12 | + render( |
| 13 | + <EditGroupForm |
| 14 | + onSubmit={() => {}} |
| 15 | + name="name" |
| 16 | + description="description" |
| 17 | + startDate={startDate} |
| 18 | + endDate={endDate} |
| 19 | + memo="memo" |
| 20 | + />, |
| 21 | + ); |
| 22 | + const groupNameInput = screen.getByPlaceholderText(/모임명/) as HTMLInputElement; |
| 23 | + const groupDescriptionInput = screen.getByPlaceholderText(/모임 설명/) as HTMLInputElement; |
| 24 | + const groupDateStartInput = screen.getByLabelText(/모임 시작/) as HTMLInputElement; |
| 25 | + const groupDateEndInput = screen.getByLabelText(/모임 종료/) as HTMLInputElement; |
| 26 | + const groupMemoInput = screen.getByPlaceholderText(/메모/) as HTMLInputElement; |
| 27 | + |
| 28 | + expect(groupNameInput.value).toBe('name'); |
| 29 | + expect(groupDescriptionInput.value).toBe('description'); |
| 30 | + expect(groupDateStartInput.value).toBe(dateToYYMMDD(new Date(startDate))); |
| 31 | + expect(groupDateEndInput.value).toBe(dateToYYMMDD(new Date(endDate))); |
| 32 | + expect(groupMemoInput.value).toBe('memo'); |
| 33 | + }); |
| 34 | + it('모임명, 설명, 메모를 수정할 수 있어야 한다.', async () => { |
| 35 | + const user = userEvent.setup(); |
| 36 | + render(<EditGroupForm onSubmit={() => {}} />); |
| 37 | + |
| 38 | + const groupNameInput = screen.getByPlaceholderText(/모임명/) as HTMLInputElement; |
| 39 | + const groupDescriptionInput = screen.getByPlaceholderText(/모임 설명/) as HTMLInputElement; |
| 40 | + const groupDateStartInput = screen.getByLabelText(/모임 시작/) as HTMLInputElement; |
| 41 | + const groupDateEndInput = screen.getByLabelText(/모임 종료/) as HTMLInputElement; |
| 42 | + const groupMemoInput = screen.getByPlaceholderText(/메모/) as HTMLInputElement; |
| 43 | + |
| 44 | + expect(groupNameInput).toBeInTheDocument(); |
| 45 | + expect(groupDescriptionInput).toBeInTheDocument(); |
| 46 | + expect(groupDateStartInput).toBeInTheDocument(); |
| 47 | + expect(groupDateEndInput).toBeInTheDocument(); |
| 48 | + expect(groupMemoInput).toBeInTheDocument(); |
| 49 | + |
| 50 | + await act(async () => { |
| 51 | + // 기존 값이 존재하는 경우 type을 하게 되면 기존 값에 이를 추가하려고 함. |
| 52 | + // 그러므로 clear를 통해 기존 날짜 값을 초기화 해줘야 함. |
| 53 | + await user.clear(groupDateStartInput); |
| 54 | + await user.clear(groupDateEndInput); |
| 55 | + |
| 56 | + await user.type(groupNameInput, 'name'); |
| 57 | + await user.type(groupDescriptionInput, 'description'); |
| 58 | + await user.type(groupMemoInput, 'memo'); |
| 59 | + await user.type(groupDateStartInput, '2022-01-01'); |
| 60 | + await user.type(groupDateEndInput, '2022-01-02'); |
| 61 | + }); |
| 62 | + |
| 63 | + expect(groupNameInput).toHaveValue('name'); |
| 64 | + expect(groupDescriptionInput).toHaveValue('description'); |
| 65 | + expect(groupMemoInput).toHaveValue('memo'); |
| 66 | + expect(groupDateStartInput).toHaveValue('2022-01-01'); |
| 67 | + expect(groupDateEndInput).toHaveValue('2022-01-02'); |
| 68 | + }); |
| 69 | +}); |
0 commit comments