const button = screen.getByRole('button', {name: 'Click Me'})
fireEvent.click(button)
await screen.findByText('Clicked once')
fireEvent.click(button)
await screen.findByText('Clicked twice')
function waitFor<T>(
callback: () => T | Promise<T>,
options?: {
container?: HTMLElement
timeout?: number //1000ms
interval?: number //50ms
onTimeout?: (error: Error) => Error // 타임아웃시 오류 출력
mutationObserverOptions?: MutationObserverInit // 값 변경시 콜백 다시실행
},
): Promise<T>
function waitForElementToBeRemoved<T>(
callback: (() => T) | T,
options?: {
container?: HTMLElement
timeout?: number
interval?: number
onTimeout?: (error: Error) => Error
mutationObserverOptions?: MutationObserverInit
},
): Promise<void>
// 실제 예시
const el = document.querySelector('div.getOuttaHere')
waitForElementToBeRemoved(document.querySelector('div.getOuttaHere')).then(() =>
console.log('Element no longer in DOM'),
)
el.setAttribute('data-neat', true)
el.parentElement.removeChild(el)
// 1. queryBy 메서드를 사용해 toBeNull 체크
const submitButton = screen.queryByText('submit')
expect(submitButton).toBeNull()
// 2. 여러개가 들어오는 경우 queryAllBy 메서드를 사용해 길이 확인
const submitButtons = screen.queryAllByText('submit')
expect(submitButtons).toHaveLength(0) // expect no elements
// 3. not.toBeInTheDocument 사용
import '@testing-library/jest-dom'
const submitButton = screen.queryByText('submit')
expect(submitButton).not.toBeInTheDocument()
출처:
https://testing-library.com/docs/dom-testing-library/api-async