@rebilly/instruments 3.8.4-beta.0 → 3.9.2-beta.0
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/dist/index.js +5 -5
- package/dist/index.min.js +5 -5
- package/package.json +3 -3
- package/src/functions/mount/fetch-data.js +11 -3
- package/src/functions/mount/fetch-data.spec.js +29 -0
- package/src/functions/mount/mount.spec.js +1 -8
- package/src/storefront/account.js +11 -0
- package/src/storefront/models/account-model.js +39 -0
- package/src/views/method-selector/express-methods/index.js +1 -1
- package/src/views/method-selector/mount-methods.js +1 -0
- package/tests/mocks/storefront-api-mock.js +11 -7
- package/tests/msw/server.js +1 -2
- package/tests/msw/handlers.js +0 -37
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rebilly/instruments",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.9.2-beta.0",
|
|
4
4
|
"author": "Rebilly",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"unpkg": "dist/index.min.js",
|
|
@@ -37,8 +37,8 @@
|
|
|
37
37
|
"babel-plugin-module-resolver": "^4.1.0",
|
|
38
38
|
"component-emitter": "^1.3.0",
|
|
39
39
|
"jest": "^27.0.6",
|
|
40
|
-
"msw": "
|
|
41
|
-
"msw-when-then": "^1.
|
|
40
|
+
"msw": "0.38.2",
|
|
41
|
+
"msw-when-then": "^1.5.1",
|
|
42
42
|
"rollup": "^2.35.1",
|
|
43
43
|
"rollup-plugin-ignore": "^1.0.10",
|
|
44
44
|
"rollup-plugin-polyfill-node": "^0.8.0",
|
|
@@ -5,6 +5,7 @@ import { fetchReadyToPay } from '../../storefront/ready-to-pay';
|
|
|
5
5
|
import { fetchSummary } from '../../storefront/summary';
|
|
6
6
|
import { fetchInvoice as FetchInvoice } from '../../storefront/invoices';
|
|
7
7
|
import { fetchTransaction as FetchTransaction } from '../../storefront/transactions';
|
|
8
|
+
import { fetchAccount as FetchAccount } from '../../storefront/account';
|
|
8
9
|
|
|
9
10
|
export class DataInstance {
|
|
10
11
|
constructor({
|
|
@@ -104,12 +105,14 @@ export async function fetchData({
|
|
|
104
105
|
riskMetadata = null,
|
|
105
106
|
summaryPayload = null,
|
|
106
107
|
|
|
107
|
-
//
|
|
108
|
+
// Dependency injectable functions
|
|
108
109
|
fetchInvoice = FetchInvoice,
|
|
109
|
-
fetchTransaction = FetchTransaction
|
|
110
|
+
fetchTransaction = FetchTransaction,
|
|
111
|
+
fetchAccount = FetchAccount
|
|
110
112
|
}) {
|
|
111
113
|
try {
|
|
112
114
|
let transaction = null;
|
|
115
|
+
let account = null;
|
|
113
116
|
if (state.options?.transactionId) {
|
|
114
117
|
transaction = await fetchTransaction({data: {
|
|
115
118
|
id: state.options.transactionId
|
|
@@ -130,11 +133,16 @@ export async function fetchData({
|
|
|
130
133
|
riskMetadata = data;
|
|
131
134
|
}
|
|
132
135
|
|
|
136
|
+
if (state.options?.jwt) {
|
|
137
|
+
account = await fetchAccount({state});
|
|
138
|
+
}
|
|
139
|
+
|
|
133
140
|
state.data = new DataInstance({
|
|
134
141
|
state,
|
|
135
142
|
invoice,
|
|
136
143
|
transaction,
|
|
137
|
-
riskMetadata
|
|
144
|
+
riskMetadata,
|
|
145
|
+
account,
|
|
138
146
|
});
|
|
139
147
|
|
|
140
148
|
const [readyToPay, previewPurchase] = await Promise.all([
|
|
@@ -72,6 +72,35 @@ describe('fetchData function', () => {
|
|
|
72
72
|
expect(mockFetchInvoice).toBeCalledTimes(0);
|
|
73
73
|
|
|
74
74
|
});
|
|
75
|
+
|
|
76
|
+
it('Should fetch account when JWT is supplied', async () => {
|
|
77
|
+
const mockFetchAccount = jest.fn();
|
|
78
|
+
const accountState = {
|
|
79
|
+
options: {
|
|
80
|
+
jwt: 'TEST_JWT'
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
await fetchData({
|
|
85
|
+
state: accountState,
|
|
86
|
+
fetchAccount: mockFetchAccount,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
expect(mockFetchAccount).toBeCalledTimes(1);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('Should not fetch account when there JWT is not supplied', async () => {
|
|
93
|
+
const mockFetchAccount = jest.fn();
|
|
94
|
+
|
|
95
|
+
await fetchData({
|
|
96
|
+
state: {
|
|
97
|
+
options: {}
|
|
98
|
+
},
|
|
99
|
+
fetchAccount: mockFetchAccount,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
expect(mockFetchAccount).toBeCalledTimes(0);
|
|
103
|
+
});
|
|
75
104
|
});
|
|
76
105
|
|
|
77
106
|
describe('DataInstance', () => {
|
|
@@ -1,11 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { RebillyInstrumentsInstance } from '@rebilly/instruments';
|
|
3
|
-
import { get, ok, post } from 'msw-when-then';
|
|
4
|
-
import { when } from 'tests/msw/server';
|
|
5
|
-
import { storefrontURL } from 'tests/mocks/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';
|
|
1
|
+
import {RenderMockRebillyInstruments} from 'tests/mocks/rebilly-instruments-mock';
|
|
9
2
|
|
|
10
3
|
describe('RebillyInstruments instance', () => {
|
|
11
4
|
it('should inject HTML to the merchant\'s website', async () => {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import AccountModel from './models/account-model';
|
|
2
|
+
import { Endpoint } from './index';
|
|
3
|
+
|
|
4
|
+
export async function fetchAccount({ state = null }) {
|
|
5
|
+
return Endpoint({state}, async () => {
|
|
6
|
+
state.storefront.setSessionToken(state.options.jwt);
|
|
7
|
+
const {fields} = await state.storefront.account.get();
|
|
8
|
+
|
|
9
|
+
return new AccountModel(fields);
|
|
10
|
+
});
|
|
11
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import BaseModel from './base-model';
|
|
2
|
+
|
|
3
|
+
export class AddressModel {
|
|
4
|
+
constructor({
|
|
5
|
+
firstName = null,
|
|
6
|
+
lastName = null,
|
|
7
|
+
organization = null,
|
|
8
|
+
address = null,
|
|
9
|
+
address2 = null,
|
|
10
|
+
city = null,
|
|
11
|
+
region = null,
|
|
12
|
+
country = null,
|
|
13
|
+
postalCode = null,
|
|
14
|
+
emails = [],
|
|
15
|
+
phoneNumbers = [],
|
|
16
|
+
} = {}) {
|
|
17
|
+
this.firstName = firstName;
|
|
18
|
+
this.lastName = lastName;
|
|
19
|
+
this.organization = organization;
|
|
20
|
+
this.address = address;
|
|
21
|
+
this.address2 = address2;
|
|
22
|
+
this.city = city;
|
|
23
|
+
this.region = region;
|
|
24
|
+
this.country = country;
|
|
25
|
+
this.postalCode = postalCode;
|
|
26
|
+
this.email = emails.find(v => v.primary)?.value || null;
|
|
27
|
+
this.phoneNumber = phoneNumbers.find(v => v.primary)?.value || null;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export default class AccountModel extends BaseModel {
|
|
32
|
+
constructor({
|
|
33
|
+
primaryAddress = null,
|
|
34
|
+
...fields
|
|
35
|
+
} = {}) {
|
|
36
|
+
super(fields);
|
|
37
|
+
this.address = new AddressModel(primaryAddress);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -65,6 +65,7 @@ export function MountMethods({
|
|
|
65
65
|
'payment-card'
|
|
66
66
|
].includes(methodId) && state.options.paymentInstruments[methodType]?.popup;
|
|
67
67
|
const model = {
|
|
68
|
+
account: state.data?.account,
|
|
68
69
|
options: state.options,
|
|
69
70
|
mainStyle: state.mainStyle,
|
|
70
71
|
plans: state.data.plans,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {ok, get, post} from 'msw-when-then';
|
|
2
2
|
|
|
3
3
|
export const storefrontURL = '*/storefront';
|
|
4
4
|
|
|
@@ -8,22 +8,26 @@ export const initStoreFrontApiMocks = (when) => {
|
|
|
8
8
|
})());
|
|
9
9
|
|
|
10
10
|
when(post(`${storefrontURL}/preview-purchase`)).thenReturn((() => {
|
|
11
|
-
return ok({})
|
|
11
|
+
return ok({});
|
|
12
12
|
})());
|
|
13
13
|
|
|
14
14
|
when(get(`${storefrontURL}/plans`)).thenReturn((() => {
|
|
15
|
-
return ok([])
|
|
15
|
+
return ok([]);
|
|
16
16
|
})());
|
|
17
17
|
|
|
18
18
|
when(get(`${storefrontURL}/products`)).thenReturn((() => {
|
|
19
|
-
return ok([])
|
|
19
|
+
return ok([]);
|
|
20
20
|
})());
|
|
21
21
|
|
|
22
22
|
when(post(`${storefrontURL}/payment-instruments`)).thenReturn((() => {
|
|
23
|
-
return ok([])
|
|
23
|
+
return ok([]);
|
|
24
24
|
})());
|
|
25
|
-
|
|
25
|
+
|
|
26
26
|
when(post(`${storefrontURL}/payment-instruments/*/setup`)).thenReturn((() => {
|
|
27
|
-
return ok([])
|
|
27
|
+
return ok([]);
|
|
28
|
+
})());
|
|
29
|
+
|
|
30
|
+
when(get(`${storefrontURL}/transactions/test-transaction-id`)).thenReturn((() => {
|
|
31
|
+
return ok({});
|
|
28
32
|
})());
|
|
29
33
|
};
|
package/tests/msw/server.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import {setupServer} from 'msw/node';
|
|
2
2
|
import {rest} from 'msw';
|
|
3
3
|
import {whenThen} from 'msw-when-then';
|
|
4
|
-
import {handlers} from './handlers';
|
|
5
4
|
import {initStoreFrontApiMocks} from '../mocks/storefront-api-mock';
|
|
6
5
|
import {initRebillyApiMocks} from '../mocks/rebilly-api-mock';
|
|
7
6
|
|
|
8
|
-
export const server = setupServer(
|
|
7
|
+
export const server = setupServer();
|
|
9
8
|
|
|
10
9
|
export const {when} = whenThen(server, rest);
|
|
11
10
|
|
package/tests/msw/handlers.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
const withFallback = (resolver) => {
|
|
2
|
-
return {
|
|
3
|
-
predicate () {
|
|
4
|
-
// Match all requests, regardless of URL, method, etc.
|
|
5
|
-
return true;
|
|
6
|
-
},
|
|
7
|
-
resolver
|
|
8
|
-
};
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export const handlers = [
|
|
12
|
-
withFallback((params, params2) => {
|
|
13
|
-
// Handle any requests that didn't match the existing handlers.
|
|
14
|
-
// Throw exceptions, or return a mocked response.
|
|
15
|
-
|
|
16
|
-
// params: {
|
|
17
|
-
// url: URL {},
|
|
18
|
-
// method: 'GET',
|
|
19
|
-
// body: '',
|
|
20
|
-
// headers: Headers { map: [Object] },
|
|
21
|
-
// cookies: {},
|
|
22
|
-
// params: {},
|
|
23
|
-
// redirect: 'manual',
|
|
24
|
-
// referrer: '',
|
|
25
|
-
// keepalive: false,
|
|
26
|
-
// cache: 'default',
|
|
27
|
-
// mode: 'cors',
|
|
28
|
-
// referrerPolicy: 'no-referrer',
|
|
29
|
-
// integrity: '',
|
|
30
|
-
// destination: 'document',
|
|
31
|
-
// bodyUsed: false,
|
|
32
|
-
// credentials: 'same-origin'
|
|
33
|
-
// }
|
|
34
|
-
|
|
35
|
-
throw Error(`UNHANDLED URL!!! Url: ${params.url} method: ${params.method}}`);
|
|
36
|
-
})
|
|
37
|
-
];
|