From d6c0c8ea5e59c15b96369c102b38b5b19e8c0a4c Mon Sep 17 00:00:00 2001 From: dan-searle Date: Tue, 22 Jan 2019 12:02:42 +0000 Subject: [PATCH] Handle scenario where latestItemTime results in no "earlier today" items to show. --- .../__tests__/TeaserTimeline.test.jsx | 50 +++++++++++++++---- .../x-teaser-timeline/src/lib/transform.js | 19 ++++--- 2 files changed, 53 insertions(+), 16 deletions(-) diff --git a/components/x-teaser-timeline/__tests__/TeaserTimeline.test.jsx b/components/x-teaser-timeline/__tests__/TeaserTimeline.test.jsx index 661eaf727..b13db1fe7 100644 --- a/components/x-teaser-timeline/__tests__/TeaserTimeline.test.jsx +++ b/components/x-teaser-timeline/__tests__/TeaserTimeline.test.jsx @@ -30,6 +30,24 @@ describe('x-teaser-timeline', () => { }); }); + describe('given latestItemsTime is set and results in all today`s articles being "latest"', () => { + let component; + + beforeEach(() => { + component = shallow( + ); + }); + + it('does not render the empty "earlier today" group', () => { + expect(component.render().find('section')).toHaveLength(3); + expect(component.render().find('section h2').text().toLowerCase().includes('earlier today')).toBe(false); + }); + }); + describe('given latestItemsTime is not set', () => { beforeEach(() => { tree = renderer.create().toJSON(); @@ -94,9 +112,13 @@ describe('x-teaser-timeline', () => { describe('custom slot content is a string', () => { describe('without latestArticlesTime set', () => { beforeEach(() => { - props.customSlotContent = '
Custom slot content
'; - props.customSlotPosition = 3; - component = shallow(); + component = shallow( + + ); }); it('has custom content in correct position', () => { @@ -107,10 +129,14 @@ describe('x-teaser-timeline', () => { describe('with latestArticlesTime set', () => { beforeEach(() => { - props.customSlotContent = '
Custom slot content
'; - props.customSlotPosition = 2; - props.latestItemsTime = '2018-10-16T12:10:33.000Z'; - component = shallow(); + component = shallow( + + ); }); it('has custom content in correct position', () => { @@ -122,9 +148,13 @@ describe('x-teaser-timeline', () => { describe('custom slot content is a node', () => { beforeEach(() => { - props.customSlotContent =
Custom slot content
; - props.customSlotPosition = 3; - component = shallow(); + component = shallow( + Custom slot content} + customSlotPosition={3} + /> + ); }); it('has custom content in correct position', () => { diff --git a/components/x-teaser-timeline/src/lib/transform.js b/components/x-teaser-timeline/src/lib/transform.js index f69238455..dd13d4433 100644 --- a/components/x-teaser-timeline/src/lib/transform.js +++ b/components/x-teaser-timeline/src/lib/transform.js @@ -36,19 +36,26 @@ export const splitTodaysItems = (itemGroups, localTodayDate, latestItemsTime) => } const { latestItems, earlierItems } = splitLatestEarlier(itemGroups[0].items, latestItemsTime); - - itemGroups[0] = { - date: 'today-earlier', - items: earlierItems - }; + const splitGroups = []; if (latestItems.length) { - itemGroups.unshift({ + splitGroups.push({ date: 'today-latest', items: latestItems }); } + if (earlierItems.length) { + splitGroups.push({ + date: 'today-earlier', + items: earlierItems + }); + } + + if (splitGroups.length) { + itemGroups.splice(0, 1, ...splitGroups); + } + return itemGroups; };