From 0bc86fe8ece39e1a2b9978957ba6177d1fed075f Mon Sep 17 00:00:00 2001 From: perco12 <141269770+perco12@users.noreply.github.com> Date: Mon, 28 Aug 2023 11:03:27 -0500 Subject: [PATCH] feat: runtime functionality for easier development testing (#972) --- .eslintrc | 3 +- README.md | 9 ++++ src/utils/sdk.js | 9 ++-- tests/unit/spec/src/utils/sdk.test.js | 69 +++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 tests/unit/spec/src/utils/sdk.test.js diff --git a/.eslintrc b/.eslintrc index 5cea6ab477..9ba35ca26f 100644 --- a/.eslintrc +++ b/.eslintrc @@ -7,7 +7,8 @@ }, "globals": { "__MESSAGES__": true, - "__MESSAGING_GLOBALS__": true + "__MESSAGING_GLOBALS__": true, + "__ENV__": true }, "plugins": ["prettier"], "rules": { diff --git a/README.md b/README.md index 36dd9fef7e..45aad0d7bf 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,15 @@ Alternatively, you can remove `-- --testPathPattern {integrationType}` and just CONFIG_PATH={locale}/{account} npm run test:func:snapshots ``` +### Stage + +If you are looking to run against an alternative environment, set the `window.__TEST_ENV__` global to override the environment. **Please note, this is only available in development environments.** + +```javascript +//Change the value of the test environment + +``` + ## Releasing This package is published weekly, **Every Wednesday**. Please [view our Changelog](CHANGELOG.md) to stay updated with bug fixes and new features. diff --git a/src/utils/sdk.js b/src/utils/sdk.js index 7db94cb765..ba8257c1db 100644 --- a/src/utils/sdk.js +++ b/src/utils/sdk.js @@ -204,8 +204,9 @@ export const isScriptBeingDestroyed = () => { }; export function getPayPalDomain() { - if (__MESSAGES__.__TEST_ENV__) { - return __MESSAGES__.__TEST_ENV__; + const testEnviroment = window.__TEST_ENV__ ?? __MESSAGES__.__TEST_ENV__; + if (testEnviroment && getEnv() !== 'production' && getEnv() !== 'sandbox') { + return testEnviroment; } else if (__MESSAGES__.__TARGET__ === 'SDK') { return getSDKPayPalDomain(); } else { @@ -224,9 +225,9 @@ export function getStageTag() { return undefined; } } - export function getDevTouchpoint() { - if (__MESSAGES__.__DEV_TOUCHPOINT__ && getEnv() !== 'production' && getEnv() !== 'sandbox') { + const devTouchpoint = window.__DEV_TOUCHPOINT__ ?? __MESSAGES__.__DEV_TOUCHPOINT__; + if (devTouchpoint && getEnv() !== 'production' && getEnv() !== 'sandbox') { return true; } else { return undefined; // Prevent the zoid query param diff --git a/tests/unit/spec/src/utils/sdk.test.js b/tests/unit/spec/src/utils/sdk.test.js new file mode 100644 index 0000000000..9bb2cd035c --- /dev/null +++ b/tests/unit/spec/src/utils/sdk.test.js @@ -0,0 +1,69 @@ +import { getPayPalDomain, getDevTouchpoint } from '../../../../../src/utils/sdk'; + +describe('getPayPalDomain', () => { + beforeEach(() => { + // reset test variables + window.__TEST_ENV__ = undefined; + __ENV__ = 'stage'; + __MESSAGES__ = { + __TEST_ENV__: undefined, + __DOMAIN__: { __SANDBOX__: 'https://www.sandbox.com', __STAGE__: 'https://www.stage.com' } + }; + }); + test('returns message test environment if window test environment is not set', () => { + __MESSAGES__.__TEST_ENV__ = 'https://www.test-env.com'; + expect(getPayPalDomain()).toBe('https://www.test-env.com'); + }); + test('returns window test environment if it is set', () => { + __MESSAGES__.__TEST_ENV__ = 'https://www.test-env.com'; + window.__TEST_ENV__ = 'https://www.window-test-env.com'; + expect(getPayPalDomain()).toBe('https://www.window-test-env.com'); + }); + test('returns sandbox domain if no test environment is set and env is sandbox', () => { + __MESSAGES__.__TEST_ENV__ = 'https://www.test-env.com'; + window.__TEST_ENV__ = 'https://www.window-test-env.com'; + __ENV__ = 'sandbox'; + expect(getPayPalDomain()).toBe('https://www.sandbox.com'); + }); + test('returns stage domain if no test environment is set', () => { + expect(getPayPalDomain()).toBe('https://www.stage.com'); + }); +}); + +describe('getDevTouchpoint', () => { + beforeEach(() => { + // reset test variables + window.__DEV_TOUCHPOINT__ = undefined; + __ENV__ = 'stage'; + __MESSAGES__ = { + __DEV_TOUCHPOINT__: undefined + }; + }); + test('returns true if window devTouchpoint is set', () => { + window.__DEV_TOUCHPOINT__ = true; + expect(getDevTouchpoint()).toBe(true); + }); + test('returns undefined if window devTouchpoint is set to false', () => { + window.__DEV_TOUCHPOINT__ = false; + expect(getDevTouchpoint()).toBe(undefined); + }); + test('returns true if window devTouchpoint is not set and message devTouchpoint is true', () => { + __MESSAGES__.__DEV_TOUCHPOINT__ = true; + expect(getDevTouchpoint()).toBe(true); + }); + test('returns true if window devTouchpoint is set and message devTouchpoint is false ', () => { + window.__DEV_TOUCHPOINT__ = true; + __MESSAGES__.__DEV_TOUCHPOINT__ = false; + expect(getDevTouchpoint()).toBe(true); + }); + test('returns undefined if window devTouchpoint is set to false and message devTouchpoint is true ', () => { + window.__DEV_TOUCHPOINT__ = false; + __MESSAGES__.__DEV_TOUCHPOINT__ = true; + expect(getDevTouchpoint()).toBe(undefined); + }); + test('returns undefined if env is not stage', () => { + window.__DEV_TOUCHPOINT__ = true; + __ENV__ = 'sandbox'; + expect(getDevTouchpoint()).toBe(undefined); + }); +});