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

46 lines
2 KiB
TypeScript
Raw Normal View History

import { screen } from '@testing-library/react';
import { Mock } from 'ts-mockery';
import { CreateShortUrlResult as createResult } from '../../../src/short-urls/helpers/CreateShortUrlResult';
2023-02-18 12:40:37 +03:00
import type { ShortUrl } from '../../../src/short-urls/data';
import type { TimeoutToggle } from '../../../src/utils/helpers/hooks';
2022-07-10 20:44:49 +03:00
import { renderWithEvents } from '../../__helpers__/setUpTest';
2023-02-18 12:40:37 +03:00
import type { ShortUrlCreation } from '../../../src/short-urls/reducers/shortUrlCreation';
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 = (creation: ShortUrlCreation) => renderWithEvents(
<CreateShortUrlResult resetCreateShortUrl={() => {}} creation={creation} />,
);
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({ error: true, saved: false, saving: false });
expect(screen.getByText('An error occurred while creating the URL :(')).toBeInTheDocument();
2018-11-01 11:30:05 +03:00
});
it.each([[true], [false]])('renders nothing when not saved yet', (saving) => {
const { container } = setUp({ error: false, saved: false, saving });
expect(container.firstChild).toBeNull();
2018-11-01 11:30:05 +03:00
});
it('renders a result message when result is provided', () => {
setUp(
{ result: Mock.of<ShortUrl>({ shortUrl: 'https://s.test/abc123' }), saving: false, saved: true, error: false },
);
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
});
it('Invokes tooltip timeout when copy to clipboard button is clicked', async () => {
const { user } = setUp(
{ result: Mock.of<ShortUrl>({ shortUrl: 'https://s.test/abc123' }), saving: false, saved: true, error: false },
);
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
});
});