shlink-web-client/test/short-urls/helpers/CreateShortUrlResult.test.tsx

42 lines
1.8 KiB
TypeScript
Raw Normal View History

import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Mock } from 'ts-mockery';
import { CreateShortUrlResult as createResult } from '../../../src/short-urls/helpers/CreateShortUrlResult';
import { ShortUrl } from '../../../src/short-urls/data';
import { TimeoutToggle } from '../../../src/utils/helpers/hooks';
2018-11-01 11:30:05 +03:00
describe('<CreateShortUrlResult />', () => {
const copyToClipboard = jest.fn();
const useTimeoutToggle = jest.fn(() => [false, copyToClipboard]) as TimeoutToggle;
const CreateShortUrlResult = createResult(useTimeoutToggle);
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
afterEach(jest.clearAllMocks);
2018-11-01 11:30:05 +03:00
it('renders an error when error is true', () => {
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', () => {
const { container } = setUp();
expect(container.firstChild).toBeNull();
2018-11-01 11:30:05 +03:00
});
it('renders a result message when result is provided', () => {
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
});
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
expect(copyToClipboard).not.toHaveBeenCalled();
await user.click(screen.getByRole('button'));
expect(copyToClipboard).toHaveBeenCalledTimes(1);
2018-11-01 11:30:05 +03:00
});
});