Skip to content

Commit

Permalink
[DTPPSDK-1319]Add getExperimentation() (#163)
Browse files Browse the repository at this point in the history
* pass experimentation to url params

* change the param format for experimentation params

* modify getExperimentation function

* use const

* add a test for getExperimentation()

* add null test

* Add empty value test

* add types

* update Experimentation type

* Revert "update Experimentation type"

This reverts commit e5b1c42.
  • Loading branch information
xuanwan05 authored Aug 9, 2023
1 parent 048b2c7 commit 0d1f467
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 0 deletions.
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") {
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)}`
);
}
});
});
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",
},
};

0 comments on commit 0d1f467

Please sign in to comment.