2022-07-15 23:00:30 +03:00
|
|
|
import { screen } from '@testing-library/react';
|
2020-08-26 21:37:36 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2022-05-28 12:16:59 +03:00
|
|
|
import { DeleteShortUrlModal } from '../../../src/short-urls/helpers/DeleteShortUrlModal';
|
2020-08-26 21:37:36 +03:00
|
|
|
import { ShortUrl } from '../../../src/short-urls/data';
|
|
|
|
import { ShortUrlDeletion } from '../../../src/short-urls/reducers/shortUrlDeletion';
|
2020-12-22 11:49:13 +03:00
|
|
|
import { ProblemDetailsError } from '../../../src/api/types';
|
2022-07-15 23:00:30 +03:00
|
|
|
import { renderWithEvents } from '../../__helpers__/setUpTest';
|
2018-11-01 14:24:16 +03:00
|
|
|
|
|
|
|
describe('<DeleteShortUrlModal />', () => {
|
2020-08-26 21:37:36 +03:00
|
|
|
const shortUrl = Mock.of<ShortUrl>({
|
2018-11-01 14:24:16 +03:00
|
|
|
tags: [],
|
|
|
|
shortCode: 'abc123',
|
2020-08-26 21:37:36 +03:00
|
|
|
longUrl: 'https://long-domain.com/foo/bar',
|
|
|
|
});
|
|
|
|
const deleteShortUrl = jest.fn(async () => Promise.resolve());
|
2022-07-15 23:00:30 +03:00
|
|
|
const setUp = (shortUrlDeletion: Partial<ShortUrlDeletion>) => renderWithEvents(
|
|
|
|
<DeleteShortUrlModal
|
|
|
|
isOpen
|
|
|
|
shortUrl={shortUrl}
|
|
|
|
shortUrlDeletion={Mock.of<ShortUrlDeletion>(shortUrlDeletion)}
|
|
|
|
deleteShortUrl={deleteShortUrl}
|
|
|
|
toggle={() => {}}
|
|
|
|
resetDeleteShortUrl={() => {}}
|
|
|
|
/>,
|
|
|
|
);
|
2018-11-01 14:24:16 +03:00
|
|
|
|
2020-08-26 21:37:36 +03:00
|
|
|
afterEach(jest.clearAllMocks);
|
2018-11-01 14:24:16 +03:00
|
|
|
|
|
|
|
it('shows generic error when non-threshold error occurs', () => {
|
2022-07-15 23:00:30 +03:00
|
|
|
setUp({
|
2018-11-01 14:24:16 +03:00
|
|
|
loading: false,
|
|
|
|
error: true,
|
|
|
|
shortCode: 'abc123',
|
2020-12-12 15:33:21 +03:00
|
|
|
errorData: Mock.of<ProblemDetailsError>({ type: 'OTHER_ERROR' }),
|
2018-11-01 14:24:16 +03:00
|
|
|
});
|
2022-07-15 23:00:30 +03:00
|
|
|
expect(screen.getByText('Something went wrong while deleting the URL :(')).toBeInTheDocument();
|
2018-11-01 14:24:16 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('disables submit button when loading', () => {
|
2022-07-15 23:00:30 +03:00
|
|
|
setUp({
|
2018-11-01 14:24:16 +03:00
|
|
|
loading: true,
|
|
|
|
error: false,
|
|
|
|
shortCode: 'abc123',
|
|
|
|
});
|
2022-07-15 23:00:30 +03:00
|
|
|
expect(screen.getByRole('button', { name: 'Deleting...' })).toHaveAttribute('disabled');
|
2018-11-01 14:24:16 +03:00
|
|
|
});
|
|
|
|
|
2022-07-15 23:00:30 +03:00
|
|
|
it('enables submit button when proper short code is provided', async () => {
|
2018-11-01 14:24:16 +03:00
|
|
|
const shortCode = 'abc123';
|
2022-07-15 23:00:30 +03:00
|
|
|
const { user } = setUp({
|
2018-11-01 14:24:16 +03:00
|
|
|
loading: false,
|
|
|
|
error: false,
|
|
|
|
shortCode,
|
|
|
|
});
|
2022-07-15 23:00:30 +03:00
|
|
|
const getDeleteBtn = () => screen.getByRole('button', { name: 'Delete' });
|
2018-11-01 14:24:16 +03:00
|
|
|
|
2022-07-15 23:00:30 +03:00
|
|
|
expect(getDeleteBtn()).toHaveAttribute('disabled');
|
|
|
|
await user.type(screen.getByPlaceholderText(/^Insert the short code/), shortCode);
|
|
|
|
expect(getDeleteBtn()).not.toHaveAttribute('disabled');
|
2018-11-01 14:24:16 +03:00
|
|
|
});
|
|
|
|
|
2022-07-15 23:00:30 +03:00
|
|
|
it('tries to delete short URL when form is submit', async () => {
|
2018-11-01 14:24:16 +03:00
|
|
|
const shortCode = 'abc123';
|
2022-07-15 23:00:30 +03:00
|
|
|
const { user } = setUp({
|
2018-11-01 14:24:16 +03:00
|
|
|
loading: false,
|
|
|
|
error: false,
|
|
|
|
shortCode,
|
|
|
|
});
|
|
|
|
|
2021-10-31 14:33:17 +03:00
|
|
|
expect(deleteShortUrl).not.toHaveBeenCalled();
|
2022-07-15 23:00:30 +03:00
|
|
|
await user.type(screen.getByPlaceholderText(/^Insert the short code/), shortCode);
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Delete' }));
|
2021-10-31 14:33:17 +03:00
|
|
|
expect(deleteShortUrl).toHaveBeenCalledTimes(1);
|
2018-11-01 14:24:16 +03:00
|
|
|
});
|
|
|
|
});
|