2022-07-10 00:03:21 +03:00
|
|
|
import { screen } from '@testing-library/react';
|
2020-08-30 10:59:14 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { ShortUrl } from '../../../src/short-urls/data';
|
2023-02-18 13:11:01 +03:00
|
|
|
import { CreateShortUrlResult as createResult } from '../../../src/short-urls/helpers/CreateShortUrlResult';
|
|
|
|
import type { ShortUrlCreation } from '../../../src/short-urls/reducers/shortUrlCreation';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { TimeoutToggle } from '../../../src/utils/helpers/hooks';
|
2022-07-10 20:44:49 +03:00
|
|
|
import { renderWithEvents } from '../../__helpers__/setUpTest';
|
2018-11-01 11:30:05 +03:00
|
|
|
|
|
|
|
describe('<CreateShortUrlResult />', () => {
|
2020-05-31 11:16:09 +03:00
|
|
|
const copyToClipboard = jest.fn();
|
2022-05-29 13:18:21 +03:00
|
|
|
const useTimeoutToggle = jest.fn(() => [false, copyToClipboard]) as TimeoutToggle;
|
|
|
|
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
|
|
|
|
2020-08-30 10:59:14 +03:00
|
|
|
afterEach(jest.clearAllMocks);
|
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-01-18 00:53:49 +03:00
|
|
|
{ result: Mock.of<ShortUrl>({ 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-01-18 00:53:49 +03:00
|
|
|
{ result: Mock.of<ShortUrl>({ 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
|
|
|
});
|
|
|
|
});
|