// ํ
์คํธ ์ํ์ฝ๋
import React from 'react';
import { render } from '@testing-library/react';
import App from './App';
window.matchMedia = window.matchMedia || function() {
return {
matches : false,
addListener : function() {},
removeListener: function() {}
};
};
describe('<App/>', () => {
it('matches snapshot', () => {
const utils = render(<App/>);
expect(utils.container).toMatchSnapshot();
})
it('shows the props correctly', () => {
const utils = render(<App/>);
utils.getByText('ISSUER');
})
})
describe: ํ๋์ ํ ์คํธ ๊ทธ๋ฃน
it: ๋จ์ ํ ์คํธ
์ ์ฝ๋๋ฅผ ์คํ ์ src/snapshots ์์ App.test.js.snap ์์ฑ
์ปดํฌ๋ํธ๊ฐ ๋ ๋๋ง๋์ ๋ ์ค๋
์ท๊ณผ ๋ถ์ผ์นํ๋ฉด ํ
์คํธ๊ฐ ์คํจ
์ค๋
์ท ์
๋ฐ์ดํธ๋ ์คํ ์ฝ์์์ uํค
// Jest + enzyme
import React from 'react';
import Counter from './counter.jsx';
import { shallow, configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });
describe('<Counter />', () => {
it('์ฑ๊ณต์ ์ผ๋ก ๋ ๋๋ง๋์ด์ผ ํฉ๋๋ค.', () => {
const wrapper = shallow(<Counter />);
expect(wrapper.length).toBe(1);
});
it('ํ์ดํ ์ธํ์ด ๋ ๋๋ง๋์ด์ผ ํฉ๋๋ค.', () => {
const wrapper = shallow(<Counter />);
expect(wrapper.find('#title').length).toEqual(1);
});
it('ํ์ดํ์ด ๋ณ๊ฒฝ๋์ด์ผ ํฉ๋๋ค.', () => {
const wrapper = shallow(<Counter />);
wrapper.find('#title').simulate('change', { target: { value: '๊ฐ' } });
expect(wrapper.state().title).toBe('๊ฐ');
});
it('์ซ์๊ฐ ์ฌ๋ผ๊ฐ์ผ ํฉ๋๋ค.', () => {
const wrapper = shallow(<Counter />);
wrapper.find('#up').simulate('click');
wrapper.find('#up').simulate('click');
expect(wrapper.state().value).toBeLessThan(1);
});
});
<label for="username-input">์์ด๋</label>
<input id="username-input" />
const inputNode = getByLabelText('์์ด๋');
<input placeholder="์์ด๋" />;
const inputNode = getByPlaceholderText('์์ด๋');
<div>Hello World!</div>;
const div = getByText('Hello World!');
<img src="/awesome.png" alt="awesome image" />;
const imgAwesome = getByAltText('awesomse image');
<p>
<span title="React">๋ฆฌ์กํธ</span>๋ ์งฑ ๋ฉ์ง ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค.
</p>
<svg>
<title>Delete</title>
<g><path/></g>
</svg>
const spanReact = getByTitle('React');
const svgDelete = getByTitle('Delete');
<input value="text" />;
const input = getByDisplayValue('text');
<span role="button">์ญ์ </span>;
const spanRemove = getByRole('button');
<div data-testid="commondiv">ํํ div</div>;
const commonDiv = getByTestId('commondiv');
1. getByLabelText
2. getByPlaceholderText
3. getByText
4. getByDisplayValue
5. getByAltText
6. getByTitle
7. getByRole
8. getByTestId
fireEvent.์ด๋ฒคํธ์ด๋ฆ(DOM, ์ด๋ฒคํธ๊ฐ์ฒด);
fireEvent.change(myInput, { target: { value: 'hello world' } });
// src/Counter.js
import React, { useState, useCallback } from 'react';
const Counter = () => {
const [number, setNumber] = useState(0);
const onIncrease = useCallback(() => {
setNumber(number + 1);
}, [number]);
const onDecrease = useCallback(() => {
setNumber(number - 1);
}, [number]);
return (
<div>
<h2>{number}</h2>
<button onClick={onIncrease}>+1</button>
<button onClick={onDecrease}>-1</button>
</div>
);
};
// src/Counter.test.js
import React from 'react';
import { render, fireEvent } from 'react-testing-library';
import Counter from './Counter';
describe('<Counter />', () => {
it('matches snapshot', () => {
const utils = render(<Counter />);
expect(utils.container).toMatchSnapshot();
});
it('has a number and two buttons', () => {
const utils = render(<Counter />);
// ๋ฒํผ๊ณผ ์ซ์๊ฐ ์๋์ง ํ์ธ
utils.getByText('0');
utils.getByText('+1');
utils.getByText('-1');
});
it('increases', () => {
const utils = render(<Counter />);
const number = utils.getByText('0');
const plusButton = utils.getByText('+1');
// ํด๋ฆญ ์ด๋ฒคํธ๋ฅผ ๋๋ฒ ๋ฐ์์ํค๊ธฐ
fireEvent.click(plusButton);
fireEvent.click(plusButton);
expect(number).toHaveTextContent('2'); // jest-dom ์ ํ์ฅ matcher ์ฌ์ฉ
expect(number.textContent).toBe('2'); // textContent ๋ฅผ ์ง์ ๋น๊ต
});
it('decreases', () => {
const utils = render(<Counter />);
const number = utils.getByText('0');
const plusButton = utils.getByText('-1');
// ํด๋ฆญ ์ด๋ฒคํธ๋ฅผ ๋๋ฒ ๋ฐ์์ํค๊ธฐ
fireEvent.click(plusButton);
fireEvent.click(plusButton);
expect(number).toHaveTextContent('-2'); // jest-dom ์ ํ์ฅ matcher ์ฌ์ฉ
});
});