입력 테스트
import {render, screen} from '@testing-library/react'
import mockRouter from 'next-frouter-mock'
const setup = (url : string) => {
mockRouter.setCurrentUrl(url)
render(<Header/>)
cosnt combobox = screen.getByRole('combobox', { name : '공개 여부'})
return { combobox }
}// Test
test('기본값으로 "전체"가 선택되어있다', async () => {
const { combobox } = setup();
expect(combobox).toHaveDisplayValue("전체");
});폼 유효성 검사 테스트
ReactHookForm에서 resolver를 이용하여, 유효성을 검증할 수 있다.
// 목 함수
function setup() {
const onClickSave = jest.fn();
const onValid = jest.fn();
const onInvalid = jest.fn();
render(
<PostForm
title="신규"
onClickSave={onClickSave}
onValid={onValid}
onInvalid={onInvalid}
/>
);
async function typeTitle(title: string) {
const textbox = screen.getByRole("textbox", { name: "제목" });
await user.type(textbox, title);
}
async function saveAsPublished() {
await user.click(screen.getByRole("swtich", { name: "공개 여부" }));
await user.click(screen.getByRole("button", { name: "공개하기" }));
}
async function saveAsDraft() {
await user.click(
scrren.getByRole("button", { name: "비공개 상태로 저장" })
);
}
return {
typeTitle,
saveAsDraft,
saveAsPublished,
onClickSave,
onValid,
onInvalid,
};
}toHaveErrorMessage
요소가 가지고 있는 aria-errormessage를 확인하는 matcher이다.
Matcher를 사용하기 전에, 문서를 읽거나
matchers.d.ts를 확인하면 좋을 것 같다.
Last updated