diff --git a/src/app/components/AmpExperiment/index.test.tsx b/src/app/components/AmpExperiment/index.test.tsx new file mode 100644 index 00000000000..bd54ba91ec0 --- /dev/null +++ b/src/app/components/AmpExperiment/index.test.tsx @@ -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( + , + ); + expect(container.querySelector('amp-experiment')).toBeInTheDocument(); + expect(container).toMatchInlineSnapshot(` +
+ + + +
+ `); + }); + + it('should render an amp-experiment with the expected config when multiple experiments are running at the same time', async () => { + const { container } = render( + , + ); + expect(container.querySelector('amp-experiment')).toBeInTheDocument(); + expect(container).toMatchInlineSnapshot(` +
+ + + +
+ `); + }); + + it(`should add amp-experiment extension script to page head`, async () => { + render(); + + 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); + }); + }); +}); diff --git a/src/app/components/AmpExperiment/index.tsx b/src/app/components/AmpExperiment/index.tsx new file mode 100644 index 00000000000..54e7d615c95 --- /dev/null +++ b/src/app/components/AmpExperiment/index.tsx @@ -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 = () => ( + +