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';
|
2022-05-28 12:16:59 +03:00
|
|
|
import { CreateShortUrlResult as createResult } from '../../../src/short-urls/helpers/CreateShortUrlResult';
|
2020-08-30 10:59:14 +03:00
|
|
|
import { ShortUrl } from '../../../src/short-urls/data';
|
2022-05-29 13:18:21 +03:00
|
|
|
import { TimeoutToggle } from '../../../src/utils/helpers/hooks';
|
2022-07-10 20:44:49 +03:00
|
|
|
import { renderWithEvents } from '../../__helpers__/setUpTest';
|
2022-11-07 20:55:12 +03:00
|
|
|
import { ShortUrlCreation } from '../../../src/short-urls/reducers/shortUrlCreation';
|
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(
|
|
|
|
{ result: Mock.of<ShortUrl>({ shortUrl: 'https://doma.in/abc123' }), saving: false, saved: true, error: false },
|
|
|
|
);
|
2022-07-06 19:30:33 +03:00
|
|
|
expect(screen.getByText(/The short URL is/)).toHaveTextContent('Great! The short URL is https://doma.in/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(
|
|
|
|
{ result: Mock.of<ShortUrl>({ shortUrl: 'https://doma.in/abc123' }), saving: false, saved: true, error: false },
|
|
|
|
);
|
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
|
|
|
});
|
|
|
|
});
|