Skip to content

Commit

Permalink
refactor: remove stage references (#968)
Browse files Browse the repository at this point in the history
  • Loading branch information
yeul authored Sep 12, 2023
1 parent 7af68ff commit d3e1c52
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 50 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ CONFIG_PATH={locale}/{account} npm run test:func:snapshots

### Stage

#### For PayPal Internal Testing Only:

To test against PayPal's standard stage url, ensure that the demo page contains the following script, then run `npm run dev:stage`:

```javascript
//Change the value of the url and add the script to the demo page (i.e. standalone.html)
<script>window.__TEST_ENV__ = "https://www.{PAYPAL_STAGE_URL_HERE}.com"</script>
```

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
Expand Down
4 changes: 2 additions & 2 deletions globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ module.exports = (env = { TARGET: 'sdk' }) => ({
__DEV_TOUCHPOINT__: env.DEV_TOUCHPOINT,
__DOMAIN__: {
__LOCAL__: `https://localhost.paypal.com:${PORT}`,
__STAGE__: 'https://www.msmaster.qa.paypal.com',
__SANDBOX__: 'https://www.sandbox.paypal.com',
__PRODUCTION__: 'https://www.paypal.com'
},
__URI__: {
__MESSAGE__: '/credit-presentment/smart/message',
__TREATMENTS__: '/credit-presentment/experiments/local',
__MODAL__: '/credit-presentment/smart/modal',
__LOGGER__: '/credit-presentment/log'
__LOGGER__: '/credit-presentment/log',
__CREDIT_APPLY__: '/ppcreditapply/da/us'
}
}
});
16 changes: 1 addition & 15 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,5 @@ module.exports = {
},
setupFilesAfterEnv: ['<rootDir>/tests/unit/utils/setup.js', '<rootDir>/tests/customMatchers.js'],
// grumbler-scripts includes babel-plugin-istanbul
coveragePathIgnorePatterns: ['<rootDir>'],
globals: {
__ENV__: 'test',
__MESSAGES__: {
__VERSION__: '1.0.0',
__DOMAIN__: {
__TEST__: 'http://localhost.paypal.com:8080'
},
__URI__: {
__MESSAGE__: '/credit-presentment/smart/message',
__MODAL__: '/credit-presentment/smart/modal',
__LOGGER__: '/credit-presentment/log'
}
}
}
coveragePathIgnorePatterns: ['<rootDir>']
};
13 changes: 4 additions & 9 deletions src/components/modal/lib/hooks/applyNow.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import { useTransitionState, useXProps, useServerData } from '../providers';
import { getGlobalUrl } from '../../../../utils';

export default clickTitle => {
const { payerId } = useServerData();
const { onClick, refId, env = 'production' } = useXProps();
const { onClick, refId } = useXProps();

const [, handleClose] = useTransitionState();

const urlBase = {
local: 'msmaster.qa.paypal',
stage: 'msmaster.qa.paypal',
sandbox: 'sandbox.paypal',
production: 'paypal'
}[env];

return () => {
onClick({ linkName: clickTitle });
const urlBase = getGlobalUrl('CREDIT_APPLY');
// TODO: Get finalized query param keys
const win = window.open(
`https://www.${urlBase}.com/ppcreditapply/da/us?cats_id=DA_AD_UPSTREAM&actor=merchant&mktgrefid=${refId}&payer_id=${payerId}`
`${urlBase}?cats_id=DA_AD_UPSTREAM&actor=merchant&mktgrefid=${refId}&payer_id=${payerId}`
);
const intervalId = setInterval(() => {
if (win.closed) {
Expand Down
13 changes: 4 additions & 9 deletions src/components/modal/v2/lib/hooks/applyNow.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import { useTransitionState, useXProps, useServerData } from '../providers';
import { getGlobalUrl } from '../../../../../utils';

export default (clickTitle, src = 'link_click') => {
const { payerId } = useServerData();
const { onClick, refId, env = 'production' } = useXProps();
const { onClick, refId } = useXProps();

const [, handleClose] = useTransitionState();

const urlBase = {
local: 'msmaster.qa.paypal',
stage: 'msmaster.qa.paypal',
sandbox: 'sandbox.paypal',
production: 'paypal'
}[env];

return () => {
onClick({ linkName: clickTitle, src });
const urlBase = getGlobalUrl('CREDIT_APPLY');
// TODO: Get finalized query param keys
const win = window.open(
`https://www.${urlBase}.com/ppcreditapply/da/us?cats_id=DA_AD_UPSTREAM&actor=merchant&mktgrefid=${refId}&payer_id=${payerId}`
`${urlBase}?cats_id=DA_AD_UPSTREAM&actor=merchant&mktgrefid=${refId}&payer_id=${payerId}`
);
const intervalId = setInterval(() => {
if (win.closed) {
Expand Down
26 changes: 21 additions & 5 deletions src/utils/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,29 @@ export const isScriptBeingDestroyed = () => {
};

export function getPayPalDomain() {
const testEnviroment = window.__TEST_ENV__ ?? __MESSAGES__.__TEST_ENV__;
if (testEnviroment && getEnv() !== 'production' && getEnv() !== 'sandbox') {
return testEnviroment;
} else if (__MESSAGES__.__TARGET__ === 'SDK') {
if (getEnv() !== 'production' && getEnv() !== 'sandbox') {
const testEnviroment = window.__TEST_ENV__ ?? __MESSAGES__.__TEST_ENV__;

if (testEnviroment) {
return testEnviroment;
}

// eslint-disable-next-line security/detect-unsafe-regex
if (window.location.origin.match(/\.paypal\.com(:\d+)?$/)) {
return window.location.origin;
}
}

if (__MESSAGES__.__TARGET__ === 'SDK') {
return getSDKPayPalDomain();
} else {
return __MESSAGES__.__DOMAIN__[`__${getEnv().toUpperCase()}__`];
const domain = __MESSAGES__.__DOMAIN__[`__${getEnv().toUpperCase()}__`];

if (domain) {
return domain;
}

throw new Error('Missing PayPal Domain');
}
}

Expand Down
11 changes: 8 additions & 3 deletions tests/unit/spec/src/components/modal/lib/hooks/applyNow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,28 @@ describe('Apply Now URL hook', () => {

describe('Should return the staging URL', () => {
test('env = local', () => {
__ENV__ = 'local';
applyNow()();

expect(global.open).toBeCalledWith(
'https://www.msmaster.qa.paypal.com/ppcreditapply/da/us?cats_id=DA_AD_UPSTREAM&actor=merchant&mktgrefid=1&payer_id=1'
`https://localhost.paypal.com:8080/ppcreditapply/da/us?cats_id=DA_AD_UPSTREAM&actor=merchant&mktgrefid=1&payer_id=1`
);
});
test('env = stage', () => {
useXProps.mockReturnValue({ ...defaultXProps, env: 'stage' });
__ENV__ = 'stage';
window.__TEST_ENV__ = 'https://www.stage.com';
useXProps.mockReturnValue({ ...defaultXProps });
applyNow()();

expect(global.open).toBeCalledWith(
'https://www.msmaster.qa.paypal.com/ppcreditapply/da/us?cats_id=DA_AD_UPSTREAM&actor=merchant&mktgrefid=1&payer_id=1'
'https://www.stage.com/ppcreditapply/da/us?cats_id=DA_AD_UPSTREAM&actor=merchant&mktgrefid=1&payer_id=1'
);
});
});

describe('Should return the sandbox URL', () => {
test('env = sandbox', () => {
__ENV__ = 'sandbox';
useXProps.mockReturnValue({ ...defaultXProps, env: 'sandbox' });
applyNow()();

Expand All @@ -57,6 +61,7 @@ describe('Apply Now URL hook', () => {

describe('Should return production URL', () => {
test('env = production', () => {
__ENV__ = 'production';
useXProps.mockReturnValue({ ...defaultXProps, env: 'production' });
applyNow()();

Expand Down
10 changes: 8 additions & 2 deletions tests/unit/spec/src/utils/sdk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('getPayPalDomain', () => {
__ENV__ = 'stage';
__MESSAGES__ = {
__TEST_ENV__: undefined,
__DOMAIN__: { __SANDBOX__: 'https://www.sandbox.com', __STAGE__: 'https://www.stage.com' }
__DOMAIN__: { __SANDBOX__: 'https://www.sandbox.com' }
};
});
test('returns message test environment if window test environment is not set', () => {
Expand All @@ -25,7 +25,13 @@ describe('getPayPalDomain', () => {
__ENV__ = 'sandbox';
expect(getPayPalDomain()).toBe('https://www.sandbox.com');
});
test('returns stage domain if no test environment is set', () => {
test('returns error if no test environment is set', () => {
expect(() => getPayPalDomain()).toThrow(Error);
});
test('returns stage domain if set via window test env global and env is stage', () => {
__MESSAGES__.__TEST_ENV__ = 'https://www.stage.com';
window.__TEST_ENV__ = 'https://www.stage.com';
__ENV__ = 'stage';
expect(getPayPalDomain()).toBe('https://www.stage.com');
});
});
Expand Down
13 changes: 9 additions & 4 deletions tests/unit/utils/setup.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import '@testing-library/jest-dom/extend-expect';
import globals from '../../../globals';

// Tests will currently fail with non-US locale
const standaloneGlobals = globals({
TARGET: 'standalone',
VERSION: '1.0.0'
});

standaloneGlobals.__MESSAGES__.__DOMAIN__.__TEST__ = 'http://localhost.paypal.com:8080';

Object.assign(window, {
__ENV__: 'test',
__LOCAL__: false,
...globals({
TARGET: 'standalone'
})
...standaloneGlobals
});
1 change: 0 additions & 1 deletion webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ module.exports = (env = {}) => {
__PROTOCOL__: PROTOCOL,
__HOST__: `${HOSTNAME}:${PORT}`,
__SDK_HOST__: `${HOSTNAME}:${PORT}`,
__STAGE_HOST__: 'msmaster.qa.paypal.com',
__PORT__: PORT,
__PATH__: `/${FILE_NAME}.js`,
__NAMESPACE__: 'paypal',
Expand Down

0 comments on commit d3e1c52

Please sign in to comment.