From e0db6d5a578c14b07e3ffc1fcb917500e11ea5c8 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 10 Mar 2019 17:55:02 +0100 Subject: [PATCH] Improved SortableBarGraph test --- src/visits/GraphCard.js | 4 +- src/visits/SortableBarGraph.js | 4 +- test/visits/SortableBarGraph.test.js | 74 +++++++++++++++++++++++----- 3 files changed, 65 insertions(+), 17 deletions(-) diff --git a/src/visits/GraphCard.js b/src/visits/GraphCard.js index b8d8d94c..0b2c5c0b 100644 --- a/src/visits/GraphCard.js +++ b/src/visits/GraphCard.js @@ -6,7 +6,7 @@ import { keys, values } from 'ramda'; import './GraphCard.scss'; const propTypes = { - title: PropTypes.oneOfType([ PropTypes.string, PropTypes.node ]), + title: PropTypes.oneOfType([ PropTypes.string, PropTypes.func ]), footer: PropTypes.oneOfType([ PropTypes.string, PropTypes.node ]), isBarChart: PropTypes.bool, stats: PropTypes.object, @@ -65,7 +65,7 @@ const renderGraph = (title, isBarChart, stats, max, redraw) => { const GraphCard = ({ title, footer, isBarChart, stats, max, redraw = false }) => ( - {title} + {typeof title === 'function' ? title() : title} {renderGraph(title, isBarChart, stats, max, redraw)} {footer && {footer}} diff --git a/src/visits/SortableBarGraph.js b/src/visits/SortableBarGraph.js index 4096467a..2d3c6146 100644 --- a/src/visits/SortableBarGraph.js +++ b/src/visits/SortableBarGraph.js @@ -97,7 +97,7 @@ export default class SortableBarGraph extends React.Component { const { stats, sortingItems, title, extraHeaderContent, withPagination = true } = this.props; const { currentPageStats, pagination, max } = this.determineStats(stats, sortingItems); const activeCities = keys(currentPageStats); - const computedTitle = ( + const computeTitle = () => ( {title}
@@ -134,7 +134,7 @@ export default class SortableBarGraph extends React.Component { return ( ', () => { let wrapper; @@ -15,9 +17,14 @@ describe('', () => { Foo: 100, Bar: 50, }; - const createWrapper = (extraHeaderContent) => { + const createWrapper = (withPagination = false, extraStats = {}) => { wrapper = shallow( - + ); return wrapper; @@ -37,7 +44,7 @@ describe('', () => { beforeEach(() => { const wrapper = createWrapper(); - const dropdown = wrapper.find(SortingDropdown); + const dropdown = wrapper.renderProp('title')().find(SortingDropdown); assert = (sortName, sortDir, expectedKeys, expectedValues, done) => { dropdown.prop('onChange')(sortName, sortDir); @@ -59,15 +66,56 @@ describe('', () => { it('value - DESC', (done) => assert('value', 'DESC', [ 'Foo', 'Bar' ], [ 100, 50 ], done)); }); - it('renders extra header functions', () => { - const wrapper = createWrapper(( - - Foo - Bar - - )); + describe('renders properly paginated stats when pagination is set', () => { + let assert; - expect(wrapper.find('.foo-span')).toHaveLength(1); - expect(wrapper.find('.bar-span')).toHaveLength(1); + beforeEach(() => { + const wrapper = createWrapper(true, range(1, 159).reduce((accum, value) => { + accum[`key_${value}`] = value; + + return accum; + }, {})); + const dropdown = wrapper.renderProp('title')().find(PaginationDropdown); + + assert = (itemsPerPage, expectedStats, done) => { + dropdown.prop('setValue')(itemsPerPage); + setImmediate(() => { + const graphCard = wrapper.find(GraphCard); + const statsKeys = keys(graphCard.prop('stats')); + + expect(statsKeys).toEqual(expectedStats); + done(); + }); + }; + }); + + const buildExpected = (size) => [ 'Foo', 'Bar', ...rangeOf(size - 2, (i) => `key_${i}`) ]; + + it('50 items per page', (done) => assert(50, buildExpected(50), done)); + it('100 items per page', (done) => assert(100, buildExpected(100), done)); + it('200 items per page', (done) => assert(200, buildExpected(160), done)); + it('500 items per page', (done) => assert(500, buildExpected(160), done)); + }); + + it('renders extra header content', () => { + wrapper = shallow( + + ( + + Foo + Bar + + )} + /> + + ).find(SortableBarGraph); + const header = wrapper.renderProp('extraHeaderContent')(); + + expect(header.find('.foo-span')).toHaveLength(1); + expect(header.find('.bar-span')).toHaveLength(1); }); });