Considerations for fireEvent[Testing Library]

SnowCat·2023년 3월 9일
0

Testing Library

목록 보기
10/11
post-thumbnail

Interactions vs events

  • fireEvent는 유저가 상호작용하는 것과 완벽히 일치하지 않음
    가령 fireEvent.click 메서드를 버튼에서 실행하는 경우 실제 유저 행동에서 나오는 아래의 5단계 동작을 실행하지 않음
    • fireEvent.mouseOver
    • fireEvent.mouseMove
    • fireEvent.mouseDown
    • element.focus() (버튼을 focus 할 수 있는 경우)
    • fireEvent.mouseUp

Alternatives

  • Keydown을 사용하는 경우에는 아래와 같이 키보드 이벤트를 받을수 있는지도 같이 체크해줄 필요가 있음
// fireEvent.keyDown(getByText('click me')); 대신 아래 코드 사용
 getByText('click me').focus();
 fireEvent.keyDown(document.activeElement || document.body);
  • Focus를 사용하는 경우 객체를 선택한 다음 focus메서드를 사용해 실제 이벤트를 받을 수 있는지를 체크해줄 필요가 있음
// fireEvent.focus(getByText('focus me')); 대신 아래 코드 사용
getByText('focus me').focus();
  • 다른 상호작용에서 실제 유저의 행동을 정확하게 확인할 필요가 있을 경우에는 user-event 메서드를 사용하거나, 실제 환경을 기반으로 하는 테스트를 진행해야 함

출처:
https://testing-library.com/docs/guide-events

profile
냐아아아아아아아아앙

0개의 댓글