@rebilly/instruments 3.20.0-beta.0 → 3.21.1-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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rebilly/instruments",
3
- "version": "3.20.0-beta.0",
3
+ "version": "3.21.1-beta.0",
4
4
  "author": "Rebilly",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
@@ -23,7 +23,7 @@
23
23
  "lodash.kebabcase": "^4.1.1",
24
24
  "lodash.merge": "^4.6.2",
25
25
  "popostmate": "^1.6.4",
26
- "rebilly-js-sdk": "^47.13.2",
26
+ "rebilly-js-sdk": "^47.14.2",
27
27
  "values.js": "^2.0.0"
28
28
  },
29
29
  "devDependencies": {
@@ -5,7 +5,7 @@ import { fetchReadyToPay } from '../../storefront/ready-to-pay';
5
5
  import { fetchSummary } from '../../storefront/summary';
6
6
  import { fetchInvoiceAndProducts as FetchInvoiceAndProducts } from '../../storefront/invoices';
7
7
  import { fetchTransaction as FetchTransaction } from '../../storefront/transactions';
8
- import { fetchAccount as FetchAccount } from '../../storefront/account';
8
+ import { fetchAccountAndWebsite as FetchAccountAndWebsite } from '../../storefront/account-and-website';
9
9
  import { fetchPaymentInstrument as FetchInstruments } from '../../storefront/payment-instruments';
10
10
 
11
11
  export class DataInstance {
@@ -124,7 +124,7 @@ export async function fetchData({
124
124
  // Dependency injectable functions
125
125
  fetchInvoiceAndProducts = FetchInvoiceAndProducts,
126
126
  fetchTransaction = FetchTransaction,
127
- fetchAccount = FetchAccount,
127
+ fetchAccountAndWebsite = FetchAccountAndWebsite,
128
128
  fetchInstruments = FetchInstruments
129
129
  }) {
130
130
  try {
@@ -135,8 +135,9 @@ export async function fetchData({
135
135
  let accountPromise = Promise.resolve(null);
136
136
  let availableInstrumentsPromise = null;
137
137
  if (state.options?.jwt) {
138
- accountPromise = fetchAccount({state}).then(account => {
138
+ accountPromise = fetchAccountAndWebsite({state}).then(({account, website}) => {
139
139
  state.data.account = account;
140
+ state.data.website = website;
140
141
  });
141
142
  availableInstrumentsPromise = fetchInstruments({state});
142
143
  }
@@ -75,7 +75,7 @@ describe('fetchData function', () => {
75
75
  });
76
76
 
77
77
  it('Should fetch account when JWT is supplied', async () => {
78
- const mockFetchAccount = jest.fn();
78
+ const mockFetchAccountAndWebsite = jest.fn();
79
79
  const accountState = {
80
80
  options: {
81
81
  jwt: 'TEST_JWT'
@@ -84,23 +84,23 @@ describe('fetchData function', () => {
84
84
 
85
85
  await fetchData({
86
86
  state: accountState,
87
- fetchAccount: mockFetchAccount,
87
+ fetchAccountAndWebsite: mockFetchAccountAndWebsite,
88
88
  });
89
89
 
90
- expect(mockFetchAccount).toBeCalledTimes(1);
90
+ expect(mockFetchAccountAndWebsite).toBeCalledTimes(1);
91
91
  });
92
92
 
93
93
  it('Should not fetch account when there JWT is not supplied', async () => {
94
- const mockFetchAccount = jest.fn();
94
+ const mockFetchAccountAndWebsite = jest.fn();
95
95
 
96
96
  await fetchData({
97
97
  state: {
98
98
  options: {}
99
99
  },
100
- fetchAccount: mockFetchAccount,
100
+ fetchAccountAndWebsite: mockFetchAccountAndWebsite,
101
101
  });
102
102
 
103
- expect(mockFetchAccount).toBeCalledTimes(0);
103
+ expect(mockFetchAccountAndWebsite).toBeCalledTimes(0);
104
104
  });
105
105
  });
106
106
 
@@ -0,0 +1,15 @@
1
+ import AccountModel from './models/account-model';
2
+ import WebsiteModel from './models/website-model';
3
+ import { Endpoint } from './index';
4
+
5
+ export async function fetchAccountAndWebsite({ state = null }) {
6
+ return Endpoint({state}, async () => {
7
+ state.storefront.setSessionToken(state.options.jwt);
8
+ const {fields} = await state.storefront.account.get({expand: 'website'});
9
+
10
+ return {
11
+ website: new WebsiteModel(fields._embedded?.website),
12
+ account: new AccountModel(fields),
13
+ }
14
+ });
15
+ }
@@ -0,0 +1,73 @@
1
+ import { StorefontTestingInstance } from 'tests/mocks/storefront-mock';
2
+ import { ok, get } from 'msw-when-then';
3
+ import { when } from 'tests/msw/server';
4
+ import { storefrontURL } from 'tests/mocks/storefront-api-mock';
5
+ import { fetchAccountAndWebsite } from './account-and-website';
6
+ import AccountModel, { AddressModel } from './models/account-model';
7
+ import WebsiteModel from './models/website-model';
8
+
9
+ describe('Storefront API Account', () => {
10
+ it('should handle fetch a customer with a null primary address', async () => {
11
+ const options = {
12
+ jwt: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIzYjBkNTRkOS0xNmM4LTRlZDAtYjljMy01ODAyZmQ4YzE2ZTIiLCJleHAiOjE2ODY2OTc0MTQuODQxMjUsImlhdCI6MTY1NTE2MTQxNC44Nzk4OTEsImFjbCI6W3sic2NvcGUiOnsib3JnYW5pemF0aW9uSWQiOlsiMzY0NEFGTEFKMUciXSwicGF5bWVudE1ldGhvZHMiOlsicGF5bWVudC1jYXJkIl19LCJwZXJtaXNzaW9ucyI6WzI4NCwyODYsNDE0LDQxNSw0MzQsNDExLDQxMiw0MTMsNDI0LDQyNSw0MjYsNDI3LDQyOCw0MjksNDA5LDQxMCw0MDEsNDAyLDQzMyw0ODgsNDg3XX1dLCJjbGFpbXMiOnsid2Vic2l0ZUlkIjoiZGVtby13ZWJzaXRlIiwidHJhbnNhY3Rpb25JZCI6IjRiZDE3N2NmLTFlY2EtNDA2Yy04OWI0LTEzYzMzODk5NzlmMCIsInBheW1lbnRNZXRob2RzIjpbInBheW1lbnQtY2FyZCJdfSwibWVyY2hhbnQiOiIzNjQ0QUZMQUoxRyIsImN1c3RvbWVyIjp7ImlkIjoidGVzdC1jdXN0b21lci1pZCIsIm5hbWUiOiJUZXN0IEN1c3RvbWVyIE5hbWUiLCJjcmVhdGVkVGltZSI6IjIwMjItMDctMTRUMDA6MDA6MDArMDA6MDAifX0.VPue9QBhRGe3vvncaD47w84N8Bv2hEeShUl6SlNqoOQ',
13
+ items: [
14
+ {
15
+ planId: 'test-plan-id',
16
+ quantity: 1
17
+ }
18
+ ]
19
+ };
20
+
21
+ when(get(`${storefrontURL}/acount`)).thenReturn(
22
+ ok({
23
+ defaultPaymentInstrument: null,
24
+ id: "test-customer-id",
25
+ primaryAddress: null,
26
+ websiteId: "test-website-id",
27
+ })
28
+ );
29
+
30
+ const instance = StorefontTestingInstance({
31
+ options
32
+ });
33
+
34
+ jest.spyOn(instance.storefront.account, 'get');
35
+
36
+ const response = await fetchAccountAndWebsite({ state: instance });
37
+ expect(response.account.address).toEqual(new AddressModel({}));
38
+ })
39
+
40
+ it('should return an object with account and website', async () => {
41
+ const options = {
42
+ jwt: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIzYjBkNTRkOS0xNmM4LTRlZDAtYjljMy01ODAyZmQ4YzE2ZTIiLCJleHAiOjE2ODY2OTc0MTQuODQxMjUsImlhdCI6MTY1NTE2MTQxNC44Nzk4OTEsImFjbCI6W3sic2NvcGUiOnsib3JnYW5pemF0aW9uSWQiOlsiMzY0NEFGTEFKMUciXSwicGF5bWVudE1ldGhvZHMiOlsicGF5bWVudC1jYXJkIl19LCJwZXJtaXNzaW9ucyI6WzI4NCwyODYsNDE0LDQxNSw0MzQsNDExLDQxMiw0MTMsNDI0LDQyNSw0MjYsNDI3LDQyOCw0MjksNDA5LDQxMCw0MDEsNDAyLDQzMyw0ODgsNDg3XX1dLCJjbGFpbXMiOnsid2Vic2l0ZUlkIjoiZGVtby13ZWJzaXRlIiwidHJhbnNhY3Rpb25JZCI6IjRiZDE3N2NmLTFlY2EtNDA2Yy04OWI0LTEzYzMzODk5NzlmMCIsInBheW1lbnRNZXRob2RzIjpbInBheW1lbnQtY2FyZCJdfSwibWVyY2hhbnQiOiIzNjQ0QUZMQUoxRyIsImN1c3RvbWVyIjp7ImlkIjoidGVzdC1jdXN0b21lci1pZCIsIm5hbWUiOiJUZXN0IEN1c3RvbWVyIE5hbWUiLCJjcmVhdGVkVGltZSI6IjIwMjItMDctMTRUMDA6MDA6MDArMDA6MDAifX0.VPue9QBhRGe3vvncaD47w84N8Bv2hEeShUl6SlNqoOQ',
43
+ items: [
44
+ {
45
+ planId: 'test-plan-id',
46
+ quantity: 1
47
+ }
48
+ ]
49
+ };
50
+
51
+ when(get(`${storefrontURL}/acount`)).thenReturn(
52
+ ok({
53
+ defaultPaymentInstrument: null,
54
+ id: "test-customer-id",
55
+ primaryAddress: null,
56
+ websiteId: "test-website-id",
57
+ _embedded: {
58
+ website: {},
59
+ }
60
+ })
61
+ );
62
+
63
+ const instance = StorefontTestingInstance({
64
+ options
65
+ });
66
+
67
+ jest.spyOn(instance.storefront.account, 'get');
68
+
69
+ const response = await fetchAccountAndWebsite({ state: instance });
70
+ expect(response.account instanceof(AccountModel)).toBe(true);
71
+ expect(response.website instanceof(WebsiteModel)).toBe(true);
72
+ })
73
+ });
@@ -0,0 +1,3 @@
1
+ import BaseModel from './base-model';
2
+
3
+ export default class WebsiteModel extends BaseModel {}
@@ -1,11 +0,0 @@
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
- }
@@ -1,39 +0,0 @@
1
- import { StorefontTestingInstance } from 'tests/mocks/storefront-mock';
2
- import { ok, get } from 'msw-when-then';
3
- import { when } from 'tests/msw/server';
4
- import { storefrontURL } from 'tests/mocks/storefront-api-mock';
5
- import { fetchAccount } from './account';
6
- import { AddressModel } from './models/account-model';
7
-
8
- describe('Storefront API Account', () => {
9
- it('should handle fetch a customer with a null primary address', async () => {
10
- const options = {
11
- jwt: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIzYjBkNTRkOS0xNmM4LTRlZDAtYjljMy01ODAyZmQ4YzE2ZTIiLCJleHAiOjE2ODY2OTc0MTQuODQxMjUsImlhdCI6MTY1NTE2MTQxNC44Nzk4OTEsImFjbCI6W3sic2NvcGUiOnsib3JnYW5pemF0aW9uSWQiOlsiMzY0NEFGTEFKMUciXSwicGF5bWVudE1ldGhvZHMiOlsicGF5bWVudC1jYXJkIl19LCJwZXJtaXNzaW9ucyI6WzI4NCwyODYsNDE0LDQxNSw0MzQsNDExLDQxMiw0MTMsNDI0LDQyNSw0MjYsNDI3LDQyOCw0MjksNDA5LDQxMCw0MDEsNDAyLDQzMyw0ODgsNDg3XX1dLCJjbGFpbXMiOnsid2Vic2l0ZUlkIjoiZGVtby13ZWJzaXRlIiwidHJhbnNhY3Rpb25JZCI6IjRiZDE3N2NmLTFlY2EtNDA2Yy04OWI0LTEzYzMzODk5NzlmMCIsInBheW1lbnRNZXRob2RzIjpbInBheW1lbnQtY2FyZCJdfSwibWVyY2hhbnQiOiIzNjQ0QUZMQUoxRyIsImN1c3RvbWVyIjp7ImlkIjoidGVzdC1jdXN0b21lci1pZCIsIm5hbWUiOiJUZXN0IEN1c3RvbWVyIE5hbWUiLCJjcmVhdGVkVGltZSI6IjIwMjItMDctMTRUMDA6MDA6MDArMDA6MDAifX0.VPue9QBhRGe3vvncaD47w84N8Bv2hEeShUl6SlNqoOQ',
12
- websiteId: 'test-website-id',
13
- items: [
14
- {
15
- planId: 'test-plan-id',
16
- quantity: 1
17
- }
18
- ]
19
- };
20
-
21
- when(get(`${storefrontURL}/acount`)).thenReturn(
22
- ok({
23
- defaultPaymentInstrument: null,
24
- id: "test-customer-id",
25
- primaryAddress: null,
26
- websiteId: "test-website-id",
27
- })
28
- );
29
-
30
- const instance = StorefontTestingInstance({
31
- options
32
- });
33
-
34
- jest.spyOn(instance.storefront.account, 'get');
35
-
36
- const response = await fetchAccount({ state: instance });
37
- expect(response.address).toEqual(new AddressModel({}));
38
- })
39
- });