From 7b7cec8e272110e1ace97d7e7f11f4c087036b9e Mon Sep 17 00:00:00 2001 From: Ravi <7014230+arelra@users.noreply.github.com> Date: Thu, 11 Jan 2024 17:32:04 +0000 Subject: [PATCH] Remove cypress-switches --- .../src/components/SetABTests.importable.tsx | 12 ++-------- .../src/experiments/cypress-switches.ts | 24 ------------------- .../src/model/enhance-switches.ts | 13 ++++++---- 3 files changed, 11 insertions(+), 38 deletions(-) delete mode 100644 dotcom-rendering/src/experiments/cypress-switches.ts diff --git a/dotcom-rendering/src/components/SetABTests.importable.tsx b/dotcom-rendering/src/components/SetABTests.importable.tsx index 9cf066b42ef..7de6dc7f401 100644 --- a/dotcom-rendering/src/components/SetABTests.importable.tsx +++ b/dotcom-rendering/src/components/SetABTests.importable.tsx @@ -1,10 +1,9 @@ -import { AB } from '@guardian/ab-core'; import type { CoreAPIConfig } from '@guardian/ab-core'; +import { AB } from '@guardian/ab-core'; import { getCookie, log } from '@guardian/libs'; import { useEffect, useState } from 'react'; import { getOphan } from '../client/ophan/ophan'; import { tests } from '../experiments/ab-tests'; -import { getCypressSwitches } from '../experiments/cypress-switches'; import { runnableTestsToParticipations } from '../experiments/lib/ab-participations'; import { getForcedParticipationsFromUrl } from '../lib/getAbUrlHash'; import { setABTests } from '../lib/useAB'; @@ -67,10 +66,6 @@ export const SetABTests = ({ ); } - // Get the forced switches to use for when running within cypress - // Is empty object if not in cypress - const cypressAbSwitches = getCypressSwitches(); - const allForcedTestVariants = { ...forcedTestVariants, ...getForcedParticipationsFromUrl(window.location.hash), @@ -79,10 +74,7 @@ export const SetABTests = ({ const ab = new AB({ mvtId, pageIsSensitive, - abTestSwitches: { - ...abTestSwitches, - ...cypressAbSwitches, // by adding cypress switches below CAPI, we can override any production switch in Cypress - }, + abTestSwitches, arrayOfTestObjects: tests, forcedTestVariants: allForcedTestVariants, ophanRecord: ophan.record, diff --git a/dotcom-rendering/src/experiments/cypress-switches.ts b/dotcom-rendering/src/experiments/cypress-switches.ts deleted file mode 100644 index 272febb489a..00000000000 --- a/dotcom-rendering/src/experiments/cypress-switches.ts +++ /dev/null @@ -1,24 +0,0 @@ -// Forced AB Test Switches for Cypress testing - -// AB test switches are defined in Frontend and required to be "ON" for Cypress E2E Tests to successfully run and pass. -// However, it is useful to be able to switch an AB test on/off in Frontend Admin UI without breaking tests that rely -// on those switches in another repo (DCR). - -// By adding the switches here, and the state we want them to be in when we run Cypress tests, we no longer have to -// worry about the state of the switch in Frontend. In turn allowing tests to pass even if a switch has changed state -// in Frontend, e.g. a switch has been set to "OFF". - -// Add switches to be a specific state in Cypress to this object -const cypressSwitches = { - abAbTestTest: true, // Test switch, used for Cypress integration test - abSignInGateMainControl: true, - abSignInGateMainVariant: true, -}; - -// Function to retrieve the switches if running in Cypress -export const getCypressSwitches = (): Record => { - // If running within cypress, return the forced switches - if (window.Cypress) return cypressSwitches; - // Otherwise just return empty object - return {}; -}; diff --git a/dotcom-rendering/src/model/enhance-switches.ts b/dotcom-rendering/src/model/enhance-switches.ts index c659099b25f..8b409146058 100644 --- a/dotcom-rendering/src/model/enhance-switches.ts +++ b/dotcom-rendering/src/model/enhance-switches.ts @@ -1,6 +1,13 @@ import type { Switches } from '../types/config'; -export type ABTestSwitches = { [key: `ab${string}`]: boolean | undefined }; +export type ABTestSwitches = { [key: `ab${string}`]: boolean }; + +const isValidEntry = ( + entry: [string, boolean | undefined], +): entry is [string, boolean] => { + const [key, value] = entry; + return key.startsWith('ab') && typeof value === 'boolean'; +}; /** * Switches that start with "ab" are used for AB tests. @@ -18,6 +25,4 @@ export type ABTestSwitches = { [key: `ab${string}`]: boolean | undefined }; * @returns {ABTestSwitches} an object containing only AB switches */ export const filterABTestSwitches = (switches: Switches): ABTestSwitches => - Object.fromEntries( - Object.entries(switches).filter(([key]) => key.startsWith('ab')), - ); + Object.fromEntries(Object.entries(switches).filter(isValidEntry));