Skip to content

Commit

Permalink
add getFirstRenderExperiments (#206)
Browse files Browse the repository at this point in the history
Co-authored-by: Nate Bierdeman <[email protected]>
  • Loading branch information
nikrom17 and nbierdeman authored Jun 4, 2024
1 parent 48718ca commit b14f905
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ module.exports = {

// Experiment Variable
__DISABLE_SET_COOKIE__: true,
// TODO first-render-experiment-cleanup
__EXPERIMENTATION__: true,
__FIRST_RENDER_EXPERIMENTS__: true,
},
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 6 additions & 1 deletion src/declarations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof PROTOCOL>;
declare var __HOST__: string;
Expand Down Expand Up @@ -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;
18 changes: 13 additions & 5 deletions src/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -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__;
Expand Down Expand Up @@ -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 {};
}
35 changes: 32 additions & 3 deletions src/global.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
getCorrelationID,
getPlatform,
getExperimentation,
getFirstRenderExperiments,
} from "./global";

describe(`globals cases`, () => {
Expand Down Expand Up @@ -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",
Expand All @@ -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);
});
});
6 changes: 6 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -13,6 +14,7 @@ export type Experimentation = {
[any]: empty,
};

// TODO first-render-experiment-cleanup
export type GetExperimentation = {
__EXPERIENCE__?: string,
__TREATMENT__?: string,
Expand All @@ -21,3 +23,7 @@ export type GetExperimentation = {
// eslint-disable-next-line flowtype/no-weak-types
[any]: empty,
};

export type FirstRenderExperiments = {
[key: string]: boolean,
};
4 changes: 4 additions & 0 deletions test/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

0 comments on commit b14f905

Please sign in to comment.