Skip to content

Commit c0c0a54

Browse files
committed
add tests for gallery ad placeholder logic
1 parent 8cb9539 commit c0c0a54

File tree

1 file changed

+219
-122
lines changed

1 file changed

+219
-122
lines changed

dotcom-rendering/src/model/enhance-ad-placeholders.test.ts

Lines changed: 219 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ const exampleFormat = {
1414
theme: Pillar.Culture,
1515
};
1616

17+
const galleryFormat = {
18+
design: ArticleDesign.Gallery,
19+
display: ArticleDisplay.Immersive,
20+
theme: Pillar.News,
21+
};
22+
1723
// Test helper functions
1824

1925
const getTestParagraphElements = (length: number): TextBlockElement[] =>
@@ -23,6 +29,17 @@ const getTestParagraphElements = (length: number): TextBlockElement[] =>
2329
html: '<p>I am a paragraph</p>',
2430
});
2531

32+
const getTestImageBlockElements = (length: number): ImageBlockElement[] =>
33+
Array<ImageBlockElement>(length).fill({
34+
_type: 'model.dotcomrendering.pageElements.ImageBlockElement',
35+
elementId: 'mockId',
36+
media: { allImages: [] },
37+
data: {},
38+
displayCredit: true,
39+
imageSources: [],
40+
role: 'inline',
41+
});
42+
2643
const getInlineImageElement = (): ImageBlockElement => ({
2744
_type: 'model.dotcomrendering.pageElements.ImageBlockElement',
2845
media: { allImages: [] },
@@ -56,163 +73,243 @@ const elementIsAdPlaceholder = (
5673
'model.dotcomrendering.pageElements.AdPlaceholderBlockElement';
5774

5875
// Tests
59-
describe('Enhancing ad placeholders', () => {
60-
const testCases = [
61-
{ paragraphs: 0, expectedPositions: [] },
62-
{ paragraphs: 1, expectedPositions: [] },
63-
{ paragraphs: 3, expectedPositions: [] },
64-
{ paragraphs: 6, expectedPositions: [3] },
65-
{ paragraphs: 9, expectedPositions: [3] },
66-
{ paragraphs: 11, expectedPositions: [3, 10] },
67-
{ paragraphs: 12, expectedPositions: [3, 10] },
68-
{
69-
paragraphs: 16,
70-
expectedPositions: [3, 10],
71-
},
72-
{
73-
paragraphs: 87,
74-
expectedPositions: [
75-
3, 10, 17, 24, 31, 38, 45, 52, 59, 66, 73, 80, 87, 94,
76-
],
77-
},
78-
{
79-
paragraphs: 88,
80-
expectedPositions: [
81-
3, 10, 17, 24, 31, 38, 45, 52, 59, 66, 73, 80, 87, 94,
82-
],
83-
},
84-
{
85-
paragraphs: 999,
86-
expectedPositions: [
87-
3, 10, 17, 24, 31, 38, 45, 52, 59, 66, 73, 80, 87, 94, 101,
88-
],
89-
},
90-
] satisfies Array<{ paragraphs: number; expectedPositions: number[] }>;
91-
92-
describe.each(testCases)(
93-
'for $paragraphs paragraph(s) in an article',
94-
({ paragraphs, expectedPositions }) => {
95-
const elements = getTestParagraphElements(paragraphs);
96-
const expectedPlaceholders = expectedPositions.length;
76+
describe('enhanceAdPlaceholders', () => {
77+
describe('for general articles', () => {
78+
const testCases = [
79+
{ paragraphs: 0, expectedPositions: [] },
80+
{ paragraphs: 1, expectedPositions: [] },
81+
{ paragraphs: 3, expectedPositions: [] },
82+
{ paragraphs: 6, expectedPositions: [3] },
83+
{ paragraphs: 9, expectedPositions: [3] },
84+
{ paragraphs: 11, expectedPositions: [3, 10] },
85+
{ paragraphs: 12, expectedPositions: [3, 10] },
86+
{
87+
paragraphs: 16,
88+
expectedPositions: [3, 10],
89+
},
90+
{
91+
paragraphs: 87,
92+
expectedPositions: [
93+
3, 10, 17, 24, 31, 38, 45, 52, 59, 66, 73, 80, 87, 94,
94+
],
95+
},
96+
{
97+
paragraphs: 88,
98+
expectedPositions: [
99+
3, 10, 17, 24, 31, 38, 45, 52, 59, 66, 73, 80, 87, 94,
100+
],
101+
},
102+
{
103+
paragraphs: 999,
104+
expectedPositions: [
105+
3, 10, 17, 24, 31, 38, 45, 52, 59, 66, 73, 80, 87, 94, 101,
106+
],
107+
},
108+
] satisfies Array<{ paragraphs: number; expectedPositions: number[] }>;
109+
110+
describe.each(testCases)(
111+
'for $paragraphs paragraph(s) in an article',
112+
({ paragraphs, expectedPositions }) => {
113+
const elements = getTestParagraphElements(paragraphs);
114+
const expectedPlaceholders = expectedPositions.length;
115+
const input: FEElement[] = elements;
116+
117+
const output = enhanceAdPlaceholders(
118+
exampleFormat,
119+
'Apps',
120+
false,
121+
)(input);
122+
const placeholderIndices = output.flatMap((el, idx) =>
123+
elementIsAdPlaceholder(el) ? [idx] : [],
124+
);
125+
126+
it(`should insert ${expectedPlaceholders} ad placeholder(s)`, () => {
127+
expect(placeholderIndices.length).toEqual(
128+
expectedPlaceholders,
129+
);
130+
});
131+
132+
if (expectedPlaceholders > 0) {
133+
it(`should insert ad placeholder(s) in the expected position(s): ${expectedPositions.join(
134+
',',
135+
)}`, () => {
136+
expect(placeholderIndices).toEqual(expectedPositions);
137+
});
138+
}
139+
},
140+
);
141+
142+
it('should not insert an ad placeholder before an inline image element, but can insert it after the image', () => {
143+
const threeParagraphs = getTestParagraphElements(3);
144+
145+
const elements = [
146+
...threeParagraphs,
147+
getInlineImageElement(),
148+
...threeParagraphs,
149+
];
150+
97151
const input: FEElement[] = elements;
98152

99153
const output = enhanceAdPlaceholders(
100154
exampleFormat,
101155
'Apps',
102156
false,
103157
)(input);
158+
const outputPlaceholders = output.filter(elementIsAdPlaceholder);
159+
160+
expect(outputPlaceholders.length).toEqual(1);
161+
104162
const placeholderIndices = output.flatMap((el, idx) =>
105163
elementIsAdPlaceholder(el) ? [idx] : [],
106164
);
107165

108-
it(`should insert ${expectedPlaceholders} ad placeholder(s)`, () => {
109-
expect(placeholderIndices.length).toEqual(expectedPlaceholders);
110-
});
166+
// Expect one placeholder to be present after the fourth element only
167+
expect(placeholderIndices).toEqual([4]);
168+
});
111169

112-
if (expectedPlaceholders > 0) {
113-
it(`should insert ad placeholder(s) in the expected position(s): ${expectedPositions.join(
114-
',',
115-
)}`, () => {
116-
expect(placeholderIndices).toEqual(expectedPositions);
117-
});
118-
}
119-
},
120-
);
170+
it('should not insert an ad placeholder after a thumbnail image element', () => {
171+
const threeParagraphs = getTestParagraphElements(3);
121172

122-
it('should not insert an ad placeholder before an inline image element, but can insert it after the image', () => {
123-
const threeParagraphs = getTestParagraphElements(3);
173+
const elements = [
174+
...threeParagraphs,
175+
getThumbnailImageElement(),
176+
...threeParagraphs,
177+
];
124178

125-
const elements = [
126-
...threeParagraphs,
127-
getInlineImageElement(),
128-
...threeParagraphs,
129-
];
179+
const input: FEElement[] = elements;
130180

131-
const input: FEElement[] = elements;
181+
const output = enhanceAdPlaceholders(
182+
exampleFormat,
183+
'Apps',
184+
false,
185+
)(input);
186+
const outputPlaceholders = output.filter(elementIsAdPlaceholder);
132187

133-
const output = enhanceAdPlaceholders(
134-
exampleFormat,
135-
'Apps',
136-
false,
137-
)(input);
138-
const outputPlaceholders = output.filter(elementIsAdPlaceholder);
188+
expect(outputPlaceholders.length).toEqual(1);
139189

140-
expect(outputPlaceholders.length).toEqual(1);
190+
const placeholderIndices = output.flatMap((el, idx) =>
191+
elementIsAdPlaceholder(el) ? [idx] : [],
192+
);
141193

142-
const placeholderIndices = output.flatMap((el, idx) =>
143-
elementIsAdPlaceholder(el) ? [idx] : [],
144-
);
194+
// Expect one placeholder to be present after the fifth element only
195+
expect(placeholderIndices).toEqual([5]);
196+
});
145197

146-
// Expect one placeholder to be present after the fourth element only
147-
expect(placeholderIndices).toEqual([4]);
148-
});
198+
it('should not insert an ad placeholder after an element which is not an image or text', () => {
199+
const threeParagraphs = getTestParagraphElements(3);
149200

150-
it('should not insert an ad placeholder after a thumbnail image element', () => {
151-
const threeParagraphs = getTestParagraphElements(3);
201+
const elements = [
202+
...threeParagraphs,
203+
getSubheadingElement(),
204+
...threeParagraphs,
205+
];
152206

153-
const elements = [
154-
...threeParagraphs,
155-
getThumbnailImageElement(),
156-
...threeParagraphs,
157-
];
207+
const input: FEElement[] = elements;
158208

159-
const input: FEElement[] = elements;
209+
const output = enhanceAdPlaceholders(
210+
exampleFormat,
211+
'Apps',
212+
false,
213+
)(input);
214+
const outputPlaceholders = output.filter(elementIsAdPlaceholder);
160215

161-
const output = enhanceAdPlaceholders(
162-
exampleFormat,
163-
'Apps',
164-
false,
165-
)(input);
166-
const outputPlaceholders = output.filter(elementIsAdPlaceholder);
216+
expect(outputPlaceholders.length).toEqual(1);
167217

168-
expect(outputPlaceholders.length).toEqual(1);
218+
const placeholderIndices = output.flatMap((el, idx) =>
219+
elementIsAdPlaceholder(el) ? [idx] : [],
220+
);
169221

170-
const placeholderIndices = output.flatMap((el, idx) =>
171-
elementIsAdPlaceholder(el) ? [idx] : [],
172-
);
222+
// Expect one placeholder to be present after the fifth element only
223+
expect(placeholderIndices).toEqual([5]);
224+
});
173225

174-
// Expect one placeholder to be present after the fifth element only
175-
expect(placeholderIndices).toEqual([5]);
176-
});
226+
it('should not insert ad placeholders if shouldHideAds is true', () => {
227+
const input: FEElement[] = getTestParagraphElements(6);
177228

178-
it('should not insert an ad placeholder after an element which is not an image or text', () => {
179-
const threeParagraphs = getTestParagraphElements(3);
229+
const output = enhanceAdPlaceholders(
230+
exampleFormat,
231+
'Apps',
232+
true,
233+
)(input);
234+
const outputPlaceholders = output.filter(elementIsAdPlaceholder);
180235

181-
const elements = [
182-
...threeParagraphs,
183-
getSubheadingElement(),
184-
...threeParagraphs,
185-
];
236+
expect(outputPlaceholders.length).toEqual(0);
237+
});
238+
});
186239

187-
const input: FEElement[] = elements;
240+
describe('for gallery articles', () => {
241+
const testCases = [
242+
{ images: 0, expectedPositions: [] },
243+
{ images: 1, expectedPositions: [] },
244+
{ images: 4, expectedPositions: [4] },
245+
{ images: 6, expectedPositions: [4] },
246+
{ images: 9, expectedPositions: [4, 9] },
247+
{ images: 16, expectedPositions: [4, 9, 14, 19] },
248+
{
249+
images: 87,
250+
expectedPositions: [
251+
4, 9, 14, 19, 24, 29, 34, 39, 44, 49, 54, 59, 64, 69, 74,
252+
79, 84, 89, 94, 99, 104,
253+
],
254+
},
255+
] satisfies Array<{ images: number; expectedPositions: number[] }>;
256+
257+
describe.each(testCases)(
258+
'for $images images(s) in a gallery article',
259+
({ images, expectedPositions }) => {
260+
const elements = getTestImageBlockElements(images);
261+
const expectedPlaceholders = expectedPositions.length;
262+
const input: FEElement[] = elements;
263+
264+
const output = enhanceAdPlaceholders(
265+
galleryFormat,
266+
'Apps',
267+
false,
268+
)(input);
269+
const placeholderIndices = output.flatMap((el, idx) =>
270+
elementIsAdPlaceholder(el) ? [idx] : [],
271+
);
272+
273+
it(`should insert ${expectedPlaceholders} ad placeholder(s)`, () => {
274+
expect(placeholderIndices.length).toEqual(
275+
expectedPlaceholders,
276+
);
277+
});
188278

189-
const output = enhanceAdPlaceholders(
190-
exampleFormat,
191-
'Apps',
192-
false,
193-
)(input);
194-
const outputPlaceholders = output.filter(elementIsAdPlaceholder);
279+
if (expectedPlaceholders > 0) {
280+
it(`should insert ad placeholder(s) in the expected position(s): ${expectedPositions.join(
281+
',',
282+
)}`, () => {
283+
expect(placeholderIndices).toEqual(expectedPositions);
284+
});
285+
}
286+
},
287+
);
195288

196-
expect(outputPlaceholders.length).toEqual(1);
289+
it('should not insert ad placeholders if shouldHideAds is true', () => {
290+
const input: FEElement[] = getTestParagraphElements(6);
197291

198-
const placeholderIndices = output.flatMap((el, idx) =>
199-
elementIsAdPlaceholder(el) ? [idx] : [],
200-
);
292+
const output = enhanceAdPlaceholders(
293+
galleryFormat,
294+
'Apps',
295+
true,
296+
)(input);
297+
const outputPlaceholders = output.filter(elementIsAdPlaceholder);
201298

202-
// Expect one placeholder to be present after the fifth element only
203-
expect(placeholderIndices).toEqual([5]);
204-
});
299+
expect(outputPlaceholders.length).toEqual(0);
300+
});
205301

206-
it('should not insert ad placeholders if shouldHideAds is true', () => {
207-
const input: FEElement[] = getTestParagraphElements(6);
302+
it('should still insert ad placeholders if renderingTarget is web', () => {
303+
const input: FEElement[] = getTestParagraphElements(6);
208304

209-
const output = enhanceAdPlaceholders(
210-
exampleFormat,
211-
'Apps',
212-
true,
213-
)(input);
214-
const outputPlaceholders = output.filter(elementIsAdPlaceholder);
305+
const output = enhanceAdPlaceholders(
306+
galleryFormat,
307+
'Web',
308+
false,
309+
)(input);
310+
const outputPlaceholders = output.filter(elementIsAdPlaceholder);
215311

216-
expect(outputPlaceholders.length).toEqual(0);
312+
expect(outputPlaceholders.length).toBeGreaterThan(0);
313+
});
217314
});
218315
});

0 commit comments

Comments
 (0)