mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-03 06:11:48 +00:00
Extracted visits charts elements into reusable component
This commit is contained in:
@@ -51,7 +51,7 @@ describe('<TagCard />', () => {
|
||||
|
||||
expect(links.at(1).prop('to')).toEqual('/server/1/list-short-urls/1?tag=ssr');
|
||||
expect(links.at(1).text()).toContain('48');
|
||||
expect(links.at(2).prop('to')).toEqual('/server/1/tags/ssr/visits');
|
||||
expect(links.at(2).prop('to')).toEqual('/server/1/tag/ssr/visits');
|
||||
expect(links.at(2).text()).toContain('23,257');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,18 +1,11 @@
|
||||
import React from 'react';
|
||||
import { shallow } from 'enzyme';
|
||||
import { identity } from 'ramda';
|
||||
import { Card } from 'reactstrap';
|
||||
import createShortUrlVisits from '../../src/visits/ShortUrlVisits';
|
||||
import Message from '../../src/utils/Message';
|
||||
import GraphCard from '../../src/visits/GraphCard';
|
||||
import SortableBarGraph from '../../src/visits/SortableBarGraph';
|
||||
import DateRangeRow from '../../src/utils/DateRangeRow';
|
||||
import VisitsHeader from '../../src/visits/VisitsHeader';
|
||||
|
||||
describe('<ShortUrlVisits />', () => {
|
||||
let wrapper;
|
||||
const processStatsFromVisits = () => (
|
||||
{ os: {}, browsers: {}, referrers: {}, countries: {}, cities: {}, citiesForMap: {} }
|
||||
);
|
||||
const getShortUrlVisitsMock = jest.fn();
|
||||
const match = {
|
||||
params: { shortCode: 'abc123' },
|
||||
@@ -22,9 +15,10 @@ describe('<ShortUrlVisits />', () => {
|
||||
goBack: jest.fn(),
|
||||
};
|
||||
const realTimeUpdates = { enabled: true };
|
||||
const VisitsStats = jest.fn();
|
||||
|
||||
const createComponent = (shortUrlVisits) => {
|
||||
const ShortUrlVisits = createShortUrlVisits({ processStatsFromVisits, normalizeVisits: identity }, () => '');
|
||||
beforeEach(() => {
|
||||
const ShortUrlVisits = createShortUrlVisits(VisitsStats);
|
||||
|
||||
wrapper = shallow(
|
||||
<ShortUrlVisits
|
||||
@@ -33,77 +27,23 @@ describe('<ShortUrlVisits />', () => {
|
||||
match={match}
|
||||
location={location}
|
||||
history={history}
|
||||
shortUrlVisits={shortUrlVisits}
|
||||
shortUrlVisits={{ loading: true, visits: [] }}
|
||||
shortUrlDetail={{}}
|
||||
cancelGetShortUrlVisits={identity}
|
||||
matchMedia={() => ({ matches: false })}
|
||||
settings={{ realTimeUpdates }}
|
||||
/>
|
||||
);
|
||||
|
||||
return wrapper;
|
||||
};
|
||||
|
||||
afterEach(() => wrapper && wrapper.unmount());
|
||||
|
||||
it('renders a preloader when visits are loading', () => {
|
||||
const wrapper = createComponent({ loading: true, visits: [] });
|
||||
const loadingMessage = wrapper.find(Message);
|
||||
|
||||
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, visits: [] });
|
||||
const loadingMessage = wrapper.find(Message);
|
||||
afterEach(() => wrapper.unmount());
|
||||
afterEach(jest.resetAllMocks);
|
||||
|
||||
expect(loadingMessage).toHaveLength(1);
|
||||
expect(loadingMessage.html()).toContain('This is going to take a while... :S');
|
||||
});
|
||||
it('renders visit stats and visits header', () => {
|
||||
const visitStats = wrapper.find(VisitsStats);
|
||||
const visitHeader = wrapper.find(VisitsHeader);
|
||||
|
||||
it('renders an error message when visits could not be loaded', () => {
|
||||
const wrapper = createComponent({ loading: false, error: true, visits: [] });
|
||||
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(Message);
|
||||
|
||||
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);
|
||||
const sortableBarGraphs = wrapper.find(SortableBarGraph);
|
||||
|
||||
expect(graphs.length + sortableBarGraphs.length).toEqual(5);
|
||||
});
|
||||
|
||||
it('reloads visits when selected dates change', () => {
|
||||
const wrapper = createComponent({ loading: false, error: false, visits: [{}, {}, {}] });
|
||||
const dateRange = wrapper.find(DateRangeRow);
|
||||
|
||||
dateRange.simulate('startDateChange', '2016-01-01T00:00:00+01:00');
|
||||
dateRange.simulate('endDateChange', '2016-01-02T00:00:00+01:00');
|
||||
dateRange.simulate('endDateChange', '2016-01-03T00:00:00+01:00');
|
||||
|
||||
expect(wrapper.find(DateRangeRow).prop('startDate')).toEqual('2016-01-01T00:00:00+01:00');
|
||||
expect(wrapper.find(DateRangeRow).prop('endDate')).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');
|
||||
expect(visitStats).toHaveLength(1);
|
||||
expect(visitHeader).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
95
test/visits/VisitsStats.test.js
Normal file
95
test/visits/VisitsStats.test.js
Normal file
@@ -0,0 +1,95 @@
|
||||
import React from 'react';
|
||||
import { shallow } from 'enzyme';
|
||||
import { identity } from 'ramda';
|
||||
import { Card } from 'reactstrap';
|
||||
import createVisitStats from '../../src/visits/VisitsStats';
|
||||
import Message from '../../src/utils/Message';
|
||||
import GraphCard from '../../src/visits/GraphCard';
|
||||
import SortableBarGraph from '../../src/visits/SortableBarGraph';
|
||||
import DateRangeRow from '../../src/utils/DateRangeRow';
|
||||
|
||||
describe('<VisitStats />', () => {
|
||||
let wrapper;
|
||||
const processStatsFromVisits = () => (
|
||||
{ os: {}, browsers: {}, referrers: {}, countries: {}, cities: {}, citiesForMap: {} }
|
||||
);
|
||||
const getVisitsMock = jest.fn();
|
||||
|
||||
const createComponent = (visitsInfo) => {
|
||||
const VisitStats = createVisitStats({ processStatsFromVisits, normalizeVisits: identity }, () => '');
|
||||
|
||||
wrapper = shallow(
|
||||
<VisitStats
|
||||
getVisits={getVisitsMock}
|
||||
visitsInfo={visitsInfo}
|
||||
cancelGetVisits={identity}
|
||||
matchMedia={() => ({ matches: false })}
|
||||
/>
|
||||
);
|
||||
|
||||
return wrapper;
|
||||
};
|
||||
|
||||
afterEach(() => wrapper && wrapper.unmount());
|
||||
|
||||
it('renders a preloader when visits are loading', () => {
|
||||
const wrapper = createComponent({ loading: true, visits: [] });
|
||||
const loadingMessage = wrapper.find(Message);
|
||||
|
||||
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, visits: [] });
|
||||
const loadingMessage = wrapper.find(Message);
|
||||
|
||||
expect(loadingMessage).toHaveLength(1);
|
||||
expect(loadingMessage.html()).toContain('This is going to take a while... :S');
|
||||
});
|
||||
|
||||
it('renders an error message when visits could not be loaded', () => {
|
||||
const wrapper = createComponent({ loading: false, error: true, visits: [] });
|
||||
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(Message);
|
||||
|
||||
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);
|
||||
const sortableBarGraphs = wrapper.find(SortableBarGraph);
|
||||
|
||||
expect(graphs.length + sortableBarGraphs.length).toEqual(5);
|
||||
});
|
||||
|
||||
it('reloads visits when selected dates change', () => {
|
||||
const wrapper = createComponent({ loading: false, error: false, visits: [{}, {}, {}] });
|
||||
const dateRange = wrapper.find(DateRangeRow);
|
||||
|
||||
dateRange.simulate('startDateChange', '2016-01-01T00:00:00+01:00');
|
||||
dateRange.simulate('endDateChange', '2016-01-02T00:00:00+01:00');
|
||||
dateRange.simulate('endDateChange', '2016-01-03T00:00:00+01:00');
|
||||
|
||||
expect(wrapper.find(DateRangeRow).prop('startDate')).toEqual('2016-01-01T00:00:00+01:00');
|
||||
expect(wrapper.find(DateRangeRow).prop('endDate')).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');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user