Skip to content

Commit

Permalink
feat: runtime functionality for easier development testing (#972)
Browse files Browse the repository at this point in the history
  • Loading branch information
perco12 authored Aug 28, 2023
1 parent 2e343e9 commit 0bc86fe
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
},
"globals": {
"__MESSAGES__": true,
"__MESSAGING_GLOBALS__": true
"__MESSAGING_GLOBALS__": true,
"__ENV__": true
},
"plugins": ["prettier"],
"rules": {
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<script>window.__TEST_ENV__ = "https://www.te-test-env.com"</script>
```

## Releasing

This package is published weekly, **Every Wednesday**. Please [view our Changelog](CHANGELOG.md) to stay updated with bug fixes and new features.
9 changes: 5 additions & 4 deletions src/utils/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
69 changes: 69 additions & 0 deletions tests/unit/spec/src/utils/sdk.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 0bc86fe

Please sign in to comment.