mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2024-12-23 17:40:23 +03:00
Migrated more tests to react testing lib
This commit is contained in:
parent
8bf1a9d023
commit
54fe849efd
3 changed files with 114 additions and 93 deletions
|
@ -1,46 +1,50 @@
|
||||||
import { shallow } from 'enzyme';
|
import { render, screen } from '@testing-library/react';
|
||||||
|
import userEvent from '@testing-library/user-event';
|
||||||
|
import { MemoryRouter } from 'react-router-dom';
|
||||||
import { Mock } from 'ts-mockery';
|
import { Mock } from 'ts-mockery';
|
||||||
|
import { formatISO } from 'date-fns';
|
||||||
import { OrphanVisits as createOrphanVisits } from '../../src/visits/OrphanVisits';
|
import { OrphanVisits as createOrphanVisits } from '../../src/visits/OrphanVisits';
|
||||||
import { MercureBoundProps } from '../../src/mercure/helpers/boundToMercureHub';
|
import { MercureBoundProps } from '../../src/mercure/helpers/boundToMercureHub';
|
||||||
import { VisitsInfo } from '../../src/visits/types';
|
import { Visit, VisitsInfo } from '../../src/visits/types';
|
||||||
import { VisitsStats } from '../../src/visits/VisitsStats';
|
|
||||||
import { Settings } from '../../src/settings/reducers/settings';
|
import { Settings } from '../../src/settings/reducers/settings';
|
||||||
import { ReportExporter } from '../../src/common/services/ReportExporter';
|
import { ReportExporter } from '../../src/common/services/ReportExporter';
|
||||||
import { SelectedServer } from '../../src/servers/data';
|
import { SelectedServer } from '../../src/servers/data';
|
||||||
import { VisitsHeader } from '../../src/visits/VisitsHeader';
|
|
||||||
|
|
||||||
jest.mock('react-router-dom', () => ({
|
|
||||||
...jest.requireActual('react-router-dom'),
|
|
||||||
useNavigate: jest.fn().mockReturnValue(jest.fn()),
|
|
||||||
useParams: jest.fn().mockReturnValue({}),
|
|
||||||
}));
|
|
||||||
|
|
||||||
describe('<OrphanVisits />', () => {
|
describe('<OrphanVisits />', () => {
|
||||||
it('wraps visits stats and header', () => {
|
|
||||||
const getOrphanVisits = jest.fn();
|
const getOrphanVisits = jest.fn();
|
||||||
const cancelGetOrphanVisits = jest.fn();
|
const exportVisits = jest.fn();
|
||||||
const orphanVisits = Mock.all<VisitsInfo>();
|
const orphanVisits = Mock.of<VisitsInfo>({ visits: [Mock.of<Visit>({ date: formatISO(new Date()) })] });
|
||||||
const OrphanVisits = createOrphanVisits(Mock.all<ReportExporter>());
|
const OrphanVisits = createOrphanVisits(Mock.of<ReportExporter>({ exportVisits }));
|
||||||
|
const setUp = () => ({
|
||||||
const wrapper = shallow(
|
user: userEvent.setup(),
|
||||||
|
...render(
|
||||||
|
<MemoryRouter>
|
||||||
<OrphanVisits
|
<OrphanVisits
|
||||||
{...Mock.of<MercureBoundProps>({ mercureInfo: {} })}
|
{...Mock.of<MercureBoundProps>({ mercureInfo: {} })}
|
||||||
getOrphanVisits={getOrphanVisits}
|
getOrphanVisits={getOrphanVisits}
|
||||||
orphanVisits={orphanVisits}
|
orphanVisits={orphanVisits}
|
||||||
cancelGetOrphanVisits={cancelGetOrphanVisits}
|
cancelGetOrphanVisits={jest.fn()}
|
||||||
settings={Mock.all<Settings>()}
|
settings={Mock.all<Settings>()}
|
||||||
selectedServer={Mock.all<SelectedServer>()}
|
selectedServer={Mock.all<SelectedServer>()}
|
||||||
/>,
|
/>
|
||||||
).dive();
|
</MemoryRouter>,
|
||||||
const stats = wrapper.find(VisitsStats);
|
),
|
||||||
const header = wrapper.find(VisitsHeader);
|
});
|
||||||
|
|
||||||
expect(stats).toHaveLength(1);
|
it('wraps visits stats and header', () => {
|
||||||
expect(header).toHaveLength(1);
|
setUp();
|
||||||
expect(stats.prop('cancelGetVisits')).toEqual(cancelGetOrphanVisits);
|
expect(screen.getByRole('heading', { name: 'Orphan visits' })).toBeInTheDocument();
|
||||||
expect(stats.prop('visitsInfo')).toEqual(orphanVisits);
|
expect(getOrphanVisits).toHaveBeenCalled();
|
||||||
expect(stats.prop('isOrphanVisits')).toEqual(true);
|
});
|
||||||
expect(header.prop('visits')).toEqual(orphanVisits.visits);
|
|
||||||
expect(header.prop('goBack')).toEqual(expect.any(Function));
|
it('exports visits when clicking the button', async () => {
|
||||||
|
const { user } = setUp();
|
||||||
|
const btn = screen.getByRole('button', { name: 'Export (1)' });
|
||||||
|
|
||||||
|
expect(exportVisits).not.toHaveBeenCalled();
|
||||||
|
expect(btn).toBeInTheDocument();
|
||||||
|
|
||||||
|
await user.click(btn);
|
||||||
|
expect(exportVisits).toHaveBeenCalledWith('orphan_visits.csv', expect.anything());
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,49 +1,54 @@
|
||||||
import { shallow, ShallowWrapper } from 'enzyme';
|
import { render, screen } from '@testing-library/react';
|
||||||
|
import userEvent from '@testing-library/user-event';
|
||||||
|
import { MemoryRouter } from 'react-router-dom';
|
||||||
import { identity } from 'ramda';
|
import { identity } from 'ramda';
|
||||||
import { Mock } from 'ts-mockery';
|
import { Mock } from 'ts-mockery';
|
||||||
|
import { formatISO } from 'date-fns';
|
||||||
import { ShortUrlVisits as createShortUrlVisits, ShortUrlVisitsProps } from '../../src/visits/ShortUrlVisits';
|
import { ShortUrlVisits as createShortUrlVisits, ShortUrlVisitsProps } from '../../src/visits/ShortUrlVisits';
|
||||||
import { ShortUrlVisitsHeader } from '../../src/visits/ShortUrlVisitsHeader';
|
|
||||||
import { ShortUrlVisits as ShortUrlVisitsState } from '../../src/visits/reducers/shortUrlVisits';
|
import { ShortUrlVisits as ShortUrlVisitsState } from '../../src/visits/reducers/shortUrlVisits';
|
||||||
import { ShortUrlDetail } from '../../src/short-urls/reducers/shortUrlDetail';
|
import { ShortUrlDetail } from '../../src/short-urls/reducers/shortUrlDetail';
|
||||||
import { VisitsStats } from '../../src/visits/VisitsStats';
|
|
||||||
import { MercureBoundProps } from '../../src/mercure/helpers/boundToMercureHub';
|
import { MercureBoundProps } from '../../src/mercure/helpers/boundToMercureHub';
|
||||||
import { ReportExporter } from '../../src/common/services/ReportExporter';
|
import { ReportExporter } from '../../src/common/services/ReportExporter';
|
||||||
|
import { Visit } from '../../src/visits/types';
|
||||||
jest.mock('react-router-dom', () => ({
|
import { Settings } from '../../src/settings/reducers/settings';
|
||||||
...jest.requireActual('react-router-dom'),
|
|
||||||
useNavigate: jest.fn().mockReturnValue(jest.fn()),
|
|
||||||
useLocation: jest.fn().mockReturnValue({ search: '' }),
|
|
||||||
useParams: jest.fn().mockReturnValue({ shortCode: 'abc123' }),
|
|
||||||
}));
|
|
||||||
|
|
||||||
describe('<ShortUrlVisits />', () => {
|
describe('<ShortUrlVisits />', () => {
|
||||||
let wrapper: ShallowWrapper;
|
|
||||||
const getShortUrlVisitsMock = jest.fn();
|
const getShortUrlVisitsMock = jest.fn();
|
||||||
const ShortUrlVisits = createShortUrlVisits(Mock.all<ReportExporter>());
|
const exportVisits = jest.fn();
|
||||||
|
const shortUrlVisits = Mock.of<ShortUrlVisitsState>({ visits: [Mock.of<Visit>({ date: formatISO(new Date()) })] });
|
||||||
beforeEach(() => {
|
const ShortUrlVisits = createShortUrlVisits(Mock.of<ReportExporter>({ exportVisits }));
|
||||||
wrapper = shallow(
|
const setUp = () => ({
|
||||||
|
user: userEvent.setup(),
|
||||||
|
...render(
|
||||||
|
<MemoryRouter>
|
||||||
<ShortUrlVisits
|
<ShortUrlVisits
|
||||||
{...Mock.all<ShortUrlVisitsProps>()}
|
{...Mock.all<ShortUrlVisitsProps>()}
|
||||||
{...Mock.of<MercureBoundProps>({ mercureInfo: {} })}
|
{...Mock.of<MercureBoundProps>({ mercureInfo: {} })}
|
||||||
getShortUrlDetail={identity}
|
getShortUrlDetail={identity}
|
||||||
getShortUrlVisits={getShortUrlVisitsMock}
|
getShortUrlVisits={getShortUrlVisitsMock}
|
||||||
shortUrlVisits={Mock.of<ShortUrlVisitsState>({ loading: true, visits: [] })}
|
shortUrlVisits={shortUrlVisits}
|
||||||
shortUrlDetail={Mock.all<ShortUrlDetail>()}
|
shortUrlDetail={Mock.all<ShortUrlDetail>()}
|
||||||
|
settings={Mock.all<Settings>()}
|
||||||
cancelGetShortUrlVisits={() => {}}
|
cancelGetShortUrlVisits={() => {}}
|
||||||
/>,
|
/>
|
||||||
).dive(); // Dive is needed as this component is wrapped in a HOC
|
</MemoryRouter>,
|
||||||
|
),
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(jest.clearAllMocks);
|
it('wraps visits stats and header', () => {
|
||||||
afterEach(() => wrapper.unmount());
|
setUp();
|
||||||
|
expect(screen.getAllByRole('heading')[0]).toHaveTextContent('Visits for');
|
||||||
|
expect(getShortUrlVisitsMock).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
it('renders visit stats and visits header', () => {
|
it('exports visits when clicking the button', async () => {
|
||||||
const visitStats = wrapper.find(VisitsStats);
|
const { user } = setUp();
|
||||||
const visitHeader = wrapper.find(ShortUrlVisitsHeader);
|
const btn = screen.getByRole('button', { name: 'Export (1)' });
|
||||||
|
|
||||||
expect(visitStats).toHaveLength(1);
|
expect(exportVisits).not.toHaveBeenCalled();
|
||||||
expect(visitStats.prop('isOrphanVisits')).not.toBeDefined();
|
expect(btn).toBeInTheDocument();
|
||||||
expect(visitHeader).toHaveLength(1);
|
|
||||||
|
await user.click(btn);
|
||||||
|
expect(exportVisits).toHaveBeenCalledWith('short-url_undefined_visits.csv', expect.anything());
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,47 +1,59 @@
|
||||||
import { shallow, ShallowWrapper } from 'enzyme';
|
import { render, screen } from '@testing-library/react';
|
||||||
|
import userEvent from '@testing-library/user-event';
|
||||||
|
import { formatISO } from 'date-fns';
|
||||||
|
import { MemoryRouter } from 'react-router-dom';
|
||||||
import { Mock } from 'ts-mockery';
|
import { Mock } from 'ts-mockery';
|
||||||
import { TagVisits as createTagVisits, TagVisitsProps } from '../../src/visits/TagVisits';
|
import { TagVisits as createTagVisits, TagVisitsProps } from '../../src/visits/TagVisits';
|
||||||
import { TagVisitsHeader } from '../../src/visits/TagVisitsHeader';
|
|
||||||
import { ColorGenerator } from '../../src/utils/services/ColorGenerator';
|
import { ColorGenerator } from '../../src/utils/services/ColorGenerator';
|
||||||
import { TagVisits as TagVisitsStats } from '../../src/visits/reducers/tagVisits';
|
import { TagVisits as TagVisitsStats } from '../../src/visits/reducers/tagVisits';
|
||||||
import { VisitsStats } from '../../src/visits/VisitsStats';
|
|
||||||
import { MercureBoundProps } from '../../src/mercure/helpers/boundToMercureHub';
|
import { MercureBoundProps } from '../../src/mercure/helpers/boundToMercureHub';
|
||||||
import { ReportExporter } from '../../src/common/services/ReportExporter';
|
import { ReportExporter } from '../../src/common/services/ReportExporter';
|
||||||
|
import { Visit } from '../../src/visits/types';
|
||||||
|
import { Settings } from '../../src/settings/reducers/settings';
|
||||||
|
|
||||||
jest.mock('react-router-dom', () => ({
|
jest.mock('react-router-dom', () => ({
|
||||||
...jest.requireActual('react-router-dom'),
|
...jest.requireActual('react-router-dom'),
|
||||||
useNavigate: jest.fn().mockReturnValue(jest.fn()),
|
|
||||||
useLocation: jest.fn().mockReturnValue({}),
|
|
||||||
useParams: jest.fn().mockReturnValue({ tag: 'foo' }),
|
useParams: jest.fn().mockReturnValue({ tag: 'foo' }),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
describe('<TagVisits />', () => {
|
describe('<TagVisits />', () => {
|
||||||
let wrapper: ShallowWrapper;
|
|
||||||
const getTagVisitsMock = jest.fn();
|
const getTagVisitsMock = jest.fn();
|
||||||
|
const exportVisits = jest.fn();
|
||||||
beforeEach(() => {
|
const tagVisits = Mock.of<TagVisitsStats>({ visits: [Mock.of<Visit>({ date: formatISO(new Date()) })] });
|
||||||
const TagVisits = createTagVisits(Mock.all<ColorGenerator>(), Mock.all<ReportExporter>());
|
const TagVisits = createTagVisits(
|
||||||
|
Mock.of<ColorGenerator>({ isColorLightForKey: () => false, getColorForKey: () => 'red' }),
|
||||||
wrapper = shallow(
|
Mock.of<ReportExporter>({ exportVisits }),
|
||||||
|
);
|
||||||
|
const setUp = () => ({
|
||||||
|
user: userEvent.setup(),
|
||||||
|
...render(
|
||||||
|
<MemoryRouter>
|
||||||
<TagVisits
|
<TagVisits
|
||||||
{...Mock.all<TagVisitsProps>()}
|
{...Mock.all<TagVisitsProps>()}
|
||||||
{...Mock.of<MercureBoundProps>({ mercureInfo: {} })}
|
{...Mock.of<MercureBoundProps>({ mercureInfo: {} })}
|
||||||
getTagVisits={getTagVisitsMock}
|
getTagVisits={getTagVisitsMock}
|
||||||
tagVisits={Mock.of<TagVisitsStats>({ loading: true, visits: [] })}
|
tagVisits={tagVisits}
|
||||||
|
settings={Mock.all<Settings>()}
|
||||||
cancelGetTagVisits={() => {}}
|
cancelGetTagVisits={() => {}}
|
||||||
/>,
|
/>
|
||||||
).dive(); // Dive is needed as this component is wrapped in a HOC
|
</MemoryRouter>,
|
||||||
|
),
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => wrapper.unmount());
|
it('wraps visits stats and header', () => {
|
||||||
afterEach(jest.resetAllMocks);
|
setUp();
|
||||||
|
expect(screen.getAllByRole('heading')[0]).toHaveTextContent('Visits for');
|
||||||
|
expect(getTagVisitsMock).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
it('renders visit stats and visits header', () => {
|
it('exports visits when clicking the button', async () => {
|
||||||
const visitStats = wrapper.find(VisitsStats);
|
const { user } = setUp();
|
||||||
const visitHeader = wrapper.find(TagVisitsHeader);
|
const btn = screen.getByRole('button', { name: 'Export (1)' });
|
||||||
|
|
||||||
expect(visitStats).toHaveLength(1);
|
expect(exportVisits).not.toHaveBeenCalled();
|
||||||
expect(visitStats.prop('isOrphanVisits')).not.toBeDefined();
|
expect(btn).toBeInTheDocument();
|
||||||
expect(visitHeader).toHaveLength(1);
|
|
||||||
|
await user.click(btn);
|
||||||
|
expect(exportVisits).toHaveBeenCalledWith('tag_foo_visits.csv', expect.anything());
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue