[TIL] 엘리스 SW 엔지니어 트랙 Day 061

📖 오늘 배운 내용 - 2022.01.15
- React 테스팅
- jest
- react-testing-library
📝 React 테스팅
[React] React 테스팅
🎯 React 테스팅 📝 코드 테스트가 필요한 경우 코드를 작성하고 나면, 원하는 대로 동작하는지 알기 위해 코드에 버그가 있으면, 어떤 상황에서 버그가 발생하는지를 알기 위해 코드를 리팩토
lakelouise.tistory.com
📝 jest
[jest] jest란?
🎯 jest facebook에서 오픈소스화한 테스팅 프레임워크 assertion 함수들, test runner, mock 라이브러리 등을 모두 제공한다. create-react-app에서 기본적으로 사용된다. (npm test) 사용성이 좋고, 가장 인기..
lakelouise.tistory.com
📝 Assertion Matchers 활용
[Jest] Assertion Matchers 활용
🎯 Assertion Matchers 활용 toBe() : 객체의 내용을 비교 toEqual() : 객체 자체를 비교할 toContain() toMatch() toThrow() function isPythagorean(a, b, c) { return a * a + b * b === c * c } function cre..
lakelouise.tistory.com
📝 Async assertion
[Jest] Async assertion
🎯 Async assertion callback 패턴의 경우 test() 함수가 제공하는 done() 함수를 활용하여 콜백이 끝나고 done()을 호출이 된다. 에러가 발생하면 done()의 인자로 에러를 넘긴다. Promise 패턴의 경우 async/aw..
lakelouise.tistory.com
📝 react-testing-library
[react-testing-library] react-testing-library란?
🎯 react-testing-library 테스트가 소프트웨어가 사용되는 모습을 닮을수록, 테스트를 더욱 신뢰할 수 있게 된다. The more your tests resemble the way your software is used, the more confidence they can..
lakelouise.tistory.com
📝 쿼리의 우선순위
[react-testing-library] 쿼리의 우선순위
🎯 쿼리의 우선순위 유저가 페이지를 이동하는 방식에 가까운 쿼리일수록 우선순위가 높다. 접근성 높은 HTML을 작성할수록 테스트가 용이한 코드가 된다. 📝 ByRole (제일 높음) accessibility tree에
lakelouise.tistory.com
📝 유저 이벤트
[react-testing-library] 유저 이벤트
🎯 유저 이벤트 📝 user-event 내장 이벤트 함수인 fireEvent, createEvent를 좀 더 직관적이고 범용적으로 사용할 수 있도록 만든 라이브러리 click, type, keyboard, upload, hover, tab 등 유저가 실제로 웹페..
lakelouise.tistory.com
📝 예제
🛠 App.js
import React, { useState } from "react"; function SimpleToggle() { const [show, setShow] = useState(false) const handleClick = () => setShow(b => !b) return ( <div> {!show && <div>유저 정보를 보려면 버튼을 누르세요.</div>} {show && ( <ul> <li>Email - test@test.com</li> <li>Address - 서울시 </li> </ul> )} <button onClick={handleClick}> {!show ? "유저정보 보기 " : "유저정보 가리기"} </button> </div> ) } export default SimpleToggle;
🛠 App.test.js
import { screen, render } from "@testing-library/react"; import userEvent from '@testing-library/user-event'; import SimpleToggle from "./App"; describe("앱을 렌더링합니다.", () => { test("버튼이 있습니다.", () => { render(<SimpleToggle />); const button = screen.getByRole('button', { name: '유저정보 보기' }) expect(button).toBeInTheDocument() }); test("버튼을 누르지 않았을 시, 유저 정보 안내문이 보입니다.", () => { render(<SimpleToggle />); const text = screen.getByText('유저 정보를 보려면 버튼을 누르세요.') expect(text).toBeInTheDocument() }); }); describe("토글 기능을 테스트합니다.", () => { test("버튼을 눌렀을 시, 유저 정보가 보입니다.", () => { render(<SimpleToggle />); const infoText = /유저 정보를 보려면 버튼을 누르세요./i const text = screen.getByText(infoText) expect(text).toBeInTheDocument() const button = screen.getByRole('button', { name: '유저정보 보기' }) userEvent.click(button) // 버튼을 클릭 expect( screen.queryByText(infoText) ).not.toBeInTheDocument() // 위에서 찾은 텍스트가 보이지 않는지 체크 const email = screen.getByText('Email - elice@elicer.com') expect(email).toBeInTheDocument() const address = screen.getByText('Address - 서울시 강남구 테헤란로 401') expect(address).toBeInTheDocument() expect(button).toHaveTextContent('유저정보 가리기') }); test("버튼을 두번 누르면, 유저 정보가 보이지 않습니다.", () => { render(<SimpleToggle />); const button = screen.getByRole('button', { name: '유저정보 보기' }) userEvent.click(button, { clickCount: 1}) // 버튼 카운트를 명시할 수 있다. 한번만 누른 경우! const email = screen.getByText("Email - elice@elicer.com") expect(email).toBeInTheDocument() userEvent.click(button, { clickCount: 1}) expect(email).not.toBeInTheDocument() }); });
📕 1 단계
- 유저 정보 보기" 버튼을 찾기
const button = screen.getByRole('button', { name: '유저정보 보기' })
- 버튼이 존재하는지 체크
expect(button).toBeInTheDocument()
- 코드 작성
function SimpleToggle() { return ( <button>유저정보 보기</button> ) }
📕 2단계
- 텍스트를 찾기
const text = screen.getByText('유저 정보를 보려면 버튼을 누르세요.');
- 텍스트가 존재하는지 체크
expect(text).toBeInTheDocument()
📕 그 후
- userEvent.click(button)
-
expect( screen.queryByText(infoText) ).not.toBeInTheDocument() // 위에서 찾은 텍스트가 보이지 않는지 체크
💡 오늘 깨달은 것
- 테스트 코드 뭔가 재밌다.
- 초반 생산성은 떨어질 수 있지만 견고한 코드를 작성할 수 있을 거 같다.
'프로그래밍 > Today I Learned' 카테고리의 다른 글
[TIL] 엘리스 SW 엔지니어 트랙 Day 063 (0) | 2022.01.19 |
---|---|
[TIL] 엘리스 SW 엔지니어 트랙 Day 062 (0) | 2022.01.18 |
[TIL] 엘리스 SW 엔지니어 트랙 Day 060 (0) | 2022.01.17 |
[TIL] 엘리스 SW 엔지니어 트랙 Day 059 (0) | 2022.01.17 |
[TIL] 엘리스 SW 엔지니어 트랙 Day 058 (0) | 2022.01.16 |
댓글
이 글 공유하기
다른 글
-
[TIL] 엘리스 SW 엔지니어 트랙 Day 063
[TIL] 엘리스 SW 엔지니어 트랙 Day 063
2022.01.19 -
[TIL] 엘리스 SW 엔지니어 트랙 Day 062
[TIL] 엘리스 SW 엔지니어 트랙 Day 062
2022.01.18 -
[TIL] 엘리스 SW 엔지니어 트랙 Day 060
[TIL] 엘리스 SW 엔지니어 트랙 Day 060
2022.01.17 -
[TIL] 엘리스 SW 엔지니어 트랙 Day 059
[TIL] 엘리스 SW 엔지니어 트랙 Day 059
2022.01.17
댓글을 사용할 수 없습니다.