Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DTPPSDK-1319]Add getExperimentation() #163

Merged
merged 10 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ module.exports = {

// Experiment Variable
__DISABLE_SET_COOKIE__: true,
__EXPERIMENTATION__: true,
},
};
4 changes: 4 additions & 0 deletions src/declarations.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ declare var __FUNDING_ELIGIBILITY__: FundingEligibilityType;

// Experiment Variable
declare var __DISABLE_SET_COOKIE__: boolean;
declare var __EXPERIMENTATION__: {|
__EXPERIENCE__?: string,
__TREATMENT__?: string,
|};
19 changes: 19 additions & 0 deletions src/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
} from "@paypal/sdk-constants/src";
import { isDevice } from "@krakenjs/belter/src";

import type { Experimentation } from "./types";

export function getSDKHost(): string {
return __SDK_HOST__;
}
Expand Down Expand Up @@ -119,3 +121,20 @@ export function getDisableSetCookie(): boolean {

return false;
}

export function getExperimentation(): Experimentation | null {
if (typeof __EXPERIMENTATION__ !== "undefined") {
ravishekhar marked this conversation as resolved.
Show resolved Hide resolved
ravishekhar marked this conversation as resolved.
Show resolved Hide resolved
if (__EXPERIMENTATION__) {
const experimation: Experimentation = {};
if (__EXPERIMENTATION__.__EXPERIENCE__) {
experimation.experience = __EXPERIMENTATION__.__EXPERIENCE__;
}
if (__EXPERIMENTATION__.__TREATMENT__) {
experimation.treatment = __EXPERIMENTATION__.__TREATMENT__;
}
return experimation;
}
}

return null;
}
18 changes: 18 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,21 @@
export const _TYPES = true;

export * from "@paypal/sdk-constants/src/types";

export type Experimentation = {
experience?: string,
treatment?: string,
// Workaround for exact type. Works as expected
// when dealing with objects that can be empty.
// eslint-disable-next-line flowtype/no-weak-types
[any]: empty,
};

export type GetExperimentation = {
__EXPERIENCE__?: string,
__TREATMENT__?: string,
// Workaround for exact type. Works as expected
// when dealing with objects that can be empty.
// eslint-disable-next-line flowtype/no-weak-types
[any]: empty,
};
49 changes: 49 additions & 0 deletions test/client/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
getVersion,
getCorrelationID,
getPlatform,
getExperimentation,
} from "../../src";

describe(`globals cases`, () => {
Expand Down Expand Up @@ -307,4 +308,52 @@ describe(`globals cases`, () => {
);
}
});

it("should successfully get experimation value", () => {
window.__EXPERIMENTATION__ = {
__EXPERIENCE__: "1234, 4321",
__TREATMENT__: "8765,7890",
};
const expectedResult = {
experience: "1234, 4321",
treatment: "8765,7890",
};
const result = getExperimentation();

if (JSON.stringify(result) !== JSON.stringify(expectedResult)) {
throw new Error(
`Expected experimation to be ${JSON.stringify(
expectedResult
)}, got ${JSON.stringify(result)}`
);
}
});

it("should get experimation null value", () => {
window.__EXPERIMENTATION__ = null;
const expectedResult = null;
const result = getExperimentation();

if (result !== expectedResult) {
throw new Error(
`Expected experimation to be ${String(expectedResult)}, got ${String(
result
)}`
);
}
});

it("should get experimation empty value", () => {
window.__EXPERIMENTATION__ = {};
const expectedResult = {};
const result = getExperimentation();

if (JSON.stringify(result) !== JSON.stringify(expectedResult)) {
throw new Error(
`Expected experimation to be ${JSON.stringify(
expectedResult
)}, got ${JSON.stringify(result)}`
);
}
});
spencersablan marked this conversation as resolved.
Show resolved Hide resolved
});
6 changes: 6 additions & 0 deletions test/globals.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* @flow */
import type { GetExperimentation } from "../src/types";

type TestGlobals = {|
[string]: string | number | boolean | (() => string | (() => number)),
__COMPONENTS__: $ReadOnlyArray<string>,
__EXPERIMENTATION__: GetExperimentation,
|};

export const sdkClientTestGlobals: TestGlobals = {
Expand All @@ -20,4 +22,8 @@ export const sdkClientTestGlobals: TestGlobals = {
__PAYPAL_API_DOMAIN__: "mock://sandbox.paypal.com",
__COMPONENTS__: ["buttons"],
__DISABLE_SET_COOKIE__: true,
__EXPERIMENTATION__: {
__EXPERIENCE__: "1122",
__TREATMENT__: "1234",
},
};