|
| 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 | +}); |
0 commit comments