mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-11 10:03:51 +00:00
Implemented state machine for short URL creation
This commit is contained in:
@@ -4,34 +4,39 @@ import { CreateShortUrlResult as createResult } from '../../../src/short-urls/he
|
||||
import { ShortUrl } from '../../../src/short-urls/data';
|
||||
import { TimeoutToggle } from '../../../src/utils/helpers/hooks';
|
||||
import { renderWithEvents } from '../../__helpers__/setUpTest';
|
||||
import { ShortUrlCreation } from '../../../src/short-urls/reducers/shortUrlCreation';
|
||||
|
||||
describe('<CreateShortUrlResult />', () => {
|
||||
const copyToClipboard = jest.fn();
|
||||
const useTimeoutToggle = jest.fn(() => [false, copyToClipboard]) as TimeoutToggle;
|
||||
const CreateShortUrlResult = createResult(useTimeoutToggle);
|
||||
const setUp = (result?: ShortUrl, error = false) => renderWithEvents(
|
||||
<CreateShortUrlResult resetCreateShortUrl={() => {}} creation={{ result, saving: false, error, saved: false }} />,
|
||||
const setUp = (creation: ShortUrlCreation) => renderWithEvents(
|
||||
<CreateShortUrlResult resetCreateShortUrl={() => {}} creation={creation} />,
|
||||
);
|
||||
|
||||
afterEach(jest.clearAllMocks);
|
||||
|
||||
it('renders an error when error is true', () => {
|
||||
setUp(Mock.all<ShortUrl>(), true);
|
||||
setUp({ error: true, saved: false, saving: false });
|
||||
expect(screen.getByText('An error occurred while creating the URL :(')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders nothing when no result is provided', () => {
|
||||
const { container } = setUp();
|
||||
it.each([[true], [false]])('renders nothing when not saved yet', (saving) => {
|
||||
const { container } = setUp({ error: false, saved: false, saving });
|
||||
expect(container.firstChild).toBeNull();
|
||||
});
|
||||
|
||||
it('renders a result message when result is provided', () => {
|
||||
setUp(Mock.of<ShortUrl>({ shortUrl: 'https://doma.in/abc123' }));
|
||||
setUp(
|
||||
{ result: Mock.of<ShortUrl>({ shortUrl: 'https://doma.in/abc123' }), saving: false, saved: true, error: false },
|
||||
);
|
||||
expect(screen.getByText(/The short URL is/)).toHaveTextContent('Great! The short URL is https://doma.in/abc123');
|
||||
});
|
||||
|
||||
it('Invokes tooltip timeout when copy to clipboard button is clicked', async () => {
|
||||
const { user } = setUp(Mock.of<ShortUrl>({ shortUrl: 'https://doma.in/abc123' }));
|
||||
const { user } = setUp(
|
||||
{ result: Mock.of<ShortUrl>({ shortUrl: 'https://doma.in/abc123' }), saving: false, saved: true, error: false },
|
||||
);
|
||||
|
||||
expect(copyToClipboard).not.toHaveBeenCalled();
|
||||
await user.click(screen.getByRole('button'));
|
||||
|
||||
Reference in New Issue
Block a user