diff --git a/.eslintrc.js b/.eslintrc.js index 0916ba0e..5d08a5dc 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -24,6 +24,8 @@ module.exports = { // Experiment Variable __DISABLE_SET_COOKIE__: true, + // TODO first-render-experiment-cleanup __EXPERIMENTATION__: true, + __FIRST_RENDER_EXPERIMENTS__: true, }, }; diff --git a/package.json b/package.json index 03a0ec33..e445660c 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "babel": "babel ./server --ignore=node_modules --out-dir ./server", - "build": "npm run test && npm run doc", + "build": "npm run test && npm run doc & npm run babel", "clean": "rimraf dist coverage", "debug": "cross-env NODE_ENV=debug", "doc": "esdoc", diff --git a/src/declarations.js b/src/declarations.js index d8f1e1d6..c5bdfd2f 100644 --- a/src/declarations.js +++ b/src/declarations.js @@ -4,7 +4,7 @@ import { ENV, COMPONENTS, PROTOCOL } from "@paypal/sdk-constants/src"; // $FlowFixMe[toplevel-library-import] -import type { FundingEligibilityType } from "./types"; +import type { FundingEligibilityType, FirstRenderExperiments } from "./types"; declare var __PROTOCOL__: $Values; declare var __HOST__: string; @@ -33,7 +33,12 @@ declare var __FUNDING_ELIGIBILITY__: FundingEligibilityType; // Experiment Variable declare var __DISABLE_SET_COOKIE__: boolean; + +// TODO first-render-experiment-cleanup declare var __EXPERIMENTATION__: {| __EXPERIENCE__?: string, __TREATMENT__?: string, |}; + +// $FlowIgnore[value-as-type] +declare var __FIRST_RENDER_EXPERIMENTS__: FirstRenderExperiments; diff --git a/src/global.js b/src/global.js index 6cc19408..9f9e5a00 100644 --- a/src/global.js +++ b/src/global.js @@ -9,7 +9,7 @@ import { } from "@paypal/sdk-constants/src"; import { isDevice } from "@krakenjs/belter/src"; -import type { Experimentation } from "./types"; +import type { Experimentation, FirstRenderExperiments } from "./types"; export function getSDKHost(): string { return __SDK_HOST__; @@ -122,19 +122,27 @@ export function getDisableSetCookie(): boolean { return false; } +// TODO first-render-experiment-cleanup any thing using this function should use getFirstRenderExperiments export function getExperimentation(): Experimentation | null { if (typeof __EXPERIMENTATION__ !== "undefined") { if (__EXPERIMENTATION__) { - const experimation: Experimentation = {}; + const experimentation: Experimentation = {}; if (__EXPERIMENTATION__.__EXPERIENCE__) { - experimation.experience = __EXPERIMENTATION__.__EXPERIENCE__; + experimentation.experience = __EXPERIMENTATION__.__EXPERIENCE__; } if (__EXPERIMENTATION__.__TREATMENT__) { - experimation.treatment = __EXPERIMENTATION__.__TREATMENT__; + experimentation.treatment = __EXPERIMENTATION__.__TREATMENT__; } - return experimation; + return experimentation; } } return null; } + +export function getFirstRenderExperiments(): FirstRenderExperiments | null { + if (typeof __FIRST_RENDER_EXPERIMENTS__ !== "undefined") { + return __FIRST_RENDER_EXPERIMENTS__; + } + return {}; +} diff --git a/src/global.test.js b/src/global.test.js index fb88703a..28ac040e 100644 --- a/src/global.test.js +++ b/src/global.test.js @@ -22,6 +22,7 @@ import { getCorrelationID, getPlatform, getExperimentation, + getFirstRenderExperiments, } from "./global"; describe(`globals cases`, () => { @@ -173,7 +174,8 @@ describe(`globals cases`, () => { expect(result).toEqual(window.__FUNDING_ELIGIBILITY__); }); - it("should successfully get experimation value", () => { + // TODO first-render-experiment-cleanup + it("should successfully get experimentation value", () => { window.__EXPERIMENTATION__ = { __EXPERIENCE__: "1234, 4321", __TREATMENT__: "8765,7890", @@ -186,17 +188,44 @@ describe(`globals cases`, () => { expect(result).toEqual(expectedResult); }); - it("should get experimation null value", () => { + // TODO first-render-experiment-cleanup + it("should get experimentation null value", () => { window.__EXPERIMENTATION__ = null; const expectedResult = null; const result = getExperimentation(); expect(result).toEqual(expectedResult); }); - it("should get experimation empty value", () => { + // TODO first-render-experiment-cleanup + it("should get experimentation empty value", () => { window.__EXPERIMENTATION__ = {}; const expectedResult = {}; const result = getExperimentation(); expect(result).toEqual(expectedResult); }); + + it("should successfully get first render experiment value", () => { + window.__FIRST_RENDER_EXPERIMENTS__ = { + firstRenderExperiment: true, + }; + const expectedResult = { + firstRenderExperiment: true, + }; + const result = getFirstRenderExperiments(); + expect(result).toEqual(expectedResult); + }); + + it("should get first render experiment null value", () => { + window.__FIRST_RENDER_EXPERIMENTS__ = null; + const expectedResult = null; + const result = getFirstRenderExperiments(); + expect(result).toEqual(expectedResult); + }); + + it("should get first render experiment empty value", () => { + window.__FIRST_RENDER_EXPERIMENTS__ = {}; + const expectedResult = {}; + const result = getFirstRenderExperiments(); + expect(result).toEqual(expectedResult); + }); }); diff --git a/src/types.js b/src/types.js index 5d86187d..ff4f7627 100644 --- a/src/types.js +++ b/src/types.js @@ -4,6 +4,7 @@ export const _TYPES = true; export * from "@paypal/sdk-constants/src/types"; +// TODO first-render-experiment-cleanup export type Experimentation = { experience?: string, treatment?: string, @@ -13,6 +14,7 @@ export type Experimentation = { [any]: empty, }; +// TODO first-render-experiment-cleanup export type GetExperimentation = { __EXPERIENCE__?: string, __TREATMENT__?: string, @@ -21,3 +23,7 @@ export type GetExperimentation = { // eslint-disable-next-line flowtype/no-weak-types [any]: empty, }; + +export type FirstRenderExperiments = { + [key: string]: boolean, +}; diff --git a/test/globals.js b/test/globals.js index 318515f9..b78fc97b 100644 --- a/test/globals.js +++ b/test/globals.js @@ -17,9 +17,13 @@ export const sdkClientTestGlobals = { __PAYPAL_API_DOMAIN__: "mock://sandbox.paypal.com", __COMPONENTS__: ["buttons"], __DISABLE_SET_COOKIE__: true, + // TODO first-render-experiment-cleanup __EXPERIMENTATION__: { __EXPERIENCE__: "1122", __TREATMENT__: "1234", }, + __FIRST_RENDER_EXPERIMENTS__: { + firstRenderExperiment: true, + }, crypto: crypto.webcrypto, };