2021-03-27 19:56:46 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
|
|
|
import { Mock } from 'ts-mockery';
|
2022-02-08 00:17:57 +03:00
|
|
|
import { useLocation, useParams } from 'react-router-dom';
|
2021-03-27 19:56:46 +03:00
|
|
|
import { EditShortUrl as createEditShortUrl } from '../../src/short-urls/EditShortUrl';
|
|
|
|
import { Settings } from '../../src/settings/reducers/settings';
|
|
|
|
import { ShortUrlDetail } from '../../src/short-urls/reducers/shortUrlDetail';
|
|
|
|
import { ShortUrlEdition } from '../../src/short-urls/reducers/shortUrlEdition';
|
|
|
|
import { ShlinkApiError } from '../../src/api/ShlinkApiError';
|
|
|
|
import { ShortUrl } from '../../src/short-urls/data';
|
|
|
|
|
2022-02-08 00:17:57 +03:00
|
|
|
jest.mock('react-router-dom', () => ({
|
|
|
|
...jest.requireActual('react-router-dom'),
|
|
|
|
useNavigate: jest.fn().mockReturnValue(jest.fn()),
|
|
|
|
useParams: jest.fn().mockReturnValue({}),
|
|
|
|
useLocation: jest.fn().mockReturnValue({}),
|
|
|
|
}));
|
|
|
|
|
2021-03-27 19:56:46 +03:00
|
|
|
describe('<EditShortUrl />', () => {
|
|
|
|
let wrapper: ShallowWrapper;
|
|
|
|
const ShortUrlForm = () => null;
|
|
|
|
const getShortUrlDetail = jest.fn();
|
2021-06-22 22:01:20 +03:00
|
|
|
const editShortUrl = jest.fn(async () => Promise.resolve());
|
2021-03-27 19:56:46 +03:00
|
|
|
const shortUrlCreation = { validateUrls: true };
|
2022-02-08 00:17:57 +03:00
|
|
|
const EditShortUrl = createEditShortUrl(ShortUrlForm);
|
2021-03-27 19:56:46 +03:00
|
|
|
const createWrapper = (detail: Partial<ShortUrlDetail> = {}, edition: Partial<ShortUrlEdition> = {}) => {
|
2022-02-08 00:17:57 +03:00
|
|
|
(useParams as any).mockReturnValue({ shortCode: 'the_base_url' });
|
|
|
|
(useLocation as any).mockReturnValue({ search: '' });
|
2021-03-27 19:56:46 +03:00
|
|
|
|
|
|
|
wrapper = shallow(
|
2022-02-08 00:17:57 +03:00
|
|
|
<EditShortUrl
|
2021-03-27 19:56:46 +03:00
|
|
|
settings={Mock.of<Settings>({ shortUrlCreation })}
|
|
|
|
selectedServer={null}
|
|
|
|
shortUrlDetail={Mock.of<ShortUrlDetail>(detail)}
|
|
|
|
shortUrlEdition={Mock.of<ShortUrlEdition>(edition)}
|
|
|
|
getShortUrlDetail={getShortUrlDetail}
|
|
|
|
editShortUrl={editShortUrl}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(jest.clearAllMocks);
|
|
|
|
afterEach(() => wrapper?.unmount());
|
|
|
|
|
|
|
|
it('renders loading message while loading detail', () => {
|
|
|
|
const wrapper = createWrapper({ loading: true });
|
|
|
|
|
|
|
|
expect(wrapper.prop('loading')).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders error when loading detail fails', () => {
|
|
|
|
const wrapper = createWrapper({ error: true });
|
|
|
|
const form = wrapper.find(ShortUrlForm);
|
|
|
|
const apiError = wrapper.find(ShlinkApiError);
|
|
|
|
|
|
|
|
expect(form).toHaveLength(0);
|
|
|
|
expect(apiError).toHaveLength(1);
|
|
|
|
expect(apiError.prop('fallbackMessage')).toEqual('An error occurred while loading short URL detail :(');
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[undefined, { longUrl: '', validateUrl: true }, true],
|
2021-03-27 19:56:46 +03:00
|
|
|
[
|
|
|
|
Mock.of<ShortUrl>({ meta: {} }),
|
|
|
|
{
|
|
|
|
longUrl: undefined,
|
|
|
|
tags: undefined,
|
|
|
|
title: undefined,
|
|
|
|
domain: undefined,
|
|
|
|
validSince: undefined,
|
|
|
|
validUntil: undefined,
|
|
|
|
maxVisits: undefined,
|
|
|
|
validateUrl: true,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
],
|
|
|
|
])('renders form when detail properly loads', (shortUrl, expectedInitialState, saving) => {
|
|
|
|
const wrapper = createWrapper({ shortUrl }, { saving });
|
|
|
|
const form = wrapper.find(ShortUrlForm);
|
|
|
|
const apiError = wrapper.find(ShlinkApiError);
|
|
|
|
|
|
|
|
expect(form).toHaveLength(1);
|
|
|
|
expect(apiError).toHaveLength(0);
|
|
|
|
expect(form.prop('initialState')).toEqual(expectedInitialState);
|
|
|
|
expect(form.prop('saving')).toEqual(saving);
|
|
|
|
expect(editShortUrl).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
form.simulate('save', {});
|
|
|
|
|
|
|
|
if (shortUrl) {
|
|
|
|
expect(editShortUrl).toHaveBeenCalledWith(shortUrl.shortCode, shortUrl.domain, {});
|
|
|
|
} else {
|
|
|
|
expect(editShortUrl).not.toHaveBeenCalled();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shows error when saving data has failed', () => {
|
|
|
|
const wrapper = createWrapper({}, { error: true });
|
|
|
|
const form = wrapper.find(ShortUrlForm);
|
|
|
|
const apiError = wrapper.find(ShlinkApiError);
|
|
|
|
|
|
|
|
expect(form).toHaveLength(1);
|
|
|
|
expect(apiError).toHaveLength(1);
|
|
|
|
expect(apiError.prop('fallbackMessage')).toEqual('An error occurred while updating short URL :(');
|
|
|
|
});
|
|
|
|
});
|