2022-06-12 21:41:40 +03:00
|
|
|
import { render, screen } from '@testing-library/react';
|
|
|
|
import { MemoryRouter } from 'react-router-dom';
|
2021-03-27 19:56:46 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
|
|
|
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 { ShortUrl } from '../../src/short-urls/data';
|
|
|
|
|
|
|
|
describe('<EditShortUrl />', () => {
|
|
|
|
const shortUrlCreation = { validateUrls: true };
|
2022-06-12 21:41:40 +03:00
|
|
|
const EditShortUrl = createEditShortUrl(() => <span>ShortUrlForm</span>);
|
|
|
|
const setUp = (detail: Partial<ShortUrlDetail> = {}, edition: Partial<ShortUrlEdition> = {}) => render(
|
|
|
|
<MemoryRouter>
|
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)}
|
2022-06-12 21:41:40 +03:00
|
|
|
getShortUrlDetail={jest.fn()}
|
|
|
|
editShortUrl={jest.fn(async () => Promise.resolve())}
|
|
|
|
/>
|
|
|
|
</MemoryRouter>,
|
|
|
|
);
|
2021-03-27 19:56:46 +03:00
|
|
|
|
|
|
|
it('renders loading message while loading detail', () => {
|
2022-06-12 21:41:40 +03:00
|
|
|
setUp({ loading: true });
|
2021-03-27 19:56:46 +03:00
|
|
|
|
2022-06-12 21:41:40 +03:00
|
|
|
expect(screen.getByText('Loading...')).toBeInTheDocument();
|
|
|
|
expect(screen.queryByText('ShortUrlForm')).not.toBeInTheDocument();
|
2021-03-27 19:56:46 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('renders error when loading detail fails', () => {
|
2022-06-12 21:41:40 +03:00
|
|
|
setUp({ error: true });
|
2021-03-27 19:56:46 +03:00
|
|
|
|
2022-06-12 21:41:40 +03:00
|
|
|
expect(screen.getByText('An error occurred while loading short URL detail :(')).toBeInTheDocument();
|
|
|
|
expect(screen.queryByText('ShortUrlForm')).not.toBeInTheDocument();
|
2021-03-27 19:56:46 +03:00
|
|
|
});
|
|
|
|
|
2022-06-12 21:41:40 +03:00
|
|
|
it('renders form when detail properly loads', () => {
|
|
|
|
setUp({ shortUrl: Mock.of<ShortUrl>({ meta: {} }) });
|
2021-03-27 19:56:46 +03:00
|
|
|
|
2022-06-12 21:41:40 +03:00
|
|
|
expect(screen.getByText('ShortUrlForm')).toBeInTheDocument();
|
|
|
|
expect(screen.queryByText('Loading...')).not.toBeInTheDocument();
|
|
|
|
expect(screen.queryByText('An error occurred while loading short URL detail :(')).not.toBeInTheDocument();
|
2021-03-27 19:56:46 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('shows error when saving data has failed', () => {
|
2022-11-06 14:32:55 +03:00
|
|
|
setUp({}, { error: true, saved: true });
|
2021-03-27 19:56:46 +03:00
|
|
|
|
2022-06-12 21:41:40 +03:00
|
|
|
expect(screen.getByText('An error occurred while updating short URL :(')).toBeInTheDocument();
|
|
|
|
expect(screen.getByText('ShortUrlForm')).toBeInTheDocument();
|
2021-03-27 19:56:46 +03:00
|
|
|
});
|
2022-11-06 14:32:55 +03:00
|
|
|
|
|
|
|
it('shows message when saving data succeeds', () => {
|
|
|
|
setUp({}, { error: false, saved: true });
|
|
|
|
|
|
|
|
expect(screen.getByText('Short URL properly edited.')).toBeInTheDocument();
|
|
|
|
expect(screen.getByText('ShortUrlForm')).toBeInTheDocument();
|
|
|
|
});
|
2021-03-27 19:56:46 +03:00
|
|
|
});
|