-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'latest' into upgrade-express
- Loading branch information
Showing
10 changed files
with
503 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import React from 'react'; | ||
import { render, waitFor } from '../react-testing-library-with-providers'; | ||
import AmpExperiment from './index'; | ||
|
||
const experimentConfig = { | ||
someExperiment: { | ||
variants: { | ||
control: 33, | ||
variant_1: 33, | ||
variant_2: 33, | ||
}, | ||
}, | ||
}; | ||
|
||
const multipleExperimentConfig = { | ||
aExperiment: { | ||
variants: { | ||
control: 33, | ||
variant_1: 33, | ||
variant_2: 33, | ||
}, | ||
}, | ||
bExperiment: { | ||
variants: { | ||
control: 33, | ||
variant_1: 33, | ||
variant_2: 33, | ||
}, | ||
}, | ||
}; | ||
|
||
describe('Amp experiment container on Amp pages', () => { | ||
it('should render an amp-experiment with the expected config', async () => { | ||
const { container } = render( | ||
<AmpExperiment experimentConfig={experimentConfig} />, | ||
); | ||
expect(container.querySelector('amp-experiment')).toBeInTheDocument(); | ||
expect(container).toMatchInlineSnapshot(` | ||
<div> | ||
<amp-experiment> | ||
<script | ||
type="application/json" | ||
> | ||
{"someExperiment":{"variants":{"control":33,"variant_1":33,"variant_2":33}}} | ||
</script> | ||
</amp-experiment> | ||
</div> | ||
`); | ||
}); | ||
|
||
it('should render an amp-experiment with the expected config when multiple experiments are running at the same time', async () => { | ||
const { container } = render( | ||
<AmpExperiment experimentConfig={multipleExperimentConfig} />, | ||
); | ||
expect(container.querySelector('amp-experiment')).toBeInTheDocument(); | ||
expect(container).toMatchInlineSnapshot(` | ||
<div> | ||
<amp-experiment> | ||
<script | ||
type="application/json" | ||
> | ||
{"aExperiment":{"variants":{"control":33,"variant_1":33,"variant_2":33}},"bExperiment":{"variants":{"control":33,"variant_1":33,"variant_2":33}}} | ||
</script> | ||
</amp-experiment> | ||
</div> | ||
`); | ||
}); | ||
|
||
it(`should add amp-experiment extension script to page head`, async () => { | ||
render(<AmpExperiment experimentConfig={experimentConfig} />); | ||
|
||
await waitFor(() => { | ||
const scripts = Array.from(document.querySelectorAll('head script')); | ||
|
||
expect(scripts).toEqual( | ||
expect.arrayContaining([ | ||
expect.objectContaining({ | ||
src: `https://cdn.ampproject.org/v0/amp-experiment-0.1.js`, | ||
}), | ||
]), | ||
); | ||
|
||
expect(scripts).toHaveLength(1); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** @jsx jsx */ | ||
/* @jsxFrag React.Fragment */ | ||
import { jsx } from '@emotion/react'; | ||
import React from 'react'; | ||
import { Helmet } from 'react-helmet'; | ||
|
||
type Variant = string; | ||
type Experiment = string; | ||
type TrafficAllocationPercentage = number; | ||
|
||
type AmpExperimentConfig = { | ||
[key: Experiment]: { | ||
sticky?: boolean; | ||
consentNotificationId?: string; | ||
variants: { | ||
[key: Variant]: TrafficAllocationPercentage; | ||
}; | ||
}; | ||
}; | ||
|
||
type AmpExperimentProps = { | ||
[key: Experiment]: AmpExperimentConfig; | ||
}; | ||
|
||
const AmpHead = () => ( | ||
<Helmet> | ||
<script | ||
async | ||
custom-element="amp-experiment" | ||
src="https://cdn.ampproject.org/v0/amp-experiment-0.1.js" | ||
/> | ||
</Helmet> | ||
); | ||
|
||
const AmpExperiment = ({ experimentConfig }: AmpExperimentProps) => { | ||
return ( | ||
<> | ||
<AmpHead /> | ||
<amp-experiment> | ||
<script | ||
type="application/json" | ||
/* eslint-disable-next-line react/no-danger */ | ||
dangerouslySetInnerHTML={{ __html: JSON.stringify(experimentConfig) }} | ||
/> | ||
</amp-experiment> | ||
</> | ||
); | ||
}; | ||
|
||
export default AmpExperiment; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
declare namespace JSX { | ||
interface IntrinsicElements { | ||
'amp-experiment': React.PropsWithChildren< | ||
ScriptHTMLAttributes<HTMLScriptElement> | ||
>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/app/pages/ArticlePage/experimentTopStories/helpers.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { getExperimentTopStories } from './helpers'; | ||
import { topStoriesList } from '../PagePromoSections/TopStoriesSection/fixture/index'; | ||
|
||
describe('AMP top stories experiment', () => { | ||
const mockTextBlock = { | ||
type: 'text', | ||
model: { | ||
blocks: [], | ||
}, | ||
}; | ||
const expectedExperimentTopStoriesBlock = (index: number) => { | ||
return { | ||
type: 'experimentTopStories', | ||
model: topStoriesList, | ||
id: `experimentTopStories-${index}`, | ||
}; | ||
}; | ||
|
||
const blocksShortLength = [mockTextBlock]; | ||
|
||
const blocksEvenLength = [ | ||
mockTextBlock, | ||
mockTextBlock, | ||
mockTextBlock, | ||
mockTextBlock, | ||
]; | ||
const blocksOddLength = [mockTextBlock, mockTextBlock, mockTextBlock]; | ||
|
||
describe('getExperimentTopStories()', () => { | ||
it('returns shouldEnableExperimentTopStories as true if props match conditions.', () => { | ||
const { shouldEnableExperimentTopStories } = getExperimentTopStories({ | ||
blocks: blocksEvenLength, | ||
topStoriesContent: topStoriesList, | ||
isAmp: true, | ||
id: 'c6v11qzyv8po', | ||
service: 'news', | ||
}); | ||
expect(shouldEnableExperimentTopStories).toBe(true); | ||
}); | ||
|
||
it.each` | ||
testDescription | isAmp | id | service | ||
${'all props are undefined'} | ${false} | ${undefined} | ${undefined} | ||
${'only isAmp is true'} | ${true} | ${undefined} | ${undefined} | ||
${'only pathname is undefined'} | ${true} | ${undefined} | ${'news'} | ||
${'only pathname is defined and valid'} | ${false} | ${'c6v11qzyv8po'} | ${undefined} | ||
${'all props defined but pathname is invalid'} | ${false} | ${'c1231qzyv8po'} | ${undefined} | ||
${'only service is undefined'} | ${true} | ${'c6v11qzyv8po'} | ${undefined} | ||
${'only service is defined and valid'} | ${false} | ${undefined} | ${'news'} | ||
${'all props defined but service is invalid'} | ${true} | ${'c6v11qzyv8po'} | ${'igbo'} | ||
`( | ||
'returns shouldEnableExperimentTopStories as false because $testDescription.', | ||
({ isAmp, id, service }) => { | ||
const { shouldEnableExperimentTopStories } = getExperimentTopStories({ | ||
blocks: blocksEvenLength, | ||
topStoriesContent: topStoriesList, | ||
isAmp, | ||
id, | ||
service, | ||
}); | ||
|
||
expect(shouldEnableExperimentTopStories).toBe(false); | ||
}, | ||
); | ||
|
||
const expectedBlocksEvenLength = [ | ||
mockTextBlock, | ||
mockTextBlock, | ||
expectedExperimentTopStoriesBlock(2), | ||
mockTextBlock, | ||
mockTextBlock, | ||
]; | ||
const expectedBlocksOddLength = [ | ||
mockTextBlock, | ||
expectedExperimentTopStoriesBlock(1), | ||
mockTextBlock, | ||
mockTextBlock, | ||
]; | ||
|
||
it.each` | ||
testType | inputBlocks | expectedOutput | ||
${'even'} | ${blocksEvenLength} | ${expectedBlocksEvenLength} | ||
${'odd'} | ${blocksOddLength} | ${expectedBlocksOddLength} | ||
`( | ||
'should insert experimentTopStories block into blocks array in the correct position when blocks.length is $testType', | ||
({ inputBlocks, expectedOutput }) => { | ||
const { transformedBlocks } = getExperimentTopStories({ | ||
blocks: inputBlocks, | ||
topStoriesContent: topStoriesList, | ||
isAmp: true, | ||
id: 'c6v11qzyv8po', | ||
service: 'news', | ||
}); | ||
expect(transformedBlocks).toEqual(expectedOutput); | ||
}, | ||
); | ||
|
||
it('does not insert experiment top stories blocks if the blocks array length is < 2.', () => { | ||
const { transformedBlocks } = getExperimentTopStories({ | ||
blocks: blocksShortLength, | ||
topStoriesContent: topStoriesList, | ||
isAmp: true, | ||
id: 'c6v11qzyv8po', | ||
service: 'news', | ||
}); | ||
expect(transformedBlocks).toBe(blocksShortLength); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.