Skip to content

Commit 3b361e6

Browse files
committed
Model timeline with one named section as sectioned
1 parent 2bf8190 commit 3b361e6

File tree

2 files changed

+102
-10
lines changed

2 files changed

+102
-10
lines changed

dotcom-rendering/src/model/enhanceTimeline.test.ts

Lines changed: 100 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import { enhanceTimeline } from './enhanceTimeline';
55

66
const identity = <A>(a: A): A => a;
77

8-
const elements: FEElement[] = [
8+
const elementsWithNoSections: FEElement[] = [
99
{
1010
_type: 'model.dotcomrendering.pageElements.TimelineBlockElement',
1111
elementId: 'mock-id',
1212
sections: [
1313
{
14-
title: 'mock section title',
14+
title: '',
1515
events: [
1616
{
1717
title: 'mock event title',
@@ -73,9 +73,76 @@ const elements: FEElement[] = [
7373
},
7474
];
7575

76+
const elementsWithOneSection: FEElement[] = [
77+
{
78+
_type: 'model.dotcomrendering.pageElements.TimelineBlockElement',
79+
elementId: 'mock-id',
80+
sections: [
81+
{
82+
title: 'Section 1',
83+
events: [
84+
{
85+
title: 'mock event title',
86+
date: '1st January 2024',
87+
body: [],
88+
// Showcase image
89+
main: images[0],
90+
},
91+
{
92+
title: 'mock event title',
93+
date: '5th January 2024',
94+
body: [],
95+
// Half width image
96+
main: images[3],
97+
},
98+
],
99+
},
100+
],
101+
},
102+
];
103+
104+
const elementsWithMultipleSections: FEElement[] = [
105+
{
106+
_type: 'model.dotcomrendering.pageElements.TimelineBlockElement',
107+
elementId: 'mock-id',
108+
sections: [
109+
{
110+
title: 'Section 1',
111+
events: [
112+
{
113+
title: 'Event 1 title',
114+
date: '1st January 2024',
115+
body: [],
116+
},
117+
{
118+
title: 'Event 2 title',
119+
date: '5th January 2024',
120+
body: [],
121+
},
122+
],
123+
},
124+
{
125+
title: 'Section 2',
126+
events: [
127+
{
128+
title: 'Event 3 title',
129+
date: '1st March 2024',
130+
body: [],
131+
},
132+
{
133+
title: 'Event 4 title',
134+
date: '5th March 2024',
135+
body: [],
136+
},
137+
],
138+
},
139+
],
140+
},
141+
];
142+
76143
describe('enhanceTimeline', () => {
77144
it('keeps a main media with a role that is valid', () => {
78-
const enhanced = enhanceTimeline(identity)(elements);
145+
const enhanced = enhanceTimeline(identity)(elementsWithNoSections);
79146
assert.equal(
80147
enhanced[0]?._type,
81148
'model.dotcomrendering.pageElements.DCRTimelineBlockElement',
@@ -87,7 +154,7 @@ describe('enhanceTimeline', () => {
87154
});
88155

89156
it('drops a main media with a role that is not valid', () => {
90-
const enhanced = enhanceTimeline(identity)(elements);
157+
const enhanced = enhanceTimeline(identity)(elementsWithNoSections);
91158
assert.equal(
92159
enhanced[0]?._type,
93160
'model.dotcomrendering.pageElements.DCRTimelineBlockElement',
@@ -99,7 +166,7 @@ describe('enhanceTimeline', () => {
99166
});
100167

101168
it('keeps a main media without a role', () => {
102-
const enhanced = enhanceTimeline(identity)(elements);
169+
const enhanced = enhanceTimeline(identity)(elementsWithNoSections);
103170
assert.equal(
104171
enhanced[0]?._type,
105172
'model.dotcomrendering.pageElements.DCRTimelineBlockElement',
@@ -110,7 +177,7 @@ describe('enhanceTimeline', () => {
110177
expect(timelineEvent?.main).toBeDefined();
111178
});
112179
it('keeps a body element with a role that is valid', () => {
113-
const enhanced = enhanceTimeline(identity)(elements);
180+
const enhanced = enhanceTimeline(identity)(elementsWithNoSections);
114181
assert.equal(
115182
enhanced[0]?._type,
116183
'model.dotcomrendering.pageElements.DCRTimelineBlockElement',
@@ -122,7 +189,7 @@ describe('enhanceTimeline', () => {
122189
});
123190

124191
it('drops a body element with a role that is not valid', () => {
125-
const enhanced = enhanceTimeline(identity)(elements);
192+
const enhanced = enhanceTimeline(identity)(elementsWithNoSections);
126193
assert.equal(
127194
enhanced[0]?._type,
128195
'model.dotcomrendering.pageElements.DCRTimelineBlockElement',
@@ -134,7 +201,7 @@ describe('enhanceTimeline', () => {
134201
});
135202

136203
it('keeps a body element without a role', () => {
137-
const enhanced = enhanceTimeline(identity)(elements);
204+
const enhanced = enhanceTimeline(identity)(elementsWithNoSections);
138205
assert.equal(
139206
enhanced[0]?._type,
140207
'model.dotcomrendering.pageElements.DCRTimelineBlockElement',
@@ -151,4 +218,29 @@ describe('enhanceTimeline', () => {
151218
},
152219
]);
153220
});
221+
222+
it('enhances a timeline with one section appropriately', () => {
223+
const enhanced = enhanceTimeline(identity)(elementsWithOneSection);
224+
assert.equal(
225+
enhanced[0]?._type,
226+
'model.dotcomrendering.pageElements.DCRSectionedTimelineBlockElement',
227+
);
228+
229+
const timelineSection = enhanced[0].sections[0];
230+
assert.notEqual(timelineSection, undefined);
231+
expect(timelineSection?.title).toEqual('Section 1');
232+
});
233+
234+
it('enhances a timeline with multiple sections appropriately', () => {
235+
const enhanced = enhanceTimeline(identity)(
236+
elementsWithMultipleSections,
237+
);
238+
assert.equal(
239+
enhanced[0]?._type,
240+
'model.dotcomrendering.pageElements.DCRSectionedTimelineBlockElement',
241+
);
242+
243+
const timelineSections = enhanced[0].sections;
244+
expect(timelineSections).toHaveLength(2);
245+
});
154246
});

dotcom-rendering/src/model/enhanceTimeline.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ const enhanceTimelineBlockElement = (
9494
return [];
9595
}
9696

97-
/* A timeline with one section is "flat", it's not considered to be split
97+
/* A timeline with one unnamed section is "flat", it's not considered to be split
9898
* into sections. It's just represented as having one section for CAPI
9999
* modelling reasons, but we can model it more specifically here.
100100
*/
101-
if (otherSections.length === 0) {
101+
if (otherSections.length === 0 && firstSection.title === '') {
102102
return [
103103
{
104104
_type: 'model.dotcomrendering.pageElements.DCRTimelineBlockElement',

0 commit comments

Comments
 (0)