2022-07-05 21:30:23 +03:00
|
|
|
import { render, screen } from '@testing-library/react';
|
|
|
|
import userEvent from '@testing-library/user-event';
|
|
|
|
import { UserEvent } from '@testing-library/user-event/dist/types/setup';
|
2021-06-25 20:52:50 +03:00
|
|
|
import { formatISO } from 'date-fns';
|
2021-03-19 21:11:27 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2021-03-27 20:39:55 +03:00
|
|
|
import { ShortUrlForm as createShortUrlForm, Mode } from '../../src/short-urls/ShortUrlForm';
|
|
|
|
import { ReachableServer, SelectedServer } from '../../src/servers/data';
|
2021-06-25 20:52:50 +03:00
|
|
|
import { parseDate } from '../../src/utils/helpers/date';
|
2021-10-17 13:35:11 +03:00
|
|
|
import { OptionalString } from '../../src/utils/utils';
|
2021-03-19 21:11:27 +03:00
|
|
|
|
|
|
|
describe('<ShortUrlForm />', () => {
|
2021-06-22 22:01:20 +03:00
|
|
|
const createShortUrl = jest.fn(async () => Promise.resolve());
|
2022-07-05 21:30:23 +03:00
|
|
|
const ShortUrlForm = createShortUrlForm(() => <span>TagsSelector</span>, () => <span>DomainSelector</span>);
|
|
|
|
const setUp = (selectedServer: SelectedServer = null, mode: Mode = 'create', title?: OptionalString) => ({
|
|
|
|
user: userEvent.setup(),
|
|
|
|
...render(
|
2021-03-19 21:11:27 +03:00
|
|
|
<ShortUrlForm
|
2021-03-27 20:39:55 +03:00
|
|
|
selectedServer={selectedServer}
|
|
|
|
mode={mode}
|
2021-03-19 21:11:27 +03:00
|
|
|
saving={false}
|
2022-07-05 21:30:23 +03:00
|
|
|
initialState={{ validateUrl: true, findIfExists: false, title, longUrl: '' }}
|
2021-03-19 21:11:27 +03:00
|
|
|
onSave={createShortUrl}
|
|
|
|
/>,
|
2022-07-05 21:30:23 +03:00
|
|
|
),
|
|
|
|
});
|
2021-03-27 20:39:55 +03:00
|
|
|
|
2021-03-19 21:11:27 +03:00
|
|
|
afterEach(jest.clearAllMocks);
|
|
|
|
|
2022-07-05 21:30:23 +03:00
|
|
|
it.each([
|
|
|
|
[
|
|
|
|
async (user: UserEvent) => {
|
|
|
|
await user.type(screen.getByPlaceholderText('Custom slug'), 'my-slug');
|
|
|
|
},
|
|
|
|
{ customSlug: 'my-slug' },
|
|
|
|
],
|
|
|
|
[
|
|
|
|
async (user: UserEvent) => {
|
|
|
|
await user.type(screen.getByPlaceholderText('Short code length'), '15');
|
|
|
|
},
|
|
|
|
{ shortCodeLength: '15' },
|
|
|
|
],
|
|
|
|
])('saves short URL with data set in form controls', async (extraFields, extraExpectedValues) => {
|
|
|
|
const { user } = setUp();
|
2021-06-25 20:52:50 +03:00
|
|
|
const validSince = parseDate('2017-01-01', 'yyyy-MM-dd');
|
|
|
|
const validUntil = parseDate('2017-01-06', 'yyyy-MM-dd');
|
2021-03-19 21:11:27 +03:00
|
|
|
|
2022-07-05 21:30:23 +03:00
|
|
|
await user.type(screen.getByPlaceholderText('URL to be shortened'), 'https://long-domain.com/foo/bar');
|
|
|
|
await user.type(screen.getByPlaceholderText('Title'), 'the title');
|
|
|
|
await user.type(screen.getByPlaceholderText('Maximum number of visits allowed'), '20');
|
|
|
|
await user.type(screen.getByPlaceholderText('Enabled since...'), '2017-01-01');
|
|
|
|
await user.type(screen.getByPlaceholderText('Enabled until...'), '2017-01-06');
|
|
|
|
await extraFields(user);
|
2021-03-19 21:11:27 +03:00
|
|
|
|
2022-07-05 21:30:23 +03:00
|
|
|
expect(createShortUrl).not.toHaveBeenCalled();
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Save' }));
|
2021-03-19 21:11:27 +03:00
|
|
|
expect(createShortUrl).toHaveBeenCalledWith({
|
|
|
|
longUrl: 'https://long-domain.com/foo/bar',
|
2022-07-05 21:30:23 +03:00
|
|
|
title: 'the title',
|
2021-06-24 21:13:06 +03:00
|
|
|
validSince: formatISO(validSince),
|
|
|
|
validUntil: formatISO(validUntil),
|
2021-03-27 11:49:47 +03:00
|
|
|
maxVisits: 20,
|
2021-03-19 21:11:27 +03:00
|
|
|
findIfExists: false,
|
|
|
|
validateUrl: true,
|
2022-07-05 21:30:23 +03:00
|
|
|
...extraExpectedValues,
|
2021-03-19 21:11:27 +03:00
|
|
|
});
|
|
|
|
});
|
2021-03-27 20:39:55 +03:00
|
|
|
|
|
|
|
it.each([
|
2022-05-01 12:00:46 +03:00
|
|
|
['create' as Mode, 4],
|
|
|
|
['create-basic' as Mode, 0],
|
2021-03-27 20:39:55 +03:00
|
|
|
])(
|
|
|
|
'renders expected amount of cards based on server capabilities and mode',
|
2022-05-01 12:00:46 +03:00
|
|
|
(mode, expectedAmountOfCards) => {
|
2022-07-05 21:30:23 +03:00
|
|
|
setUp(null, mode);
|
|
|
|
const cards = screen.queryAllByRole('heading');
|
2021-03-27 20:39:55 +03:00
|
|
|
|
|
|
|
expect(cards).toHaveLength(expectedAmountOfCards);
|
|
|
|
},
|
|
|
|
);
|
2021-10-17 13:35:11 +03:00
|
|
|
|
|
|
|
it.each([
|
2022-07-05 21:30:23 +03:00
|
|
|
[null, true, 'new title'],
|
|
|
|
[undefined, true, 'new title'],
|
|
|
|
['', true, 'new title'],
|
|
|
|
[null, false, undefined],
|
|
|
|
['', false, undefined],
|
|
|
|
['old title', false, null],
|
|
|
|
])('sends expected title based on original and new values', async (originalTitle, withNewTitle, expectedSentTitle) => {
|
|
|
|
const { user } = setUp(Mock.of<ReachableServer>({ version: '2.6.0' }), 'create', originalTitle);
|
2021-10-17 13:35:11 +03:00
|
|
|
|
2022-07-05 21:30:23 +03:00
|
|
|
await user.type(screen.getByPlaceholderText('URL to be shortened'), 'https://long-domain.com/foo/bar');
|
|
|
|
if (withNewTitle) {
|
|
|
|
await user.type(screen.getByPlaceholderText('Title'), 'new title');
|
|
|
|
} else {
|
|
|
|
await user.clear(screen.getByPlaceholderText('Title'));
|
|
|
|
}
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Save' }));
|
2021-10-17 13:35:11 +03:00
|
|
|
|
|
|
|
expect(createShortUrl).toHaveBeenCalledWith(expect.objectContaining({
|
|
|
|
title: expectedSentTitle,
|
|
|
|
}));
|
|
|
|
});
|
2021-03-19 21:11:27 +03:00
|
|
|
});
|