|
1 | 1 | import { render, screen } from '@testing-library/react'; |
2 | 2 | import { fromPartial } from '@total-typescript/shoehorn'; |
| 3 | +import { createMemoryHistory } from 'history'; |
| 4 | +import { Router } from 'react-router'; |
| 5 | +import type { ShlinkCreateShortUrlData } from '../../src/api-contract'; |
3 | 6 | import { SettingsProvider } from '../../src/settings'; |
4 | 7 | import { CreateShortUrlFactory } from '../../src/short-urls/CreateShortUrl'; |
5 | 8 | import type { ShortUrlCreation } from '../../src/short-urls/reducers/shortUrlCreation'; |
| 9 | +import type { ShortUrlFormProps } from '../../src/short-urls/ShortUrlForm'; |
6 | 10 | import { checkAccessibility } from '../__helpers__/accessibility'; |
7 | 11 |
|
8 | 12 | describe('<CreateShortUrl />', () => { |
9 | | - const ShortUrlForm = () => <span>ShortUrlForm</span>; |
| 13 | + const ShortUrlForm = ({ initialState }: ShortUrlFormProps<ShlinkCreateShortUrlData>) => ( |
| 14 | + <span>ShortUrlForm ({initialState.longUrl})</span> |
| 15 | + ); |
10 | 16 | const shortUrlCreationResult = fromPartial<ShortUrlCreation>({}); |
11 | 17 | const createShortUrl = vi.fn(async () => Promise.resolve()); |
12 | 18 | const CreateShortUrl = CreateShortUrlFactory(fromPartial({ ShortUrlForm })); |
13 | | - const setUp = () => render( |
14 | | - <SettingsProvider value={{}}> |
15 | | - <CreateShortUrl |
16 | | - shortUrlCreation={shortUrlCreationResult} |
17 | | - createShortUrl={createShortUrl} |
18 | | - resetCreateShortUrl={() => {}} |
19 | | - /> |
20 | | - </SettingsProvider>, |
21 | | - ); |
| 19 | + const setUp = (longUrlQuery?: string) => { |
| 20 | + const history = createMemoryHistory(); |
| 21 | + if (longUrlQuery) { |
| 22 | + history.push({ search: `?long-url=${longUrlQuery}` }); |
| 23 | + } |
| 24 | + |
| 25 | + return render( |
| 26 | + <Router location={history.location} navigator={history}> |
| 27 | + <SettingsProvider value={{}}> |
| 28 | + <CreateShortUrl |
| 29 | + shortUrlCreation={shortUrlCreationResult} |
| 30 | + createShortUrl={createShortUrl} |
| 31 | + resetCreateShortUrl={() => {}} |
| 32 | + /> |
| 33 | + </SettingsProvider> |
| 34 | + </Router>, |
| 35 | + ); |
| 36 | + }; |
22 | 37 |
|
23 | 38 | it('passes a11y checks', () => checkAccessibility(setUp())); |
24 | 39 |
|
25 | | - it('renders computed initial state', () => { |
26 | | - setUp(); |
27 | | - expect(screen.getByText('ShortUrlForm')).toBeInTheDocument(); |
| 40 | + it.each([undefined, 'https://example.com'])('renders initial long URL', (longUrl) => { |
| 41 | + setUp(longUrl); |
| 42 | + expect(screen.getByText(`ShortUrlForm (${longUrl ?? ''})`)).toBeInTheDocument(); |
28 | 43 | }); |
29 | 44 | }); |
0 commit comments