mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2024-12-23 09:30:31 +03:00
Normalized mocks
This commit is contained in:
parent
491b2f2c07
commit
4c7bed90a3
7 changed files with 41 additions and 22 deletions
|
@ -1,7 +1,7 @@
|
||||||
import { Mock } from 'ts-mockery';
|
import { Mock } from 'ts-mockery';
|
||||||
import { AxiosInstance } from 'axios';
|
import { AxiosInstance } from 'axios';
|
||||||
import { ImageDownloader } from '../../../src/common/services/ImageDownloader';
|
import { ImageDownloader } from '../../../src/common/services/ImageDownloader';
|
||||||
import { windowMock } from '../../mocks/WindowMock';
|
import { windowMock } from '../../__mocks__/Window.mock';
|
||||||
|
|
||||||
describe('ImageDownloader', () => {
|
describe('ImageDownloader', () => {
|
||||||
const get = jest.fn();
|
const get = jest.fn();
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { ReportExporter } from '../../../src/common/services/ReportExporter';
|
import { ReportExporter } from '../../../src/common/services/ReportExporter';
|
||||||
import { NormalizedVisit } from '../../../src/visits/types';
|
import { NormalizedVisit } from '../../../src/visits/types';
|
||||||
import { windowMock } from '../../mocks/WindowMock';
|
import { windowMock } from '../../__mocks__/Window.mock';
|
||||||
import { ExportableShortUrl } from '../../../src/short-urls/data';
|
import { ExportableShortUrl } from '../../../src/short-urls/data';
|
||||||
|
|
||||||
describe('ReportExporter', () => {
|
describe('ReportExporter', () => {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { Mock } from 'ts-mockery';
|
import { Mock } from 'ts-mockery';
|
||||||
import ServersExporter from '../../../src/servers/services/ServersExporter';
|
import ServersExporter from '../../../src/servers/services/ServersExporter';
|
||||||
import { LocalStorage } from '../../../src/utils/services/LocalStorage';
|
import { LocalStorage } from '../../../src/utils/services/LocalStorage';
|
||||||
import { appendChild, removeChild, windowMock } from '../../mocks/WindowMock';
|
import { appendChild, removeChild, windowMock } from '../../__mocks__/Window.mock';
|
||||||
|
|
||||||
describe('ServersExporter', () => {
|
describe('ServersExporter', () => {
|
||||||
const storageMock = Mock.of<LocalStorage>({
|
const storageMock = Mock.of<LocalStorage>({
|
||||||
|
|
|
@ -2,22 +2,17 @@ import { screen } from '@testing-library/react';
|
||||||
import { Mock } from 'ts-mockery';
|
import { Mock } from 'ts-mockery';
|
||||||
import { formatISO } from 'date-fns';
|
import { formatISO } from 'date-fns';
|
||||||
import { ShortUrlsRow as createShortUrlsRow } from '../../../src/short-urls/helpers/ShortUrlsRow';
|
import { ShortUrlsRow as createShortUrlsRow } from '../../../src/short-urls/helpers/ShortUrlsRow';
|
||||||
import { ColorGenerator } from '../../../src/utils/services/ColorGenerator';
|
|
||||||
import { TimeoutToggle } from '../../../src/utils/helpers/hooks';
|
import { TimeoutToggle } from '../../../src/utils/helpers/hooks';
|
||||||
import { ShortUrl } from '../../../src/short-urls/data';
|
import { ShortUrl } from '../../../src/short-urls/data';
|
||||||
import { ReachableServer } from '../../../src/servers/data';
|
import { ReachableServer } from '../../../src/servers/data';
|
||||||
import { parseDate } from '../../../src/utils/helpers/date';
|
import { parseDate } from '../../../src/utils/helpers/date';
|
||||||
import { renderWithEvents } from '../../__helpers__/setUpTest';
|
import { renderWithEvents } from '../../__helpers__/setUpTest';
|
||||||
import { OptionalString } from '../../../src/utils/utils';
|
import { OptionalString } from '../../../src/utils/utils';
|
||||||
|
import { colorGeneratorMock } from '../../utils/services/__mocks__/ColorGenerator.mock';
|
||||||
|
|
||||||
describe('<ShortUrlsRow />', () => {
|
describe('<ShortUrlsRow />', () => {
|
||||||
const timeoutToggle = jest.fn(() => true);
|
const timeoutToggle = jest.fn(() => true);
|
||||||
const useTimeoutToggle = jest.fn(() => [false, timeoutToggle]) as TimeoutToggle;
|
const useTimeoutToggle = jest.fn(() => [false, timeoutToggle]) as TimeoutToggle;
|
||||||
const colorGenerator = Mock.of<ColorGenerator>({
|
|
||||||
getColorForKey: jest.fn(() => 'red'),
|
|
||||||
setColorForKey: jest.fn(),
|
|
||||||
isColorLightForKey: jest.fn(() => false),
|
|
||||||
});
|
|
||||||
const server = Mock.of<ReachableServer>({ url: 'https://doma.in' });
|
const server = Mock.of<ReachableServer>({ url: 'https://doma.in' });
|
||||||
const shortUrl: ShortUrl = {
|
const shortUrl: ShortUrl = {
|
||||||
shortCode: 'abc123',
|
shortCode: 'abc123',
|
||||||
|
@ -33,7 +28,7 @@ describe('<ShortUrlsRow />', () => {
|
||||||
maxVisits: null,
|
maxVisits: null,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const ShortUrlsRow = createShortUrlsRow(() => <span>ShortUrlsRowMenu</span>, colorGenerator, useTimeoutToggle);
|
const ShortUrlsRow = createShortUrlsRow(() => <span>ShortUrlsRowMenu</span>, colorGeneratorMock, useTimeoutToggle);
|
||||||
const setUp = (title?: OptionalString, tags: string[] = []) => renderWithEvents(
|
const setUp = (title?: OptionalString, tags: string[] = []) => renderWithEvents(
|
||||||
<table>
|
<table>
|
||||||
<tbody>
|
<tbody>
|
||||||
|
|
|
@ -1,16 +1,31 @@
|
||||||
|
import { screen } from '@testing-library/react';
|
||||||
import { shallow, ShallowWrapper } from 'enzyme';
|
import { shallow, ShallowWrapper } from 'enzyme';
|
||||||
import { Mock } from 'ts-mockery';
|
import { Mock } from 'ts-mockery';
|
||||||
import { Link } from 'react-router-dom';
|
|
||||||
import { DropdownItem } from 'reactstrap';
|
import { DropdownItem } from 'reactstrap';
|
||||||
|
import { MemoryRouter } from 'react-router-dom';
|
||||||
import { TagsTableRow as createTagsTableRow } from '../../src/tags/TagsTableRow';
|
import { TagsTableRow as createTagsTableRow } from '../../src/tags/TagsTableRow';
|
||||||
import { ReachableServer } from '../../src/servers/data';
|
import { ReachableServer } from '../../src/servers/data';
|
||||||
import { ColorGenerator } from '../../src/utils/services/ColorGenerator';
|
|
||||||
import { DropdownBtnMenu } from '../../src/utils/DropdownBtnMenu';
|
import { DropdownBtnMenu } from '../../src/utils/DropdownBtnMenu';
|
||||||
|
import { renderWithEvents } from '../__helpers__/setUpTest';
|
||||||
|
import { colorGeneratorMock } from '../utils/services/__mocks__/ColorGenerator.mock';
|
||||||
|
|
||||||
describe('<TagsTableRow />', () => {
|
describe('<TagsTableRow />', () => {
|
||||||
const DeleteTagConfirmModal = () => null;
|
const DeleteTagConfirmModal = () => null;
|
||||||
const EditTagModal = () => null;
|
const EditTagModal = () => null;
|
||||||
const TagsTableRow = createTagsTableRow(DeleteTagConfirmModal, EditTagModal, Mock.all<ColorGenerator>());
|
const TagsTableRow = createTagsTableRow(DeleteTagConfirmModal, EditTagModal, colorGeneratorMock);
|
||||||
|
const setUp = (tagStats?: { visits?: number; shortUrls?: number }) => renderWithEvents(
|
||||||
|
<MemoryRouter>
|
||||||
|
<table>
|
||||||
|
<tbody>
|
||||||
|
<TagsTableRow
|
||||||
|
tag={{ tag: 'foo&bar', visits: tagStats?.visits ?? 0, shortUrls: tagStats?.shortUrls ?? 0 }}
|
||||||
|
selectedServer={Mock.of<ReachableServer>({ id: 'abc123' })}
|
||||||
|
/>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</MemoryRouter>,
|
||||||
|
);
|
||||||
|
|
||||||
let wrapper: ShallowWrapper;
|
let wrapper: ShallowWrapper;
|
||||||
const createWrapper = (tagStats?: { visits?: number; shortUrls?: number }) => {
|
const createWrapper = (tagStats?: { visits?: number; shortUrls?: number }) => {
|
||||||
wrapper = shallow(
|
wrapper = shallow(
|
||||||
|
@ -22,22 +37,23 @@ describe('<TagsTableRow />', () => {
|
||||||
|
|
||||||
return wrapper;
|
return wrapper;
|
||||||
};
|
};
|
||||||
|
|
||||||
afterEach(() => wrapper?.unmount());
|
afterEach(() => wrapper?.unmount());
|
||||||
|
|
||||||
it.each([
|
it.each([
|
||||||
[undefined, '0', '0'],
|
[undefined, '0', '0'],
|
||||||
[{ shortUrls: 10, visits: 3480 }, '10', '3,480'],
|
[{ shortUrls: 10, visits: 3480 }, '10', '3,480'],
|
||||||
])('shows expected tag stats', (stats, expectedShortUrls, expectedVisits) => {
|
])('shows expected tag stats', (stats, expectedShortUrls, expectedVisits) => {
|
||||||
const wrapper = createWrapper(stats);
|
setUp(stats);
|
||||||
const links = wrapper.find(Link);
|
|
||||||
const shortUrlsLink = links.first();
|
|
||||||
const visitsLink = links.last();
|
|
||||||
|
|
||||||
expect(shortUrlsLink.prop('children')).toEqual(expectedShortUrls);
|
const [shortUrlsLink, visitsLink] = screen.getAllByRole('link');
|
||||||
expect(shortUrlsLink.prop('to')).toEqual(`/server/abc123/list-short-urls/1?tags=${encodeURIComponent('foo&bar')}`);
|
|
||||||
expect(visitsLink.prop('children')).toEqual(expectedVisits);
|
expect(shortUrlsLink).toHaveTextContent(expectedShortUrls);
|
||||||
expect(visitsLink.prop('to')).toEqual('/server/abc123/tag/foo&bar/visits');
|
expect(shortUrlsLink).toHaveAttribute(
|
||||||
|
'href',
|
||||||
|
`/server/abc123/list-short-urls/1?tags=${encodeURIComponent('foo&bar')}`,
|
||||||
|
);
|
||||||
|
expect(visitsLink).toHaveTextContent(expectedVisits);
|
||||||
|
expect(visitsLink).toHaveAttribute('href', '/server/abc123/tag/foo&bar/visits');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('allows toggling dropdown menu', () => {
|
it('allows toggling dropdown menu', () => {
|
||||||
|
|
8
test/utils/services/__mocks__/ColorGenerator.mock.ts
Normal file
8
test/utils/services/__mocks__/ColorGenerator.mock.ts
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
import { Mock } from 'ts-mockery';
|
||||||
|
import { ColorGenerator } from '../../../../src/utils/services/ColorGenerator';
|
||||||
|
|
||||||
|
export const colorGeneratorMock = Mock.of<ColorGenerator>({
|
||||||
|
getColorForKey: jest.fn(() => 'red'),
|
||||||
|
setColorForKey: jest.fn(),
|
||||||
|
isColorLightForKey: jest.fn(() => false),
|
||||||
|
});
|
Loading…
Reference in a new issue