Skip to content

Commit

Permalink
Merge pull request #3945 from guardian/oliver/withab
Browse files Browse the repository at this point in the history
`WithABProvider`
  • Loading branch information
oliverlloyd authored Feb 9, 2022
2 parents 96b2d6f + 4ff94ad commit 76f6e03
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
21 changes: 4 additions & 17 deletions dotcom-rendering/src/web/components/BootReact.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import ReactDOM from 'react-dom';

import { App } from '@root/src/web/components/App';
import { ABProvider } from '@guardian/ab-react';
import { tests } from '@frontend/web/experiments/ab-tests';
import { getForcedParticipationsFromUrl } from '@frontend/web/lib/getAbUrlHash';
import { getCypressSwitches } from '@frontend/web/experiments/cypress-switches';
import { loadableReady } from '@loadable/component';
import { getOphanRecordFunction } from '@root/src/web/browser/ophan/ophan';
import { getCookie } from '@guardian/libs';
import { WithABProvider } from './WithABProvider';

type Props = {
CAPI: CAPIBrowserType;
Expand All @@ -27,28 +25,17 @@ export const BootReact = ({ CAPI }: Props) => {

const ophanRecord = getOphanRecordFunction();

const windowHash = window && window.location && window.location.hash;

// Get the forced switches to use for when running within cypress
// Is empty object if not in cypress
const cypressAbSwitches = getCypressSwitches();

loadableReady(() => {
ReactDOM.render(
<ABProvider
<WithABProvider
arrayOfTestObjects={tests}
abTestSwitches={{
...CAPI.config.switches,
...cypressAbSwitches, // by adding cypress switches below CAPI, we can override any production switch in Cypress
}}
abTestSwitches={CAPI.config.switches}
pageIsSensitive={CAPI.config.isSensitive}
mvtMaxValue={1000000}
mvtId={mvtId}
ophanRecord={ophanRecord}
forcedTestVariants={getForcedParticipationsFromUrl(windowHash)}
>
<App CAPI={CAPI} ophanRecord={ophanRecord} />
</ABProvider>,
</WithABProvider>,

document.getElementById('react-root'),
);
Expand Down
42 changes: 42 additions & 0 deletions dotcom-rendering/src/web/components/WithABProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { CoreAPIConfig, OphanAPIConfig } from '@guardian/ab-core/dist/types';
import { ABProvider } from '@guardian/ab-react';
import { getCypressSwitches } from '../experiments/cypress-switches';
import { getForcedParticipationsFromUrl } from '../lib/getAbUrlHash';

type Props = {
arrayOfTestObjects: CoreAPIConfig['arrayOfTestObjects'];
abTestSwitches: CoreAPIConfig['abTestSwitches'];
pageIsSensitive: CoreAPIConfig['pageIsSensitive'];
mvtId: CoreAPIConfig['mvtId'];
ophanRecord: OphanAPIConfig['ophanRecord'];
children: JSX.Element;
};
export const WithABProvider = ({
arrayOfTestObjects,
abTestSwitches,
pageIsSensitive,
mvtId,
ophanRecord,
children,
}: Props) => {
const windowHash = window?.location.hash;
// Get the forced switches to use for when running within cypress
// Is empty object if not in cypress
const cypressAbSwitches = getCypressSwitches();
return (
<ABProvider
arrayOfTestObjects={arrayOfTestObjects}
abTestSwitches={{
...abTestSwitches,
...cypressAbSwitches, // by adding cypress switches below CAPI, we can override any production switch in Cypress
}}
pageIsSensitive={pageIsSensitive}
mvtMaxValue={1000000}
mvtId={mvtId}
ophanRecord={ophanRecord}
forcedTestVariants={getForcedParticipationsFromUrl(windowHash)}
>
{children}
</ABProvider>
);
};

0 comments on commit 76f6e03

Please sign in to comment.