ํตํฉ ํ ์คํธ ์์ฑํ๊ธฐ - ์ํ ๊ด๋ฆฌ ๋ชจํน
์์์ ๋งํ ๋ ์ปดํฌ๋ํธ๋ฅผ ํ ์คํธํ๊ธฐ ์ํด ์ํ ๊ด๋ฆฌ๋ฅผ ๋ชจํนํ๋ ๋ฐฉ๋ฒ์ ์์์ผ๋ ์ด์ ์ ์ฉํด๋ณธ๋ค.
ProductInfoTable
const ProductInfoTable = () => {
const { cart, removeCartItem, changeCartItemCount } = useCartStore(state =>
pick(state, 'cart', 'removeCartItem', 'changeCartItemCount'),
);
const { user } = useUserStore(state => pick(state, 'user'));
return (
<TableContainer component={Paper} sx={{ wordBreak: 'break-word' }}>
<Table aria-label="์ฅ๋ฐ๊ตฌ๋ ๋ฆฌ์คํธ">
<TableBody>
{Object.values(cart).map(item => (
<ProductInfoTableRow
key={item.id}
item={item}
user={user}
removeCartItem={removeCartItem}
changeCartItemCount={changeCartItemCount}
/>
))}
</TableBody>
</Table>
</TableContainer>
);
};
๋จ์ ํ ์คํธ?
ํ์ ์ปดํฌ๋ํธ์ธ ProductInfoTableRow๋ ์ฌ๋ฌ ๊ธฐ๋ฅ(์ด๋ฒคํธ ํธ๋ค๋ฌ)์ ๊ฐ์ง๊ณ ์๋ ์ปดํฌ๋ํธ์ธ๋ฐ ๋ ์ ํํ ํ
์คํธ๋ฅผ ์ํด์๋ ์ด ์ปดํฌ๋ํธ๋จผ์ ํ
์คํธ๋ฅผ ํด์ผํ์ง ์์๊น? ์๋๊น?
๊ฐ์์์๋ ์ด์ฐจํผ ProductInfoTableRow๋ฅผ ๊ฒ์ฆํ ๋ ํ ์ ์๋๊ฑด ์ธ์๋ก ์ ๋ฌ๋ฐ์ ์ํ๋ ํจ์๋ฅผ ์ ๋๋ก ๋ ๋๋งํ๊ณ ํธ์ถํ๋์ง์ ๋ํ ์ฌ๋ถ ๋ฐ์ ๊ฒ์ฆํ ์ ์์ผ๋๊น ํตํฉํ
์คํธ์ ํฌํจํ์ฌ, ์ํ ๋ชฉ๋ก์ ์ ๋๋ก ์ถ๋ ฅํ๋์ง๋ฅผ ํ์ธํ๋๊ฒ ๋ ํจ๊ณผ์ ์ด๋ผ๊ณ ํ๋ค.
์ฌ๊ธฐ์ ์๋ฆฌ์กํด์ง๋๊ฒ ๊ทธ๋ฌ๋ฉด ์ ์ด์ ๋จ์ ํ ์คํธ๊ฐ ์ ํ์ํ๊ฑฐ์๋๋ผ? ์ ํธํจ์๋ ์ปค์คํ ํ ๋ง๊ณ ๋ ๋๋ง ์ปดํฌ๋ํธ๋ฅผ ๋จ์ ํ ์คํธ๋ก ๊ฒ์ฆ ํ ํ์๊ฐ ์ ์๋๊ฑฐ์ง?
- ์ฐพ์๋ณด๋ ์ฌ๋ฌ ๊ณณ์์ ์ฌ์ฉ๋์ด ๋ณต์กํ ์กฐ๊ฑด๋ถ ๋ ๋๋ง์ ํ๋ ์ปดํฌ๋ํธ์์๋ (
<Button variant={} size={} disabled={} onClick={} ...... />) ๋จ์ ํ ์คํธ๋ฅผ ํ๋๊ฒ์ด ์ข๊ณ ๊ทธ ์ธ์ ๋๋ถ๋ถ์ ๋ ๋๋ง ์ปดํฌ๋ํธ๋ค์ ์ฌ์ค ํตํฉํ ์คํธ์ ๊ฒ์ฆํ๋ค๊ณ ํ๋ค. - ๊ทธ๋๋ ์ฌ์ ํ ์ ๋ ํ ์คํธ์ ์ ์ฉ์ฑ์ ๋ํด ์๋ฌธ์ด ๋ ๋ค.
ํตํฉ ํ ์คํธ ์์ฑ
// ProductInfoTable.spec.jsx
beforeEach(() => {
mockUseUserStore({user: {id: 1}});
mockUseCartStore({
cart: {
6: {
id: 6,
title: 'Handmade Cotton Fish',
price: 809,
description:
'The slim & simple Maple Gaming Keyboard from Dev Byte comes with a sleek body and 7- Color RGB LED Back-lighting for smart functionality',
images: [
'https://user-images.githubusercontent.com/35371660/230712070-afa23da8-1bda-4cc4-9a59-50a263ee629f.png',
],
count: 3,
},
7: {
id: 7,
title: 'Awesome Concrete Shirt',
price: 442,
description:
'The Nagasaki Lander is the trademarked name of several series of Nagasaki sport bikes, that started with the 1984 ABC800J',
images: [
'https://user-images.githubusercontent.com/35371660/230762100-b119d836-3c5b-4980-9846-b7d32ea4a08f.png',
],
count: 4,
},
},
totalCount: 7,
totalPrice: 500,
});
});
์ด๋ ๊ฒ ์ ์ ์ ํตํด ์ํ๋ฅผ ๋ชจํนํ๋ค.
๊ทธ๋ฆฌ๊ณ ๋ค์๊ณผ ๊ฐ์ ํ์ํ ํ ์คํธ๋ค์ ์งํํ๋ฉด ๋๋ค.
it('์ฅ๋ฐ๊ตฌ๋์ ํฌํจ๋ ์์ดํ
๋ค์ ์ด๋ฆ, ์๋, ํฉ๊ณ๊ฐ ์ ๋๋ก ๋
ธ์ถ๋๋ค', async () => {
await render(<ProductInfoTable />);
const [firstItem, secondItem] = screen.getAllByRole('row');
expect(
within(firstItem).getByText('Handmade Cotton Fish'),
).toBeInTheDocument();
expect(within(firstItem).getByRole('textbox')).toHaveValue('3');
expect(within(firstItem).getByText('$2,427.00')).toBeInTheDocument();
expect(
within(secondItem).getByText('Awesome Concrete Shirt'),
).toBeInTheDocument();
expect(within(secondItem).getByRole('textbox')).toHaveValue('4');
expect(within(secondItem).getByText('$1,768.00')).toBeInTheDocument();
});
getBy, queryBy
it('ํน์ ์์ดํ
์ ์ญ์ ๋ฒํผ์ ํด๋ฆญํ ๊ฒฝ์ฐ ํด๋น ์์ดํ
์ด ์ฌ๋ผ์ง๋ค', async () => {
const { user } = await render(<ProductInfoTable />);
const [, secondItem] = screen.getAllByRole('row');
const deleteButton = within(secondItem).getByRole('button');
expect(screen.getByText('Awesome Concrete Shirt')).toBeInTheDocument();
await user.click(deleteButton);
expect(screen.queryByText('Awesome Concrete Shirt')).not.toBeInTheDocument();
});
getByText๋ DOM์ ํด๋น ํ
์คํธ๊ฐ ์กด์ฌํ๋์ง ํ์ธํ๋ค.
๊ทธ๋ฐ๋ฐ ์ญ์ ๋ฒํผ์ ๋๋ฅด๋ฉด ํด๋น ์์๊ฐ ์ฌ๋ผ์ ธ์ผํ๋๋ฐ ์ฌ๋ผ์ง๊ฒ์ ํ์ธํ๋ ค๋ฉด queryBy ๋งค์ฒ๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค๊ณ ํ๋ค.
๊ณต์๋ฌธ์์ ์ค๋ช
getBy...: Returns the matching node for a query, and throw a descriptive error if no elements match or if more than one match is found (usegetAllByinstead if more than one element is expected).queryBy...: Returns the matching node for a query, and returnnullif no elements match. This is useful for asserting an element that is not present. Throws an error if more than one match is found (usequeryAllByinstead if this is OK).
๊ทธ๋ฌ๋ฉด ์
queryBy๋ก ํต์ผํ์ง ์๊ณgetBy๋งค์ฒ๋ ์ฌ์ฉํ ๊น?
