2022-07-10 00:03:21 +03:00
|
|
|
import { screen } from '@testing-library/react';
|
2023-04-13 22:48:29 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2023-08-02 10:01:44 +03:00
|
|
|
import type { TimeoutToggle } from '../../../../src/utils/helpers/hooks';
|
|
|
|
import { CreateShortUrlResult as createResult } from '../../../src/short-urls/helpers/CreateShortUrlResult';
|
|
|
|
import type { ShortUrlCreation } from '../../../src/short-urls/reducers/shortUrlCreation';
|
2022-07-10 20:44:49 +03:00
|
|
|
import { renderWithEvents } from '../../__helpers__/setUpTest';
|
2018-11-01 11:30:05 +03:00
|
|
|
|
|
|
|
describe('<CreateShortUrlResult />', () => {
|
2023-05-27 12:57:26 +03:00
|
|
|
const copyToClipboard = vi.fn();
|
|
|
|
const useTimeoutToggle = vi.fn(() => [false, copyToClipboard]) as TimeoutToggle;
|
2022-05-29 13:18:21 +03:00
|
|
|
const CreateShortUrlResult = createResult(useTimeoutToggle);
|
2022-11-07 20:55:12 +03:00
|
|
|
const setUp = (creation: ShortUrlCreation) => renderWithEvents(
|
|
|
|
<CreateShortUrlResult resetCreateShortUrl={() => {}} creation={creation} />,
|
2022-07-10 00:03:21 +03:00
|
|
|
);
|
2018-11-01 11:30:05 +03:00
|
|
|
|
|
|
|
it('renders an error when error is true', () => {
|
2022-11-07 20:55:12 +03:00
|
|
|
setUp({ error: true, saved: false, saving: false });
|
2022-07-06 19:30:33 +03:00
|
|
|
expect(screen.getByText('An error occurred while creating the URL :(')).toBeInTheDocument();
|
2018-11-01 11:30:05 +03:00
|
|
|
});
|
|
|
|
|
2022-11-07 20:55:12 +03:00
|
|
|
it.each([[true], [false]])('renders nothing when not saved yet', (saving) => {
|
|
|
|
const { container } = setUp({ error: false, saved: false, saving });
|
2022-07-06 19:30:33 +03:00
|
|
|
expect(container.firstChild).toBeNull();
|
2018-11-01 11:30:05 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('renders a result message when result is provided', () => {
|
2022-11-07 20:55:12 +03:00
|
|
|
setUp(
|
2023-04-13 22:48:29 +03:00
|
|
|
{ result: fromPartial({ shortUrl: 'https://s.test/abc123' }), saving: false, saved: true, error: false },
|
2022-11-07 20:55:12 +03:00
|
|
|
);
|
2023-01-18 00:53:49 +03:00
|
|
|
expect(screen.getByText(/The short URL is/)).toHaveTextContent('Great! The short URL is https://s.test/abc123');
|
2018-11-01 11:30:05 +03:00
|
|
|
});
|
|
|
|
|
2022-07-06 19:30:33 +03:00
|
|
|
it('Invokes tooltip timeout when copy to clipboard button is clicked', async () => {
|
2022-11-07 20:55:12 +03:00
|
|
|
const { user } = setUp(
|
2023-04-13 22:48:29 +03:00
|
|
|
{ result: fromPartial({ shortUrl: 'https://s.test/abc123' }), saving: false, saved: true, error: false },
|
2022-11-07 20:55:12 +03:00
|
|
|
);
|
2018-11-01 11:30:05 +03:00
|
|
|
|
2020-05-31 11:16:09 +03:00
|
|
|
expect(copyToClipboard).not.toHaveBeenCalled();
|
2022-07-06 19:30:33 +03:00
|
|
|
await user.click(screen.getByRole('button'));
|
2020-05-31 11:16:09 +03:00
|
|
|
expect(copyToClipboard).toHaveBeenCalledTimes(1);
|
2018-11-01 11:30:05 +03:00
|
|
|
});
|
|
|
|
});
|