@rebilly/instruments 4.8.1 → 4.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/dist/index.js +1 -1
- package/dist/index.min.js +1 -1
- package/package.json +5 -1
- package/.babelrc +0 -26
- package/project.json +0 -31
- package/rollup.config.mjs +0 -79
- package/src/data/options-schema/index.js +0 -112
- package/src/data/options-schema/schemas/options-schema.js +0 -448
- package/src/events/base-event.js +0 -47
- package/src/events/events.spec.js +0 -11
- package/src/events/index.js +0 -15
- package/src/functions/destroy.js +0 -29
- package/src/functions/destroy.spec.js +0 -63
- package/src/functions/mount/fetch-data.js +0 -236
- package/src/functions/mount/fetch-data.spec.js +0 -317
- package/src/functions/mount/get-lead-source-data.js +0 -46
- package/src/functions/mount/get-lead-source-data.spec.js +0 -38
- package/src/functions/mount/index.js +0 -105
- package/src/functions/mount/mount.spec.js +0 -96
- package/src/functions/mount/setup-element.js +0 -29
- package/src/functions/mount/setup-framepay-theme.js +0 -104
- package/src/functions/mount/setup-framepay.js +0 -26
- package/src/functions/mount/setup-i18n.js +0 -21
- package/src/functions/mount/setup-options.js +0 -100
- package/src/functions/mount/setup-options.spec.js +0 -353
- package/src/functions/mount/setup-storefront.js +0 -23
- package/src/functions/mount/setup-styles-vars.js +0 -30
- package/src/functions/mount/setup-user-flow.js +0 -60
- package/src/functions/on.js +0 -22
- package/src/functions/on.spec.js +0 -75
- package/src/functions/purchase.js +0 -173
- package/src/functions/purchase.spec.js +0 -80
- package/src/functions/setup.js +0 -66
- package/src/functions/setup.spec.js +0 -111
- package/src/functions/show.js +0 -30
- package/src/functions/show.spec.js +0 -62
- package/src/functions/update.js +0 -40
- package/src/functions/update.spec.js +0 -104
- package/src/i18n/en.json +0 -35
- package/src/i18n/es.json +0 -32
- package/src/i18n/i18n.spec.js +0 -27
- package/src/i18n/index.js +0 -59
- package/src/index.js +0 -3
- package/src/instance.js +0 -48
- package/src/instance.spec.js +0 -44
- package/src/loader/index.js +0 -145
- package/src/loader/loader.spec.js +0 -74
- package/src/state/iframes.js +0 -23
- package/src/state/index.js +0 -64
- package/src/storefront/account-and-website.js +0 -18
- package/src/storefront/account-and-website.spec.js +0 -73
- package/src/storefront/deposit-requests.js +0 -12
- package/src/storefront/fetch-plans-from-addons-bumpOffer.js +0 -30
- package/src/storefront/fetch-products-from-plans.js +0 -59
- package/src/storefront/fetch-products-from-plans.spec.js +0 -113
- package/src/storefront/index.js +0 -78
- package/src/storefront/invoices.js +0 -23
- package/src/storefront/invoices.spec.js +0 -92
- package/src/storefront/models/account-model.js +0 -36
- package/src/storefront/models/base-model.js +0 -7
- package/src/storefront/models/deposit-request-model.js +0 -24
- package/src/storefront/models/invoice-model.js +0 -20
- package/src/storefront/models/payment-metadata.js +0 -7
- package/src/storefront/models/plan-model.js +0 -94
- package/src/storefront/models/product-model.js +0 -3
- package/src/storefront/models/ready-to-pay-model.js +0 -76
- package/src/storefront/models/summary-model.js +0 -56
- package/src/storefront/models/transaction-model.js +0 -16
- package/src/storefront/models/website-model.js +0 -3
- package/src/storefront/payment-instruments.js +0 -47
- package/src/storefront/payment-instruments.spec.js +0 -88
- package/src/storefront/purchase.js +0 -60
- package/src/storefront/purchase.spec.js +0 -53
- package/src/storefront/ready-to-pay.js +0 -96
- package/src/storefront/ready-to-pay.spec.js +0 -76
- package/src/storefront/storefront.spec.js +0 -14
- package/src/storefront/summary.js +0 -114
- package/src/storefront/summary.spec.js +0 -136
- package/src/storefront/transactions.js +0 -12
- package/src/style/base/__snapshots__/theme.spec.js.snap +0 -143
- package/src/style/base/default-theme.js +0 -955
- package/src/style/base/index.js +0 -688
- package/src/style/base/theme.js +0 -30
- package/src/style/base/theme.spec.js +0 -19
- package/src/style/index.js +0 -11
- package/src/style/utils/border.js +0 -47
- package/src/style/utils/color-values.js +0 -35
- package/src/style/utils/minifyCss.js +0 -14
- package/src/utils/add-dom-element.js +0 -17
- package/src/utils/format-currency.js +0 -8
- package/src/utils/has-valid-css-selector.js +0 -4
- package/src/utils/index.js +0 -15
- package/src/utils/is-dom-element.js +0 -3
- package/src/utils/process-property-as-dom-element.js +0 -31
- package/src/utils/quantity.js +0 -47
- package/src/utils/sleep.js +0 -5
- package/src/views/amount-selector.js +0 -46
- package/src/views/common/iframe/base-iframe.js +0 -66
- package/src/views/common/iframe/events/change-iframe-src-handler.js +0 -6
- package/src/views/common/iframe/events/dispatch-event-handler.js +0 -8
- package/src/views/common/iframe/events/resize-component-handler.js +0 -9
- package/src/views/common/iframe/events/show-error-handler.js +0 -5
- package/src/views/common/iframe/events/stop-loader-handler.js +0 -9
- package/src/views/common/iframe/events/update-addons-handler.js +0 -23
- package/src/views/common/iframe/events/update-coupons-handler.js +0 -12
- package/src/views/common/iframe/events/update-items-handler.js +0 -34
- package/src/views/common/iframe/index.js +0 -5
- package/src/views/common/iframe/modal-iframe.js +0 -82
- package/src/views/common/iframe/view-iframe.js +0 -23
- package/src/views/common/render-utilities.js +0 -4
- package/src/views/confirmation.js +0 -45
- package/src/views/errors.js +0 -115
- package/src/views/form.js +0 -61
- package/src/views/method-selector/__snapshots__/method-selector.spec.js.snap +0 -135
- package/src/views/method-selector/express-methods.js +0 -52
- package/src/views/method-selector/generate-digital-wallet.js +0 -53
- package/src/views/method-selector/generate-digital-wallet.spec.js +0 -121
- package/src/views/method-selector/generate-framepay-config.js +0 -56
- package/src/views/method-selector/generate-framepay-config.spec.js +0 -216
- package/src/views/method-selector/get-method-data.js +0 -11
- package/src/views/method-selector/get-payment-methods.js +0 -25
- package/src/views/method-selector/get-payment-methods.spec.js +0 -41
- package/src/views/method-selector/index.js +0 -223
- package/src/views/method-selector/method-selector.spec.js +0 -20
- package/src/views/method-selector/mount-bump-offer.js +0 -120
- package/src/views/method-selector/mount-express-methods.js +0 -107
- package/src/views/modal.js +0 -103
- package/src/views/result.js +0 -24
- package/src/views/summary.js +0 -38
- package/tests/async-utilities.js +0 -22
- package/tests/mocks/framepay-mock.js +0 -15
- package/tests/mocks/rebilly-api-mock.js +0 -11
- package/tests/mocks/rebilly-instruments-mock.js +0 -141
- package/tests/mocks/storefront-api-mock.js +0 -62
- package/tests/mocks/storefront-mock.js +0 -20
- package/tests/msw/server.js +0 -14
- package/tests/setup-test.js +0 -30
- package/vitest.config.js +0 -18
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import mountExpressMethod from './express-methods';
|
|
2
|
-
import { generateFramepayConfig } from './generate-framepay-config';
|
|
3
|
-
import { getMethodData } from './get-method-data';
|
|
4
|
-
import Events from '../../events';
|
|
5
|
-
import state from '../../state';
|
|
6
|
-
|
|
7
|
-
const browserIsSafari = () => window.ApplePaySession;
|
|
8
|
-
|
|
9
|
-
export async function mountExpressMethods({ methods, container }) {
|
|
10
|
-
const methodIds = methods.map((expressMethod) => {
|
|
11
|
-
const { METHOD_ID } = getMethodData(expressMethod);
|
|
12
|
-
return METHOD_ID;
|
|
13
|
-
});
|
|
14
|
-
const { Rebilly } = window;
|
|
15
|
-
|
|
16
|
-
if (!Rebilly?.initialized) {
|
|
17
|
-
await Rebilly?.initialize(
|
|
18
|
-
generateFramepayConfig({
|
|
19
|
-
methodIds,
|
|
20
|
-
}),
|
|
21
|
-
);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function generateExpressMethodHeight(method) {
|
|
25
|
-
let height;
|
|
26
|
-
const optionId = method.optionsPaymentInstrumentsKey;
|
|
27
|
-
|
|
28
|
-
// Return the default size of express methods if no id is found
|
|
29
|
-
if (!optionId) return '48px';
|
|
30
|
-
|
|
31
|
-
const { buttonHeight, displayOptions } =
|
|
32
|
-
state.options?.paymentInstruments?.[optionId] || {};
|
|
33
|
-
|
|
34
|
-
if (displayOptions) {
|
|
35
|
-
height = displayOptions.buttonHeight;
|
|
36
|
-
} else {
|
|
37
|
-
height = buttonHeight;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (typeof height === 'number') {
|
|
41
|
-
height = `${buttonHeight}px`;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return height;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
methods.forEach((method) => {
|
|
48
|
-
const { METHOD_ID: id } = getMethodData(method);
|
|
49
|
-
// filter out apple pay unless in safari
|
|
50
|
-
if (id === 'apple-pay' && !browserIsSafari()) return;
|
|
51
|
-
|
|
52
|
-
container.innerHTML += `
|
|
53
|
-
<div class="rebilly-instruments-${id}-method" style="height: ${generateExpressMethodHeight(
|
|
54
|
-
method,
|
|
55
|
-
)}"></div>
|
|
56
|
-
`;
|
|
57
|
-
mountExpressMethod({
|
|
58
|
-
state,
|
|
59
|
-
id,
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
// HOTFIX: Hardcoded, use env variables in the future
|
|
64
|
-
const redirectUrl =
|
|
65
|
-
state.options.apiMode === 'sandbox'
|
|
66
|
-
? 'https://forms-sandbox.secure-payments.app/approval-url?close=true'
|
|
67
|
-
: 'https://forms.secure-payments.app/approval-url?close=true';
|
|
68
|
-
|
|
69
|
-
Rebilly?.on('token-ready', (token) => {
|
|
70
|
-
const instrumentReadyPayload = {
|
|
71
|
-
websiteId: state.options.websiteId,
|
|
72
|
-
billingAddress: token.billingAddress,
|
|
73
|
-
redirectUrl,
|
|
74
|
-
_raw: token,
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
if (state.data?.isShippingRequired) {
|
|
78
|
-
if (token.shippingAddress) {
|
|
79
|
-
instrumentReadyPayload.deliveryAddress = token.shippingAddress;
|
|
80
|
-
} else {
|
|
81
|
-
instrumentReadyPayload.deliveryAddress =
|
|
82
|
-
instrumentReadyPayload.billingAddress;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
if (Array.isArray(instrumentReadyPayload.billingAddress?.emails)) {
|
|
87
|
-
const email = instrumentReadyPayload.billingAddress.emails.find(
|
|
88
|
-
(e) => e.label === 'main',
|
|
89
|
-
);
|
|
90
|
-
|
|
91
|
-
if (email?.value) {
|
|
92
|
-
instrumentReadyPayload.billingAddress.email = email.value;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
if (Array.isArray(instrumentReadyPayload.deliveryAddress?.emails)) {
|
|
96
|
-
const email = instrumentReadyPayload.deliveryAddress.emails.find(
|
|
97
|
-
(e) => e.label === 'main',
|
|
98
|
-
);
|
|
99
|
-
|
|
100
|
-
if (email?.value) {
|
|
101
|
-
instrumentReadyPayload.deliveryAddress.email = email.value;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
Events.instrumentReady.dispatch(instrumentReadyPayload);
|
|
106
|
-
});
|
|
107
|
-
}
|
package/src/views/modal.js
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import state from '../state';
|
|
2
|
-
import { ModalIframe } from './common/iframe';
|
|
3
|
-
|
|
4
|
-
const modalTemplate = (isRedirect, method) => `
|
|
5
|
-
<div class="rebilly-instruments-modal-overlay">
|
|
6
|
-
<div class="rebilly-instruments-modal-container ${
|
|
7
|
-
method ? `rebilly-instruments-${method}` : ''
|
|
8
|
-
} ${isRedirect ? 'is-redirect' : ''}">
|
|
9
|
-
${
|
|
10
|
-
isRedirect
|
|
11
|
-
? ''
|
|
12
|
-
: `
|
|
13
|
-
<svg class="rebilly-instruments-modal-close" viewBox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
|
|
14
|
-
<path d="m15 13.5858 7.2929-7.293c.3905-.3904 1.0237-.3904 1.4142 0 .3905.3906.3905 1.0238 0 1.4143L16.4142 15l7.293 7.2929c.3904.3905.3904 1.0237 0 1.4142-.3906.3905-1.0238.3905-1.4143 0L15 16.4142l-7.2929 7.293c-.3905.3904-1.0237.3904-1.4142 0-.3905-.3906-.3905-1.0238 0-1.4143L13.5858 15l-7.293-7.2929c-.3904-.3905-.3904-1.0237 0-1.4142.3906-.3905 1.0238-.3905 1.4143 0L15 13.5858Z" fill-rule="nonzero"/>
|
|
15
|
-
</svg>
|
|
16
|
-
`
|
|
17
|
-
}
|
|
18
|
-
<div class="rebilly-instruments-modal-content"></div>
|
|
19
|
-
</div>
|
|
20
|
-
</div>
|
|
21
|
-
`;
|
|
22
|
-
|
|
23
|
-
export async function mountModal({
|
|
24
|
-
name = '',
|
|
25
|
-
url = '',
|
|
26
|
-
model = {},
|
|
27
|
-
classListArray = [],
|
|
28
|
-
close = () => {},
|
|
29
|
-
} = {}) {
|
|
30
|
-
const method = model?.method?.method;
|
|
31
|
-
const isRedirect = name === 'rebilly-instruments-approval-url';
|
|
32
|
-
state.form.insertAdjacentHTML(
|
|
33
|
-
'beforeend',
|
|
34
|
-
modalTemplate(isRedirect, method),
|
|
35
|
-
);
|
|
36
|
-
|
|
37
|
-
const modalOverlay = document.querySelector(
|
|
38
|
-
'.rebilly-instruments-modal-overlay',
|
|
39
|
-
);
|
|
40
|
-
const modalContainer = document.querySelector(
|
|
41
|
-
'.rebilly-instruments-modal-container',
|
|
42
|
-
);
|
|
43
|
-
const closeButton = document.querySelector(
|
|
44
|
-
'.rebilly-instruments-modal-close',
|
|
45
|
-
);
|
|
46
|
-
const modalContent = document.querySelector(
|
|
47
|
-
'.rebilly-instruments-modal-content',
|
|
48
|
-
);
|
|
49
|
-
|
|
50
|
-
document.body.style.overflow = 'hidden';
|
|
51
|
-
setTimeout(() => {
|
|
52
|
-
modalOverlay.classList.add('is-visible');
|
|
53
|
-
modalContainer.classList.add('is-visible');
|
|
54
|
-
}, 240);
|
|
55
|
-
state.loader.addDOMElement({ section: 'modal', el: modalContent });
|
|
56
|
-
state.loader.startLoading({ section: 'modal', id: name });
|
|
57
|
-
|
|
58
|
-
const modelSafeState = state.toModel();
|
|
59
|
-
const injectedModel = {
|
|
60
|
-
options: modelSafeState.options,
|
|
61
|
-
...model,
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
const iframe = await new ModalIframe({
|
|
65
|
-
name,
|
|
66
|
-
url,
|
|
67
|
-
model: injectedModel,
|
|
68
|
-
classListArray,
|
|
69
|
-
container: modalContent,
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
const closeModal = (...args) => {
|
|
73
|
-
modalContainer.classList.remove('is-visible');
|
|
74
|
-
modalOverlay.classList.remove('is-visible');
|
|
75
|
-
setTimeout(() => {
|
|
76
|
-
document.body.style.overflow = 'auto';
|
|
77
|
-
|
|
78
|
-
const list = modalOverlay.children;
|
|
79
|
-
for (let i = 0; i < list.length; i += 1) {
|
|
80
|
-
list[i].remove();
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
modalOverlay.remove();
|
|
84
|
-
close(...args);
|
|
85
|
-
iframe.destroy();
|
|
86
|
-
}, 300);
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
iframe.bindEventListeners({
|
|
90
|
-
close: closeModal,
|
|
91
|
-
loader: state.loader,
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
if (!isRedirect) {
|
|
95
|
-
closeButton.addEventListener('click', closeModal);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
iframe.component?.call('route', {
|
|
99
|
-
name: 'approval-url',
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
return iframe;
|
|
103
|
-
}
|
package/src/views/result.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import state from '../state';
|
|
2
|
-
import iframes from '../state/iframes';
|
|
3
|
-
import { mountForm } from './form';
|
|
4
|
-
|
|
5
|
-
export async function mountResult({ payload }) {
|
|
6
|
-
const iframe = iframes.form;
|
|
7
|
-
|
|
8
|
-
if (!iframe) {
|
|
9
|
-
await mountForm();
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const modelSafeState = state.toModel();
|
|
13
|
-
const model = {
|
|
14
|
-
options: modelSafeState.options,
|
|
15
|
-
mainStyleVars: modelSafeState.mainStyleVars,
|
|
16
|
-
[state.options.transactionType]: payload,
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
iframe?.component?.call('route', {
|
|
20
|
-
name: 'result',
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
iframe?.component?.call('update', model);
|
|
24
|
-
}
|
package/src/views/summary.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import state from '../state';
|
|
2
|
-
import iframes from '../state/iframes';
|
|
3
|
-
import { ViewIframe } from './common/iframe';
|
|
4
|
-
import { itemsUpdatedHandler } from './common/iframe/events/update-items-handler';
|
|
5
|
-
import { fetchSummary } from '../storefront/summary';
|
|
6
|
-
|
|
7
|
-
export async function mountSummary() {
|
|
8
|
-
const modelSafeState = state.toModel();
|
|
9
|
-
const model = {
|
|
10
|
-
options: modelSafeState.options,
|
|
11
|
-
data: modelSafeState.data,
|
|
12
|
-
mainStyleVars: modelSafeState.mainStyleVars,
|
|
13
|
-
};
|
|
14
|
-
const paymentMethodsUrl = state.options?._computed?.paymentMethodsUrl;
|
|
15
|
-
|
|
16
|
-
const name = 'rebilly-instruments-summary';
|
|
17
|
-
const iframe = await new ViewIframe({
|
|
18
|
-
name,
|
|
19
|
-
url: `${paymentMethodsUrl}?name=${name}`,
|
|
20
|
-
container: state.summary,
|
|
21
|
-
model,
|
|
22
|
-
route: {
|
|
23
|
-
name: 'summary',
|
|
24
|
-
},
|
|
25
|
-
});
|
|
26
|
-
iframe.bindEventListeners({ loader: state.loader });
|
|
27
|
-
iframes.summary = iframe;
|
|
28
|
-
itemsUpdatedHandler(iframe);
|
|
29
|
-
|
|
30
|
-
return {
|
|
31
|
-
then: (callback) => callback(),
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export async function updateSummary({ instrument } = {}) {
|
|
36
|
-
await fetchSummary({ data: instrument });
|
|
37
|
-
state.updateModel();
|
|
38
|
-
}
|
package/tests/async-utilities.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { sleep } from '@/utils';
|
|
2
|
-
|
|
3
|
-
export async function avoidUnhandledPromises() {
|
|
4
|
-
// We need this sleep to avoid unhandled promises.
|
|
5
|
-
// This hack will be removed if we refactor the current design.
|
|
6
|
-
await sleep(100);
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @param {Promise} promise
|
|
11
|
-
*/
|
|
12
|
-
export async function expectConfigurationError(promise) {
|
|
13
|
-
const NoConfigOrOptionsError =
|
|
14
|
-
'Could not use Rebilly Instruments mount options to fetch Rebilly data';
|
|
15
|
-
let error = null;
|
|
16
|
-
try {
|
|
17
|
-
await promise;
|
|
18
|
-
} catch (e) {
|
|
19
|
-
error = e;
|
|
20
|
-
}
|
|
21
|
-
expect(error.message).toBe(NoConfigOrOptionsError);
|
|
22
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import Emitter from 'component-emitter';
|
|
2
|
-
|
|
3
|
-
export const createFramepayMock = () => {
|
|
4
|
-
const framepayBus = new Emitter();
|
|
5
|
-
const framepayMock = {
|
|
6
|
-
initialize: vi.fn(),
|
|
7
|
-
digitalWallet: {
|
|
8
|
-
mount: vi.fn(),
|
|
9
|
-
},
|
|
10
|
-
on: (eventType, delegate = null) => framepayBus.on(eventType, delegate),
|
|
11
|
-
simulateEvent: (eventType, payload) =>
|
|
12
|
-
framepayBus.emit(eventType, payload),
|
|
13
|
-
};
|
|
14
|
-
return framepayMock;
|
|
15
|
-
};
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import RebillyInstruments from '@/index';
|
|
2
|
-
import { createFramepayMock } from './framepay-mock';
|
|
3
|
-
import { get, ok, post } from 'msw-when-then';
|
|
4
|
-
import { when } from '../msw/server';
|
|
5
|
-
import { storefrontURL } from './storefront-api-mock';
|
|
6
|
-
import PlanModel from '@/storefront/models/plan-model';
|
|
7
|
-
import ProductModel from '@/storefront/models/product-model';
|
|
8
|
-
import SummaryModel from '@/storefront/models/summary-model';
|
|
9
|
-
import InvoiceModel from '@/storefront/models/invoice-model';
|
|
10
|
-
import merge from 'lodash.merge';
|
|
11
|
-
import { DataInstance } from '@/functions/mount/fetch-data';
|
|
12
|
-
import { sleep } from '@/utils';
|
|
13
|
-
import setupFramepay from '@/functions/mount/setup-framepay';
|
|
14
|
-
|
|
15
|
-
export const setupFramepayMock = async () => {
|
|
16
|
-
/*
|
|
17
|
-
* onload() is never called in JSDOM context but we want to test the rest of the setupFramepay code,
|
|
18
|
-
* so we run it asynchonously and wait for 10 ms
|
|
19
|
-
*/
|
|
20
|
-
setupFramepay();
|
|
21
|
-
await sleep(10);
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
export async function RenderMockRebillyInstruments(options = {}) {
|
|
25
|
-
const testInvoice = new InvoiceModel({
|
|
26
|
-
id: 'test-invoice-id',
|
|
27
|
-
});
|
|
28
|
-
const testPlan = new PlanModel({ name: 'Test Plan', id: 'test-plan-id-1' });
|
|
29
|
-
const testProduct = new ProductModel({
|
|
30
|
-
description: 'My Awesome Product',
|
|
31
|
-
id: 'test-product-1',
|
|
32
|
-
name: 'My Product',
|
|
33
|
-
});
|
|
34
|
-
const testSummary = new SummaryModel({
|
|
35
|
-
currency: 'USD',
|
|
36
|
-
lineItems: [
|
|
37
|
-
{
|
|
38
|
-
description: 'test-plan-id-1',
|
|
39
|
-
planId: 'test-plan-id-1',
|
|
40
|
-
productId: 'test-product-1',
|
|
41
|
-
quantity: 1,
|
|
42
|
-
},
|
|
43
|
-
],
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
when(get(`${storefrontURL}/invoices/*`)).thenReturn(
|
|
47
|
-
(() => ok(testInvoice))(),
|
|
48
|
-
);
|
|
49
|
-
|
|
50
|
-
when(post(`${storefrontURL}/ready-to-pay`)).thenReturn(
|
|
51
|
-
(() =>
|
|
52
|
-
ok([
|
|
53
|
-
{
|
|
54
|
-
method: 'payment-card',
|
|
55
|
-
feature: {
|
|
56
|
-
name: 'Google Pay',
|
|
57
|
-
merchantName: 'google-pay-merchant-name',
|
|
58
|
-
merchantOrigin: 'google-pay-merchant-origin',
|
|
59
|
-
},
|
|
60
|
-
brands: [
|
|
61
|
-
'Visa',
|
|
62
|
-
'MasterCard',
|
|
63
|
-
'American Express',
|
|
64
|
-
'Discover',
|
|
65
|
-
],
|
|
66
|
-
filters: [],
|
|
67
|
-
},
|
|
68
|
-
]))(),
|
|
69
|
-
);
|
|
70
|
-
|
|
71
|
-
when(post(`${storefrontURL}/preview-purchase`)).thenReturn(
|
|
72
|
-
(() => ok(testSummary))(),
|
|
73
|
-
);
|
|
74
|
-
|
|
75
|
-
when(get(`${storefrontURL}/plans`)).thenReturn((() => ok([testPlan]))());
|
|
76
|
-
|
|
77
|
-
when(get(`${storefrontURL}/products`)).thenReturn(
|
|
78
|
-
(() => ok([testProduct]))(),
|
|
79
|
-
);
|
|
80
|
-
|
|
81
|
-
const defaultOptions = {
|
|
82
|
-
form: '.form-selector',
|
|
83
|
-
summary: '.summary-selector',
|
|
84
|
-
locale: 'auto',
|
|
85
|
-
apiMode: 'sandbox',
|
|
86
|
-
_dev: {
|
|
87
|
-
paymentMethodsUrl: 'https://forms.test.rebilly.dev',
|
|
88
|
-
framePayStyleLink: 'https://dev.framepay.rebilly.com/rebilly.css',
|
|
89
|
-
},
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
const hasPurchaseData = [
|
|
93
|
-
'items',
|
|
94
|
-
'invoiceId',
|
|
95
|
-
'transactionId',
|
|
96
|
-
'money',
|
|
97
|
-
'jwt',
|
|
98
|
-
].some((key) => Object.keys(options).includes(key));
|
|
99
|
-
if (!hasPurchaseData) {
|
|
100
|
-
defaultOptions.items = [
|
|
101
|
-
{
|
|
102
|
-
planId: 'test-plan-id-1',
|
|
103
|
-
quantity: 1,
|
|
104
|
-
},
|
|
105
|
-
];
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if ((options.invoiceId || options.transactionid) && !options.jwt) {
|
|
109
|
-
defaultOptions.jwt = 'test-jwt';
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
const mergedOptions = merge({ ...defaultOptions }, options);
|
|
113
|
-
|
|
114
|
-
const framepayMock = createFramepayMock();
|
|
115
|
-
global.Rebilly = framepayMock;
|
|
116
|
-
|
|
117
|
-
const form = document.createElement('div');
|
|
118
|
-
form.classList.add(`${mergedOptions.form.replace('.', '')}`);
|
|
119
|
-
document.body.appendChild(form);
|
|
120
|
-
|
|
121
|
-
const summary = document.createElement('div');
|
|
122
|
-
summary.classList.add(`${mergedOptions.summary.replace('.', '')}`);
|
|
123
|
-
document.body.appendChild(summary);
|
|
124
|
-
|
|
125
|
-
try {
|
|
126
|
-
await RebillyInstruments.mount({
|
|
127
|
-
setupFramepay: setupFramepayMock,
|
|
128
|
-
...mergedOptions,
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
RebillyInstruments.mock = {
|
|
132
|
-
data: (data) => {
|
|
133
|
-
RebillyInstruments.state.data = new DataInstance(data);
|
|
134
|
-
},
|
|
135
|
-
};
|
|
136
|
-
} catch (e) {
|
|
137
|
-
console.log(e);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
return RebillyInstruments;
|
|
141
|
-
}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { ok, get, post } from 'msw-when-then';
|
|
2
|
-
|
|
3
|
-
export const storefrontURL = '*/storefront';
|
|
4
|
-
|
|
5
|
-
export const initStoreFrontApiMocks = (when) => {
|
|
6
|
-
const mocks = [
|
|
7
|
-
/* GET */
|
|
8
|
-
{
|
|
9
|
-
url: '*/account',
|
|
10
|
-
method: get,
|
|
11
|
-
response: () => ok({}),
|
|
12
|
-
},
|
|
13
|
-
{
|
|
14
|
-
url: '*/plans',
|
|
15
|
-
method: get,
|
|
16
|
-
response: () => ok([]),
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
url: '*/payment-instruments',
|
|
20
|
-
method: get,
|
|
21
|
-
response: () => ok([]),
|
|
22
|
-
},
|
|
23
|
-
{
|
|
24
|
-
url: '*/transactions/test-transaction-id',
|
|
25
|
-
method: get,
|
|
26
|
-
response: () => ok({}),
|
|
27
|
-
},
|
|
28
|
-
|
|
29
|
-
/* POST */
|
|
30
|
-
{
|
|
31
|
-
url: '*/ready-to-pay',
|
|
32
|
-
method: post,
|
|
33
|
-
response: () =>
|
|
34
|
-
ok([
|
|
35
|
-
{
|
|
36
|
-
filters: [],
|
|
37
|
-
method: 'method',
|
|
38
|
-
},
|
|
39
|
-
]),
|
|
40
|
-
},
|
|
41
|
-
{
|
|
42
|
-
url: '*/preview-purchase',
|
|
43
|
-
method: post,
|
|
44
|
-
response: () => ok({}),
|
|
45
|
-
},
|
|
46
|
-
{
|
|
47
|
-
url: '*/payment-instruments',
|
|
48
|
-
method: post,
|
|
49
|
-
response: () => ok([]),
|
|
50
|
-
},
|
|
51
|
-
{
|
|
52
|
-
url: '*/payment-instruments/*/setup',
|
|
53
|
-
method: post,
|
|
54
|
-
response: () => ok([]),
|
|
55
|
-
},
|
|
56
|
-
];
|
|
57
|
-
|
|
58
|
-
mocks.forEach((api) => {
|
|
59
|
-
const http = api.method(`${storefrontURL}${api.url}`);
|
|
60
|
-
when(http).thenReturn(api.response());
|
|
61
|
-
});
|
|
62
|
-
};
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import Storefront from '@/storefront';
|
|
2
|
-
import { DataInstance } from '@/functions/mount/fetch-data';
|
|
3
|
-
import state from '@/state';
|
|
4
|
-
|
|
5
|
-
export function MockStorefront(config = {}) {
|
|
6
|
-
return Storefront(config);
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export function StorefontTestingInstance({ options = {}, data = {} } = {}) {
|
|
10
|
-
state.storefront = null;
|
|
11
|
-
state.storefront = MockStorefront();
|
|
12
|
-
|
|
13
|
-
state.options = null;
|
|
14
|
-
state.options = options;
|
|
15
|
-
|
|
16
|
-
state.data = null;
|
|
17
|
-
state.data = new DataInstance(data);
|
|
18
|
-
|
|
19
|
-
return state;
|
|
20
|
-
}
|
package/tests/msw/server.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { setupServer } from 'msw/node';
|
|
2
|
-
import { rest } from 'msw';
|
|
3
|
-
import { whenThen } from 'msw-when-then';
|
|
4
|
-
import { initStoreFrontApiMocks } from '../mocks/storefront-api-mock';
|
|
5
|
-
import { initRebillyApiMocks } from '../mocks/rebilly-api-mock';
|
|
6
|
-
|
|
7
|
-
export const server = setupServer();
|
|
8
|
-
|
|
9
|
-
export const { when } = whenThen(server, rest);
|
|
10
|
-
|
|
11
|
-
export const initGlobalHandlers = () => {
|
|
12
|
-
initStoreFrontApiMocks(when);
|
|
13
|
-
initRebillyApiMocks(when);
|
|
14
|
-
};
|
package/tests/setup-test.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { initGlobalHandlers, server } from './msw/server';
|
|
2
|
-
import state from '../src/state';
|
|
3
|
-
import 'vitest-canvas-mock';
|
|
4
|
-
|
|
5
|
-
vi.mock('@rebilly/risk-data-collector', async () => {
|
|
6
|
-
const actual = await vi.importActual('@rebilly/risk-data-collector');
|
|
7
|
-
return {
|
|
8
|
-
default: {
|
|
9
|
-
collectData: vi.fn().mockReturnValue({ riskMetadata: {} }),
|
|
10
|
-
},
|
|
11
|
-
...actual,
|
|
12
|
-
};
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
beforeAll(() => {
|
|
16
|
-
server.listen();
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
beforeEach(() => {
|
|
20
|
-
initGlobalHandlers();
|
|
21
|
-
state.reset();
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
afterEach(() => {
|
|
25
|
-
server.resetHandlers();
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
afterAll(() => {
|
|
29
|
-
server.close();
|
|
30
|
-
});
|
package/vitest.config.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { defineConfig } from 'vitest/config';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
|
|
4
|
-
export default defineConfig({
|
|
5
|
-
test: {
|
|
6
|
-
environment: 'jsdom',
|
|
7
|
-
globals: true,
|
|
8
|
-
watch: true,
|
|
9
|
-
setupFiles: 'tests/setup-test.js',
|
|
10
|
-
deps: {
|
|
11
|
-
inline: ['vitest-canvas-mock'],
|
|
12
|
-
},
|
|
13
|
-
alias: {
|
|
14
|
-
'@': path.resolve(__dirname, 'src'),
|
|
15
|
-
tests: path.resolve(__dirname, 'tests'),
|
|
16
|
-
},
|
|
17
|
-
},
|
|
18
|
-
});
|