@rebilly/instruments 2.1.0-beta.0 → 2.1.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.
@@ -105,69 +105,79 @@ exports.DataInstance = DataInstance;
105
105
  async function fetchData({
106
106
  state = null,
107
107
  riskMetadata = null,
108
- summaryPayload = null
108
+ summaryPayload = null,
109
+ // Dependancy injectable functions
110
+ fetchInvoice = _invoices.fetchInvoice,
111
+ fetchTransaction = _transactions.fetchTransaction
109
112
  }) {
110
- var _state$options2, _state$options3, _state$data, _state$data$transacti, _state$data2;
113
+ try {
114
+ var _state$options2, _state$options3, _state$data, _state$data$transacti, _state$data3;
115
+
116
+ let transaction = null;
117
+
118
+ if ((_state$options2 = state.options) !== null && _state$options2 !== void 0 && _state$options2.transactionId) {
119
+ transaction = await fetchTransaction({
120
+ data: {
121
+ id: state.options.transactionId
122
+ },
123
+ state
124
+ });
125
+ state.data.transaction = transaction;
126
+ }
111
127
 
112
- let transaction = null;
128
+ let invoice = null;
113
129
 
114
- if ((_state$options2 = state.options) !== null && _state$options2 !== void 0 && _state$options2.transactionId) {
115
- transaction = await (0, _transactions.fetchTransaction)({
116
- data: {
117
- id: state.options.transactionId
118
- },
119
- state
120
- });
121
- state.data.transaction = transaction;
122
- }
130
+ if ((_state$options3 = state.options) !== null && _state$options3 !== void 0 && _state$options3.invoiceId || (_state$data = state.data) !== null && _state$data !== void 0 && (_state$data$transacti = _state$data.transaction) !== null && _state$data$transacti !== void 0 && _state$data$transacti.hasInvoice) {
131
+ var _state$options4, _state$data2, _state$data2$transact;
123
132
 
124
- let invoice = null;
133
+ invoice = await fetchInvoice({
134
+ data: {
135
+ id: ((_state$options4 = state.options) === null || _state$options4 === void 0 ? void 0 : _state$options4.invoiceId) || ((_state$data2 = state.data) === null || _state$data2 === void 0 ? void 0 : (_state$data2$transact = _state$data2.transaction) === null || _state$data2$transact === void 0 ? void 0 : _state$data2$transact.invoiceId)
136
+ },
137
+ state
138
+ });
139
+ state.data.invoice = invoice;
140
+ }
141
+
142
+ if (!riskMetadata) {
143
+ const {
144
+ riskMetadata: data
145
+ } = await (0, _riskDataCollector.collectData)();
146
+ riskMetadata = data;
147
+ }
125
148
 
126
- if ((_state$options3 = state.options) !== null && _state$options3 !== void 0 && _state$options3.invoiceId || (_state$data = state.data) !== null && _state$data !== void 0 && (_state$data$transacti = _state$data.transaction) !== null && _state$data$transacti !== void 0 && _state$data$transacti.hasInvoice) {
127
- invoice = await (0, _invoices.fetchInvoice)({
128
- data: {
129
- id: state.options.invoiceId
130
- },
149
+ state.data = new DataInstance({
150
+ state,
151
+ invoice,
152
+ transaction,
153
+ riskMetadata
154
+ });
155
+ const [readyToPay, previewPurchase] = await Promise.all([((_state$data3 = state.data) === null || _state$data3 === void 0 ? void 0 : _state$data3.readyToPay) || (0, _readyToPay.fetchReadyToPay)({
156
+ riskMetadata,
157
+ state
158
+ }), state.options.items ? (0, _summary.fetchSummary)({
159
+ data: summaryPayload,
160
+ state
161
+ }) : null]);
162
+ const plans = await (0, _plans.fetchPlans)({
131
163
  state
132
164
  });
133
- state.data.invoice = invoice;
134
- }
135
-
136
- if (!riskMetadata) {
137
- const {
138
- riskMetadata: data
139
- } = await (0, _riskDataCollector.collectData)();
140
- riskMetadata = data;
165
+ state.data.plans = plans;
166
+ const products = await (0, _products.fetchProducts)({
167
+ state
168
+ });
169
+ return new DataInstance({
170
+ state,
171
+ readyToPay,
172
+ previewPurchase,
173
+ plans,
174
+ products,
175
+ invoice,
176
+ transaction,
177
+ riskMetadata
178
+ });
179
+ } catch (error) {// console.log(error);
141
180
  }
142
181
 
143
- state.data = new DataInstance({
144
- state,
145
- invoice,
146
- transaction,
147
- riskMetadata
148
- });
149
- const [readyToPay, previewPurchase] = await Promise.all([((_state$data2 = state.data) === null || _state$data2 === void 0 ? void 0 : _state$data2.readyToPay) || (0, _readyToPay.fetchReadyToPay)({
150
- riskMetadata,
151
- state
152
- }), state.options.items ? (0, _summary.fetchSummary)({
153
- data: summaryPayload,
154
- state
155
- }) : null]);
156
- const plans = await (0, _plans.fetchPlans)({
157
- state
158
- });
159
- state.data.plans = plans;
160
- const products = await (0, _products.fetchProducts)({
161
- state
162
- });
163
- return new DataInstance({
164
- state,
165
- readyToPay,
166
- previewPurchase,
167
- plans,
168
- products,
169
- invoice,
170
- transaction,
171
- riskMetadata
172
- });
182
+ return new DataInstance({});
173
183
  }
@@ -2,43 +2,67 @@
2
2
 
3
3
  var _fetchData = require("./fetch-data");
4
4
 
5
- describe.skip('Fetch Summary Data function helper', () => {
6
- class TestInstance {
7
- constructor() {
8
- this.storefront = jest.fn();
9
- this.options = jest.fn();
10
- this._fetchSummary = jest.fn(() => {
11
- return new Promise(resolve => resolve({}));
12
- });
13
- this._fetchProducts = jest.fn();
14
- }
5
+ var _transactionModel = _interopRequireDefault(require("../../storefront/models/transaction-model"));
15
6
 
16
- }
7
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
8
 
18
- it('should fetch all the data', async () => {
19
- const instance = new TestInstance();
20
- await (0, _fetchData.fetchData)({
21
- riskMetadata: {},
22
- state: instance
23
- });
24
- expect(fetchReadyToPay).toBeCalledTimes(1);
25
- expect(instance._fetchSummary).toBeCalledTimes(1);
26
- expect(instance._fetchPlans).toBeCalledTimes(1);
27
- expect(instance._fetchProducts).toBeCalledTimes(1);
9
+ describe('fetchData function', () => {
10
+ it('Should use correct invoice id for invoiceId', async () => {
11
+ const mockFetchInvoice = jest.fn();
12
+ const invoiceId = 'test-invoice-id';
13
+ const invoiceState = {
14
+ options: {
15
+ invoiceId
16
+ }
17
+ };
18
+ (0, _fetchData.fetchData)({
19
+ state: invoiceState,
20
+ fetchInvoice: mockFetchInvoice
21
+ });
22
+ expect(mockFetchInvoice).toBeCalledTimes(1);
23
+ expect(mockFetchInvoice).toBeCalledWith(expect.objectContaining({
24
+ data: {
25
+ id: invoiceId
26
+ }
27
+ }));
28
+ });
29
+ it('Should use correct invoice id for transaction with invoiceIds', async () => {
30
+ const mockFetchInvoice = jest.fn();
31
+ const invoiceId = 'test-invoice-id';
32
+ const invoiceState = {
33
+ options: {},
34
+ data: {
35
+ transaction: new _transactionModel.default({
36
+ invoiceIds: [invoiceId]
37
+ })
38
+ }
39
+ };
40
+ (0, _fetchData.fetchData)({
41
+ state: invoiceState,
42
+ fetchInvoice: mockFetchInvoice
43
+ });
44
+ expect(mockFetchInvoice).toBeCalledTimes(1);
45
+ expect(mockFetchInvoice).toBeCalledWith(expect.objectContaining({
46
+ data: {
47
+ id: invoiceId
48
+ }
49
+ }));
28
50
  });
29
- it('should pass riskMetadata to ready to pay', async () => {
30
- const testRiskMetadata = {};
31
- const instance = new TestInstance();
32
- await (0, _fetchData.fetchData)({
33
- riskMetadata: testRiskMetadata,
34
- state: instance
35
- });
36
- expect(instance._fetchReadyToPay).toBeCalledWith(testRiskMetadata);
37
- expect(async () => {
38
- await (0, _fetchData.fetchData)({
39
- state: instance
40
- });
41
- }).rejects.toEqual(new Error('riskMetadata is required for FetchSummaryData'));
51
+ it('Should not fetch invoice for transaction with no invoice Ids', async () => {
52
+ const mockFetchInvoice = jest.fn();
53
+ const invoiceState = {
54
+ options: {},
55
+ data: {
56
+ transaction: new _transactionModel.default({
57
+ invoiceIds: []
58
+ })
59
+ }
60
+ };
61
+ (0, _fetchData.fetchData)({
62
+ state: invoiceState,
63
+ fetchInvoice: mockFetchInvoice
64
+ });
65
+ expect(mockFetchInvoice).toBeCalledTimes(0);
42
66
  });
43
67
  });
44
68
  describe('DataInstance', () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rebilly/instruments",
3
- "version": "2.1.0-beta.0",
3
+ "version": "2.1.1-beta.0",
4
4
  "author": "Rebilly",
5
5
  "main": "dist/index.js",
6
6
  "license": "MIT",
@@ -3,8 +3,8 @@ import { fetchPlans } from '../../storefront/plans';
3
3
  import { fetchProducts } from '../../storefront/products';
4
4
  import { fetchReadyToPay } from '../../storefront/ready-to-pay';
5
5
  import { fetchSummary } from '../../storefront/summary';
6
- import { fetchInvoice } from '../../storefront/invoices';
7
- import { fetchTransaction } from '../../storefront/transactions';
6
+ import { fetchInvoice as FetchInvoice } from '../../storefront/invoices';
7
+ import { fetchTransaction as FetchTransaction } from '../../storefront/transactions';
8
8
 
9
9
  export class DataInstance {
10
10
  constructor({
@@ -86,53 +86,63 @@ export class DataInstance {
86
86
  export async function fetchData({
87
87
  state = null,
88
88
  riskMetadata = null,
89
- summaryPayload = null
89
+ summaryPayload = null,
90
+
91
+ // Dependancy injectable functions
92
+ fetchInvoice = FetchInvoice,
93
+ fetchTransaction = FetchTransaction
90
94
  }) {
91
- let transaction = null;
92
- if (state.options?.transactionId) {
93
- transaction = await fetchTransaction({data: {
94
- id: state.options.transactionId
95
- }, state});
96
- state.data.transaction = transaction;
97
- }
95
+ try {
96
+ let transaction = null;
97
+ if (state.options?.transactionId) {
98
+ transaction = await fetchTransaction({data: {
99
+ id: state.options.transactionId
100
+ }, state});
101
+ state.data.transaction = transaction;
102
+ }
98
103
 
99
- let invoice = null;
100
- if (state.options?.invoiceId || state.data?.transaction?.hasInvoice) {
101
- invoice = await fetchInvoice({data: {
102
- id: state.options.invoiceId
103
- }, state});
104
- state.data.invoice = invoice;
105
- }
104
+ let invoice = null;
105
+ if (state.options?.invoiceId || state.data?.transaction?.hasInvoice) {
106
+ invoice = await fetchInvoice({data: {
107
+ id: state.options?.invoiceId || state.data?.transaction?.invoiceId
108
+ }, state});
109
+ state.data.invoice = invoice;
110
+ }
106
111
 
107
- if (!riskMetadata) {
108
- const { riskMetadata: data } = await collectData();
109
- riskMetadata = data;
112
+ if (!riskMetadata) {
113
+ const { riskMetadata: data } = await collectData();
114
+ riskMetadata = data;
115
+ }
116
+
117
+ state.data = new DataInstance({
118
+ state,
119
+ invoice,
120
+ transaction,
121
+ riskMetadata
122
+ });
123
+
124
+ const [readyToPay, previewPurchase] = await Promise.all([
125
+ state.data?.readyToPay || fetchReadyToPay({ riskMetadata, state }),
126
+ state.options.items ? fetchSummary({ data: summaryPayload, state }) : null
127
+ ]);
128
+
129
+ const plans = await fetchPlans({ state });
130
+ state.data.plans = plans;
131
+ const products = await fetchProducts({ state });
132
+
133
+ return new DataInstance({
134
+ state,
135
+ readyToPay,
136
+ previewPurchase,
137
+ plans,
138
+ products,
139
+ invoice,
140
+ transaction,
141
+ riskMetadata
142
+ });
143
+ } catch(error) {
144
+ // console.log(error);
110
145
  }
111
146
 
112
- state.data = new DataInstance({
113
- state,
114
- invoice,
115
- transaction,
116
- riskMetadata
117
- });
118
-
119
- const [readyToPay, previewPurchase] = await Promise.all([
120
- state.data?.readyToPay || fetchReadyToPay({ riskMetadata, state }),
121
- state.options.items ? fetchSummary({ data: summaryPayload, state }) : null
122
- ]);
123
-
124
- const plans = await fetchPlans({ state });
125
- state.data.plans = plans;
126
- const products = await fetchProducts({ state });
127
-
128
- return new DataInstance({
129
- state,
130
- readyToPay,
131
- previewPurchase,
132
- plans,
133
- products,
134
- invoice,
135
- transaction,
136
- riskMetadata
137
- });
147
+ return new DataInstance({});
138
148
  }
@@ -1,46 +1,80 @@
1
1
  import { fetchData, DataInstance } from './fetch-data';
2
-
3
- describe.skip('Fetch Summary Data function helper', () => {
4
- class TestInstance {
5
- constructor() {
6
- this.storefront = jest.fn();
7
- this.options = jest.fn();
8
-
9
- this._fetchSummary = jest.fn(() => {
10
- return new Promise((resolve) => resolve({}));
11
- });
12
-
13
- this._fetchProducts = jest.fn();
2
+ import TransactionModel from '../../storefront/models/transaction-model';
3
+
4
+ describe('fetchData function', () => {
5
+ it('Should use correct invoice id for invoiceId', async () => {
6
+ const mockFetchInvoice = jest.fn();
7
+ const invoiceId = 'test-invoice-id';
8
+ const invoiceState = {
9
+ options: {
10
+ invoiceId
11
+ }
14
12
  }
15
- }
16
-
17
- it('should fetch all the data', async () => {
18
- const instance = new TestInstance();
19
- await fetchData({ riskMetadata: {}, state: instance });
20
- expect(fetchReadyToPay).toBeCalledTimes(1);
21
- expect(instance._fetchSummary).toBeCalledTimes(1);
22
- expect(instance._fetchPlans).toBeCalledTimes(1);
23
- expect(instance._fetchProducts).toBeCalledTimes(1);
13
+
14
+ fetchData({
15
+ state: invoiceState,
16
+ fetchInvoice: mockFetchInvoice
17
+ });
18
+
19
+ expect(mockFetchInvoice).toBeCalledTimes(1);
20
+ expect(mockFetchInvoice).toBeCalledWith(
21
+ expect.objectContaining({
22
+ data:{
23
+ id: invoiceId
24
+ }
25
+ })
26
+ );
24
27
  });
25
28
 
26
- it('should pass riskMetadata to ready to pay', async () => {
27
- const testRiskMetadata = {};
28
- const instance = new TestInstance();
29
+ it('Should use correct invoice id for transaction with invoiceIds', async () => {
30
+ const mockFetchInvoice = jest.fn();
31
+ const invoiceId = 'test-invoice-id';
32
+ const invoiceState = {
33
+ options: {},
34
+ data: {
35
+ transaction: new TransactionModel({
36
+ invoiceIds: [invoiceId]
37
+ }),
38
+ }
39
+ }
29
40
 
30
- await fetchData({ riskMetadata: testRiskMetadata, state: instance });
41
+ fetchData({
42
+ state: invoiceState,
43
+ fetchInvoice: mockFetchInvoice
44
+ });
45
+
46
+ expect(mockFetchInvoice).toBeCalledTimes(1);
47
+ expect(mockFetchInvoice).toBeCalledWith(
48
+ expect.objectContaining({
49
+ data:{
50
+ id: invoiceId
51
+ }
52
+ })
53
+ );
54
+ });
31
55
 
32
- expect(instance._fetchReadyToPay).toBeCalledWith(testRiskMetadata);
56
+ it('Should not fetch invoice for transaction with no invoice Ids', async () => {
57
+ const mockFetchInvoice = jest.fn();
58
+ const invoiceState = {
59
+ options: {},
60
+ data: {
61
+ transaction: new TransactionModel({
62
+ invoiceIds: []
63
+ }),
64
+ }
65
+ }
33
66
 
34
- expect(async () => {
35
- await fetchData({ state: instance });
36
- }).rejects.toEqual(
37
- new Error('riskMetadata is required for FetchSummaryData')
38
- );
67
+ fetchData({
68
+ state: invoiceState,
69
+ fetchInvoice: mockFetchInvoice
70
+ });
71
+
72
+ expect(mockFetchInvoice).toBeCalledTimes(0);
73
+
39
74
  });
40
75
  });
41
76
 
42
77
  describe('DataInstance', () => {
43
-
44
78
  it ('Should correctly determine amountAndCurrency', () => {
45
79
  const expectedAmountAndCurrency = {
46
80
  amount: 10,
@@ -199,6 +233,6 @@ describe('DataInstance', () => {
199
233
  }
200
234
  });
201
235
  expect(fetchedData.summaryLineItems).toEqual(expectedSummaryLineItems);
202
- })
236
+ });
203
237
  });
204
238