Skip to content

Commit 685d61b

Browse files
committed
test: CoinListWithSearchBar 컴포넌트 단위 테스트 추가
issue: #19
1 parent 9a08ba1 commit 685d61b

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { render, screen } from '@testing-library/react';
2+
import { createRoutesStub } from 'react-router';
3+
import { describe, expect, it } from 'vitest';
4+
5+
import userEvent from '@testing-library/user-event';
6+
import type { CoinListWithSearchBarProps } from '.';
7+
import CoinListWithSearchBar from '.';
8+
9+
const props: CoinListWithSearchBarProps = {
10+
coinList: [
11+
{
12+
coinIcon: <span>🪙</span>,
13+
name: '비트코인',
14+
ticker: 'BTC',
15+
to: '/coin/BTC',
16+
},
17+
{
18+
coinIcon: <span>🪙</span>,
19+
name: '이더리움',
20+
ticker: 'ETH',
21+
to: '/coin/ETH',
22+
},
23+
{
24+
coinIcon: <span>🪙</span>,
25+
name: '트럼프',
26+
ticker: 'TRUMP',
27+
to: '/coin/TRUMP',
28+
},
29+
],
30+
};
31+
32+
const Stub = createRoutesStub([
33+
{
34+
path: '/coin/:ticker',
35+
Component: () => <CoinListWithSearchBar {...props} />,
36+
},
37+
]);
38+
39+
describe('CoinListWithSearchBar 컴포넌트 테스트', () => {
40+
it('화면에 CoinListWithSearchBar가 렌더링 된다.', () => {
41+
render(<Stub initialEntries={['/coin/BTC']} />);
42+
43+
const coinListWithSearchBar = screen.getByTestId(
44+
'coin-list-with-search-bar',
45+
);
46+
47+
expect(coinListWithSearchBar).toBeInTheDocument();
48+
expect(screen.getAllByRole('link')).toHaveLength(3);
49+
});
50+
51+
it('사용자가 검색창에 티커를 입력하면 필터링된 리스트가 렌더링 된다.', async () => {
52+
const user = userEvent.setup();
53+
render(<Stub initialEntries={['/coin/BTC']} />);
54+
55+
const coinListWithSearchBar = screen.getByTestId(
56+
'coin-list-with-search-bar',
57+
);
58+
59+
expect(coinListWithSearchBar).toBeInTheDocument();
60+
61+
const input = screen.getByRole('textbox');
62+
await user.type(input, 'BTC');
63+
64+
expect(screen.getAllByRole('link')).toHaveLength(1);
65+
expect(screen.getAllByRole('link')[0]).toHaveTextContent('비트코인');
66+
});
67+
});

src/features/coin-search-list/ui/CoinListWithSearchBar/index.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { IconMagnifying } from '~/assets/svgs';
44
import type { CoinListItemProps } from '../CoinListItem';
55
import CoinListItem from '../CoinListItem';
66

7-
type CoinListWithSearchBarProps = {
7+
export type CoinListWithSearchBarProps = {
88
coinList: CoinListItemProps[];
99
};
1010

@@ -22,7 +22,10 @@ export default function CoinListWithSearchBar({
2222
};
2323

2424
return (
25-
<div className="flex h-full min-h-0 flex-1 flex-col bg-white">
25+
<div
26+
className="flex h-full min-h-0 flex-1 flex-col bg-white"
27+
data-testid="coin-list-with-search-bar"
28+
>
2629
<div className="px-2 py-3">
2730
<div className="relative w-full">
2831
<input

0 commit comments

Comments
 (0)