shlink-web-client/test/visits/ShortUrlVisits.test.js

105 lines
3.8 KiB
JavaScript
Raw Normal View History

2018-09-08 14:28:40 +03:00
import React from 'react';
import { shallow } from 'enzyme';
import { identity } from 'ramda';
import { Card } from 'reactstrap';
import createShortUrlVisits from '../../src/visits/ShortUrlVisits';
2018-09-08 14:28:40 +03:00
import MutedMessage from '../../src/utils/MuttedMessage';
import GraphCard from '../../src/visits/GraphCard';
2018-11-01 11:05:20 +03:00
import DateInput from '../../src/utils/DateInput';
2018-10-29 01:04:52 +03:00
import SortableBarGraph from '../../src/visits/SortableBarGraph';
2018-09-08 14:28:40 +03:00
describe('<ShortUrlVisits />', () => {
let wrapper;
const processStatsFromVisits = () => (
{ os: {}, browsers: {}, referrers: {}, countries: {}, cities: {}, citiesForMap: {} }
);
2019-04-19 13:41:59 +03:00
const getShortUrlVisitsMock = jest.fn();
2018-09-08 14:28:40 +03:00
const match = {
params: { shortCode: 'abc123' },
};
const createComponent = (shortUrlVisits) => {
const ShortUrlVisits = createShortUrlVisits({ processStatsFromVisits });
2018-09-08 14:28:40 +03:00
wrapper = shallow(
<ShortUrlVisits
2018-09-08 14:28:40 +03:00
getShortUrlDetail={identity}
getShortUrlVisits={getShortUrlVisitsMock}
match={match}
shortUrlVisits={shortUrlVisits}
shortUrlDetail={{}}
cancelGetShortUrlVisits={identity}
2018-09-08 14:28:40 +03:00
/>
);
return wrapper;
};
afterEach(() => {
2019-04-19 13:41:59 +03:00
getShortUrlVisitsMock.mockReset();
wrapper && wrapper.unmount();
2018-09-08 14:28:40 +03:00
});
it('renders a preloader when visits are loading', () => {
2018-09-08 14:28:40 +03:00
const wrapper = createComponent({ loading: true });
const loadingMessage = wrapper.find(MutedMessage);
expect(loadingMessage).toHaveLength(1);
expect(loadingMessage.html()).toContain('Loading...');
});
it('renders a warning when loading large amounts of visits', () => {
const wrapper = createComponent({ loading: true, loadingLarge: true });
const loadingMessage = wrapper.find(MutedMessage);
expect(loadingMessage).toHaveLength(1);
expect(loadingMessage.html()).toContain('This is going to take a while... :S');
});
2018-09-08 14:28:40 +03:00
it('renders an error message when visits could not be loaded', () => {
const wrapper = createComponent({ loading: false, error: true });
const errorMessage = wrapper.find(Card);
expect(errorMessage).toHaveLength(1);
expect(errorMessage.html()).toContain('An error occurred while loading visits :(');
});
it('renders a message when visits are loaded but the list is empty', () => {
const wrapper = createComponent({ loading: false, error: false, visits: [] });
const message = wrapper.find(MutedMessage);
expect(message).toHaveLength(1);
expect(message.html()).toContain('There are no visits matching current filter :(');
});
it('renders all graphics when visits are properly loaded', () => {
const wrapper = createComponent({ loading: false, error: false, visits: [{}, {}, {}] });
const graphs = wrapper.find(GraphCard);
2018-10-29 01:04:52 +03:00
const sortableBarGraphs = wrapper.find(SortableBarGraph);
const expectedGraphsCount = 5;
2018-09-08 14:28:40 +03:00
2018-10-29 01:04:52 +03:00
expect(graphs.length + sortableBarGraphs.length).toEqual(expectedGraphsCount);
2018-09-08 14:28:40 +03:00
});
it('reloads visits when selected dates change', () => {
const wrapper = createComponent({ loading: false, error: false, visits: [{}, {}, {}] });
const dateInput = wrapper.find(DateInput).first();
dateInput.simulate('change', '2016-01-01T00:00:00+01:00');
dateInput.simulate('change', '2016-01-02T00:00:00+01:00');
dateInput.simulate('change', '2016-01-03T00:00:00+01:00');
2019-04-19 13:41:59 +03:00
expect(getShortUrlVisitsMock).toHaveBeenCalledTimes(4);
2018-09-08 14:28:40 +03:00
expect(wrapper.state('startDate')).toEqual('2016-01-03T00:00:00+01:00');
});
it('holds the map button content generator on cities graph extraHeaderContent', () => {
const wrapper = createComponent({ loading: false, error: false, visits: [{}, {}, {}] });
const citiesGraph = wrapper.find(SortableBarGraph).find('[title="Cities"]');
const extraHeaderContent = citiesGraph.prop('extraHeaderContent');
expect(extraHeaderContent).toHaveLength(1);
expect(typeof extraHeaderContent).toEqual('function');
});
2018-09-08 14:28:40 +03:00
});