2022-07-06 19:30:33 +03:00
|
|
|
import { render, screen } from '@testing-library/react';
|
|
|
|
import userEvent from '@testing-library/user-event';
|
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';
|
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-07-06 19:30:33 +03:00
|
|
|
const setUp = (result: ShortUrl | null = null, error = false) => ({
|
|
|
|
user: userEvent.setup(),
|
|
|
|
...render(<CreateShortUrlResult resetCreateShortUrl={() => {}} result={result} error={error} saving={false} />),
|
|
|
|
});
|
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-07-06 19:30:33 +03:00
|
|
|
setUp(Mock.all<ShortUrl>(), true);
|
|
|
|
expect(screen.getByText('An error occurred while creating the URL :(')).toBeInTheDocument();
|
2018-11-01 11:30:05 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('renders nothing when no result is provided', () => {
|
2022-07-06 19:30:33 +03:00
|
|
|
const { container } = setUp();
|
|
|
|
expect(container.firstChild).toBeNull();
|
2018-11-01 11:30:05 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('renders a result message when result is provided', () => {
|
2022-07-06 19:30:33 +03:00
|
|
|
setUp(Mock.of<ShortUrl>({ shortUrl: 'https://doma.in/abc123' }));
|
|
|
|
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 () => {
|
|
|
|
const { user } = setUp(Mock.of<ShortUrl>({ shortUrl: 'https://doma.in/abc123' }));
|
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
|
|
|
});
|
|
|
|
});
|