React Testing Library
React ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉ์ ์ ์ฅ์ ๊ฐ๊น๊ฒ ํ ์คํธํ ์ ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
BDD(Behavior Driven Development)
BDD๋? ํ์ ์ฃผ๋ ๊ฐ๋ฐ์ด๋ค. ํ์ ์ฃผ๋ ๊ฐ๋ฐ์ด ๋๋์ฒด ๋ญ๊ฐ..? ํ๊ตญ๋ง์ธ๋ฐ ๋๋ฌด ์ด๋ ต๋ค. ๊ธฐ๋ณธ ๊ฐ๋ ์ ์ดํดํ๋๋ฐ ๋์์ด ๋ ์์ TDD, BDD?
๊ธฐํ๋จ๊ณ์์ ๋ง๋ค์ด์ง ์ฌ์ฉ์ ๊ฒฝํ์ ์ฝ๋๋ก ์ฎ๊ฒจ์ ํ ์คํธ ํ๋ ๊ณผ์ ์ BDD๋ผ๊ณ ํ๋ค.
Given(์ฃผ์ด์ง ํ๊ฒฝ - ์ฌ์ฉ์๊ฐ ๋ก๊ทธ์ธ์ ํ๋ ค๊ณ ํ ๋)
When(ํ์ - ์ด๋ฆ์ ์ ๋ ฅํ๋ Input์ฐฝ์ ์ด๋ฆ์ ์ ๋ ฅํ๋ฉด)
Then(๊ฒฐ๊ณผ - ์ ๋ ฅํ ์ด๋ฆ์ด ํ๋ฉด์ ๋ณด์ฌ์ง๋ค)
BDD๋ TDD์์ ํ์๋ ๊ฐ๋ ์ผ๋ก BDD ํ ์คํธ๋ก ์ ์ ์๋๋ฆฌ์ค๋ฅผ ๊ฒ์ฆํ๊ณ , ์๋๋ฆฌ์์ ์ฌ์ฉ๋ ๋ชจ๋์ TDD๋ก ์์ฑ๋๋ค. BDD ํ ์คํธ ์ผ์ด์ค๋ก ์๋๋ฆฌ์ค ๊ฒ์ฆ์ ํ๋ค๋ณด๋ฉด ๊ธฐํ๋จ๊ณ์์ ๋๋ฝ๋ ๋ถ๋ถ๋ ์ฐพ์ ์ ์๋ค.
Test Code ์์ฑํด๋ณด๊ธฐ
๋ฒ์ฉ์ผ๋ก ์ฌ์ฉํ ์ ์๋ Text์ ๋ ฅ filed๋ฅผ ๋ง๋๋ ์ฐ์ต
import {render, screen} from '@testing-library/react'
import TextField from './Textfield'
test('TextField', ()=> {
//given
const label = 'Name'
const text = 'Tester'
const setText = () => {}
//when
<TextField
label={label}
placeholder=''
text={text}
setText={setText}
/>
//then
screen.getByLabelText('Name')
})
given ๋จ๊ณ์์ ํ์ํ ๊ฐ์ ์ค๋นํด์ฃผ๊ณ , ํด๋น ๊ฐ์ TextField์ ๋ฃ์์ ๋ ๊ธฐ๋ํ๋ ํ๋ฉด์ ํ ์คํธํ๋ค.
์ข ๋ ๋ค์ํ ํ ์คํธ ์ผ์ด์ค ์ถ๊ฐํ๊ธฐ
import { render, screen, fireEvent } from '@testing-library/react'
import TextField from './TextField'
const context = describe
describe("TextField", () => {
const label = 'Name'
const text = 'Tester'
const setText = jest.fn()
// ์ด๊ธฐํ ํด์ฃผ๊ธฐ
beforeEach(() => {
setText.mockClear()
})
// when
// ์ค๋ณต๋๋ ํจ์ ๊บผ๋ด๊ธฐ
function renderTextField() {
render((
<TextField
label={label}
text={text}
setText={setText}
/>
))
}
//then
if('render an input control', () => {
renderTextfield()
screen.getByLabelText('Name')
})
context('when user types text', () => {
if('calls the change handler', () => {
renderTextField()
//when
fireEvent.change(screen.getByLabelText(label), {
target: {
value: 'New Name',
}
})
//then
expect(setText).toBeCalledWith('New Name')
})
})
})
ํ ์คํธ ์ฝ๋๋ฅผ ๋ง๋๋๊ฑด ์ ๋ง ์ด๋ ค์..!
Last updated