@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.
Files changed (139) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/index.js +1 -1
  3. package/dist/index.min.js +1 -1
  4. package/package.json +5 -1
  5. package/.babelrc +0 -26
  6. package/project.json +0 -31
  7. package/rollup.config.mjs +0 -79
  8. package/src/data/options-schema/index.js +0 -112
  9. package/src/data/options-schema/schemas/options-schema.js +0 -448
  10. package/src/events/base-event.js +0 -47
  11. package/src/events/events.spec.js +0 -11
  12. package/src/events/index.js +0 -15
  13. package/src/functions/destroy.js +0 -29
  14. package/src/functions/destroy.spec.js +0 -63
  15. package/src/functions/mount/fetch-data.js +0 -236
  16. package/src/functions/mount/fetch-data.spec.js +0 -317
  17. package/src/functions/mount/get-lead-source-data.js +0 -46
  18. package/src/functions/mount/get-lead-source-data.spec.js +0 -38
  19. package/src/functions/mount/index.js +0 -105
  20. package/src/functions/mount/mount.spec.js +0 -96
  21. package/src/functions/mount/setup-element.js +0 -29
  22. package/src/functions/mount/setup-framepay-theme.js +0 -104
  23. package/src/functions/mount/setup-framepay.js +0 -26
  24. package/src/functions/mount/setup-i18n.js +0 -21
  25. package/src/functions/mount/setup-options.js +0 -100
  26. package/src/functions/mount/setup-options.spec.js +0 -353
  27. package/src/functions/mount/setup-storefront.js +0 -23
  28. package/src/functions/mount/setup-styles-vars.js +0 -30
  29. package/src/functions/mount/setup-user-flow.js +0 -60
  30. package/src/functions/on.js +0 -22
  31. package/src/functions/on.spec.js +0 -75
  32. package/src/functions/purchase.js +0 -173
  33. package/src/functions/purchase.spec.js +0 -80
  34. package/src/functions/setup.js +0 -66
  35. package/src/functions/setup.spec.js +0 -111
  36. package/src/functions/show.js +0 -30
  37. package/src/functions/show.spec.js +0 -62
  38. package/src/functions/update.js +0 -40
  39. package/src/functions/update.spec.js +0 -104
  40. package/src/i18n/en.json +0 -35
  41. package/src/i18n/es.json +0 -32
  42. package/src/i18n/i18n.spec.js +0 -27
  43. package/src/i18n/index.js +0 -59
  44. package/src/index.js +0 -3
  45. package/src/instance.js +0 -48
  46. package/src/instance.spec.js +0 -44
  47. package/src/loader/index.js +0 -145
  48. package/src/loader/loader.spec.js +0 -74
  49. package/src/state/iframes.js +0 -23
  50. package/src/state/index.js +0 -64
  51. package/src/storefront/account-and-website.js +0 -18
  52. package/src/storefront/account-and-website.spec.js +0 -73
  53. package/src/storefront/deposit-requests.js +0 -12
  54. package/src/storefront/fetch-plans-from-addons-bumpOffer.js +0 -30
  55. package/src/storefront/fetch-products-from-plans.js +0 -59
  56. package/src/storefront/fetch-products-from-plans.spec.js +0 -113
  57. package/src/storefront/index.js +0 -78
  58. package/src/storefront/invoices.js +0 -23
  59. package/src/storefront/invoices.spec.js +0 -92
  60. package/src/storefront/models/account-model.js +0 -36
  61. package/src/storefront/models/base-model.js +0 -7
  62. package/src/storefront/models/deposit-request-model.js +0 -24
  63. package/src/storefront/models/invoice-model.js +0 -20
  64. package/src/storefront/models/payment-metadata.js +0 -7
  65. package/src/storefront/models/plan-model.js +0 -94
  66. package/src/storefront/models/product-model.js +0 -3
  67. package/src/storefront/models/ready-to-pay-model.js +0 -76
  68. package/src/storefront/models/summary-model.js +0 -56
  69. package/src/storefront/models/transaction-model.js +0 -16
  70. package/src/storefront/models/website-model.js +0 -3
  71. package/src/storefront/payment-instruments.js +0 -47
  72. package/src/storefront/payment-instruments.spec.js +0 -88
  73. package/src/storefront/purchase.js +0 -60
  74. package/src/storefront/purchase.spec.js +0 -53
  75. package/src/storefront/ready-to-pay.js +0 -96
  76. package/src/storefront/ready-to-pay.spec.js +0 -76
  77. package/src/storefront/storefront.spec.js +0 -14
  78. package/src/storefront/summary.js +0 -114
  79. package/src/storefront/summary.spec.js +0 -136
  80. package/src/storefront/transactions.js +0 -12
  81. package/src/style/base/__snapshots__/theme.spec.js.snap +0 -143
  82. package/src/style/base/default-theme.js +0 -955
  83. package/src/style/base/index.js +0 -688
  84. package/src/style/base/theme.js +0 -30
  85. package/src/style/base/theme.spec.js +0 -19
  86. package/src/style/index.js +0 -11
  87. package/src/style/utils/border.js +0 -47
  88. package/src/style/utils/color-values.js +0 -35
  89. package/src/style/utils/minifyCss.js +0 -14
  90. package/src/utils/add-dom-element.js +0 -17
  91. package/src/utils/format-currency.js +0 -8
  92. package/src/utils/has-valid-css-selector.js +0 -4
  93. package/src/utils/index.js +0 -15
  94. package/src/utils/is-dom-element.js +0 -3
  95. package/src/utils/process-property-as-dom-element.js +0 -31
  96. package/src/utils/quantity.js +0 -47
  97. package/src/utils/sleep.js +0 -5
  98. package/src/views/amount-selector.js +0 -46
  99. package/src/views/common/iframe/base-iframe.js +0 -66
  100. package/src/views/common/iframe/events/change-iframe-src-handler.js +0 -6
  101. package/src/views/common/iframe/events/dispatch-event-handler.js +0 -8
  102. package/src/views/common/iframe/events/resize-component-handler.js +0 -9
  103. package/src/views/common/iframe/events/show-error-handler.js +0 -5
  104. package/src/views/common/iframe/events/stop-loader-handler.js +0 -9
  105. package/src/views/common/iframe/events/update-addons-handler.js +0 -23
  106. package/src/views/common/iframe/events/update-coupons-handler.js +0 -12
  107. package/src/views/common/iframe/events/update-items-handler.js +0 -34
  108. package/src/views/common/iframe/index.js +0 -5
  109. package/src/views/common/iframe/modal-iframe.js +0 -82
  110. package/src/views/common/iframe/view-iframe.js +0 -23
  111. package/src/views/common/render-utilities.js +0 -4
  112. package/src/views/confirmation.js +0 -45
  113. package/src/views/errors.js +0 -115
  114. package/src/views/form.js +0 -61
  115. package/src/views/method-selector/__snapshots__/method-selector.spec.js.snap +0 -135
  116. package/src/views/method-selector/express-methods.js +0 -52
  117. package/src/views/method-selector/generate-digital-wallet.js +0 -53
  118. package/src/views/method-selector/generate-digital-wallet.spec.js +0 -121
  119. package/src/views/method-selector/generate-framepay-config.js +0 -56
  120. package/src/views/method-selector/generate-framepay-config.spec.js +0 -216
  121. package/src/views/method-selector/get-method-data.js +0 -11
  122. package/src/views/method-selector/get-payment-methods.js +0 -25
  123. package/src/views/method-selector/get-payment-methods.spec.js +0 -41
  124. package/src/views/method-selector/index.js +0 -223
  125. package/src/views/method-selector/method-selector.spec.js +0 -20
  126. package/src/views/method-selector/mount-bump-offer.js +0 -120
  127. package/src/views/method-selector/mount-express-methods.js +0 -107
  128. package/src/views/modal.js +0 -103
  129. package/src/views/result.js +0 -24
  130. package/src/views/summary.js +0 -38
  131. package/tests/async-utilities.js +0 -22
  132. package/tests/mocks/framepay-mock.js +0 -15
  133. package/tests/mocks/rebilly-api-mock.js +0 -11
  134. package/tests/mocks/rebilly-instruments-mock.js +0 -141
  135. package/tests/mocks/storefront-api-mock.js +0 -62
  136. package/tests/mocks/storefront-mock.js +0 -20
  137. package/tests/msw/server.js +0 -14
  138. package/tests/setup-test.js +0 -30
  139. package/vitest.config.js +0 -18
@@ -1,236 +0,0 @@
1
- import { collectData } from '@rebilly/risk-data-collector';
2
- import state from '../../state';
3
- import { fetchProductsFromPlans } from '../../storefront/fetch-products-from-plans';
4
- import { fetchPlansFromAddonsBumpOffer } from '../../storefront/fetch-plans-from-addons-bumpOffer';
5
- import { fetchReadyToPay } from '../../storefront/ready-to-pay';
6
- import { fetchSummary } from '../../storefront/summary';
7
- import { fetchInvoiceAndProducts as FetchInvoiceAndProducts } from '../../storefront/invoices';
8
- import { fetchTransaction as FetchTransaction } from '../../storefront/transactions';
9
- import { fetchAccountAndWebsite as FetchAccountAndWebsite } from '../../storefront/account-and-website';
10
- import { fetchPaymentInstrument as FetchInstruments } from '../../storefront/payment-instruments';
11
- import { fetchDepositRequest as FetchDepositRequest } from '../../storefront/deposit-requests';
12
- import DepositRequestModel from '../../storefront/models/deposit-request-model';
13
- import { getLeadSourceData } from './get-lead-source-data';
14
-
15
- export class DataInstance {
16
- constructor({ ...fields } = {}) {
17
- Object.entries({
18
- ...(state.data || {}),
19
- ...fields,
20
- }).forEach(([key, value]) => {
21
- this[key] = value;
22
- });
23
-
24
- this.money = state.options?.money || null;
25
- this.couponIds = [];
26
- this.addons = [];
27
- this.acceptBumpOffer = false;
28
- }
29
-
30
- get amountAndCurrency() {
31
- let currency;
32
- let amount;
33
- let amountDue;
34
-
35
- if (this.previewPurchase) {
36
- currency = this.previewPurchase.currency;
37
- amount = this.previewPurchase.total;
38
- } else if (this.invoice) {
39
- currency = this.invoice.currency;
40
- amount = this.invoice.amount;
41
- amountDue = this.invoice.amountDue;
42
- } else if (this.transaction) {
43
- currency = this.transaction.currency;
44
- amount = this.transaction.amount;
45
- } else if (this.money) {
46
- currency = this.money.currency;
47
- amount = this.money.amount;
48
- }
49
- const amountAndCurrency = {
50
- amount,
51
- currency,
52
- };
53
- if (amountDue) {
54
- amountAndCurrency.amountDue = amountDue;
55
- }
56
- return amountAndCurrency;
57
- }
58
-
59
- get isPayment() {
60
- return this.invoice || this.transaction || this.money;
61
- }
62
-
63
- get isPurchase() {
64
- return this.previewPurchase;
65
- }
66
-
67
- get hasAmountDue() {
68
- return !!(
69
- this.amountAndCurrency.amountDue &&
70
- this.amountAndCurrency.amountDue !== this.amountAndCurrency.amount
71
- );
72
- }
73
-
74
- get summaryItems() {
75
- const {
76
- discountsAmount = null,
77
- shippingAmount = null,
78
- subtotalAmount = null,
79
- taxAmount = null,
80
- } = this.previewPurchase || this.invoice || {};
81
-
82
- return {
83
- discountsAmount,
84
- shippingAmount,
85
- subtotalAmount,
86
- taxAmount,
87
- };
88
- }
89
-
90
- get summaryLineItems() {
91
- let lineItems = [];
92
- if (this.invoice) {
93
- ({ items: lineItems = [] } = this.invoice);
94
- } else {
95
- ({ lineItems = [] } = this.previewPurchase || {});
96
- }
97
-
98
- return lineItems;
99
- }
100
-
101
- get isShippingRequired() {
102
- return (this.products || []).some(
103
- (product) => product.requiresShipping,
104
- );
105
- }
106
-
107
- toPostmatesModel() {
108
- return JSON.parse(
109
- JSON.stringify({
110
- ...this,
111
- amountAndCurrency: this.amountAndCurrency,
112
- isPayment: this.isPayment,
113
- isPurchase: this.isPurchase,
114
- hasAmountDue: this.hasAmountDue,
115
- summaryItems: this.summaryItems,
116
- summaryLineItems: this.summaryLineItems,
117
- isShippingRequired: this.isShippingRequired,
118
- leadSource: getLeadSourceData(),
119
- }),
120
- );
121
- }
122
- }
123
-
124
- export async function fetchData({
125
- riskMetadata = null,
126
-
127
- // Dependency injectable functions
128
- fetchInvoiceAndProducts = FetchInvoiceAndProducts,
129
- fetchTransaction = FetchTransaction,
130
- fetchAccountAndWebsite = FetchAccountAndWebsite,
131
- fetchInstruments = FetchInstruments,
132
- fetchDepositRequest = FetchDepositRequest,
133
- } = {}) {
134
- try {
135
- state.data = new DataInstance();
136
-
137
- if (!riskMetadata) {
138
- const { riskMetadata: data } = await collectData();
139
- state.data.riskMetadata = data;
140
- } else {
141
- state.data.riskMetadata = riskMetadata;
142
- }
143
-
144
- if (state.options?.deposit) {
145
- if (state.options.deposit.depositRequestId) {
146
- state.data.deposit = await fetchDepositRequest({
147
- data: {
148
- id: state.options.deposit.depositRequestId,
149
- },
150
- });
151
- } else {
152
- state.data.deposit = new DepositRequestModel(
153
- state.options.deposit,
154
- );
155
- }
156
- }
157
-
158
- let readyToPayPromise = Promise.resolve(null);
159
-
160
- let availableInstrumentsPromise = null;
161
- if (state.options?.jwt) {
162
- fetchAccountAndWebsite({ state }).then(({ account, website }) => {
163
- state.data.account = account;
164
- state.data.website = website;
165
- });
166
- readyToPayPromise = fetchReadyToPay();
167
- availableInstrumentsPromise = fetchInstruments({ state });
168
- } else {
169
- readyToPayPromise = fetchReadyToPay();
170
- }
171
-
172
- if (state.options?.transactionId) {
173
- state.data.transaction = await fetchTransaction({
174
- data: {
175
- id: state.options.transactionId,
176
- },
177
- });
178
- }
179
-
180
- let productsPromise;
181
- const invoiceId =
182
- state.options?.invoiceId || state.data?.transaction?.invoiceId;
183
- if (invoiceId) {
184
- const { invoice, products } = await fetchInvoiceAndProducts({
185
- data: {
186
- id: invoiceId,
187
- },
188
- });
189
- productsPromise = Promise.resolve(products);
190
- state.data.invoice = invoice;
191
- }
192
-
193
- const previewPurchasePromise = state.options.items
194
- ? fetchSummary()
195
- : null;
196
-
197
- if (!state.options?.jwt) {
198
- // There is no invoice, only pla ns through the static options,
199
- // so we should fetch the products from the provided plan names.
200
- productsPromise = fetchProductsFromPlans({ state });
201
- }
202
-
203
- let plansPromise = new Promise((resolve) => {
204
- resolve([]);
205
- });
206
- if (state.options?.addons || state.options?.bumpOffer) {
207
- plansPromise = fetchPlansFromAddonsBumpOffer({ state });
208
- }
209
-
210
- const [
211
- readyToPay,
212
- previewPurchase,
213
- products,
214
- plans,
215
- availableInstruments,
216
- ] = await Promise.all([
217
- readyToPayPromise,
218
- previewPurchasePromise,
219
- productsPromise,
220
- plansPromise,
221
- availableInstrumentsPromise,
222
- ]);
223
-
224
- return new DataInstance({
225
- readyToPay,
226
- previewPurchase,
227
- products,
228
- plans,
229
- availableInstruments,
230
- });
231
- } catch (error) {
232
- // console.log(error);
233
- }
234
-
235
- return new DataInstance({});
236
- }
@@ -1,317 +0,0 @@
1
- import { StorefontTestingInstance } from 'tests/mocks/storefront-mock';
2
- import { fetchData, DataInstance } from './fetch-data';
3
- import TransactionModel from '@/storefront/models/transaction-model';
4
-
5
- describe('fetchData function', () => {
6
- it('Should use correct invoice id for invoiceId', async () => {
7
- const mockFetchInvoiceAndProducts = vi.fn();
8
- const invoiceId = 'test-invoice-id';
9
- StorefontTestingInstance({
10
- options: {
11
- invoiceId,
12
- },
13
- });
14
-
15
- await fetchData({
16
- fetchInvoiceAndProducts: mockFetchInvoiceAndProducts,
17
- });
18
-
19
- expect(mockFetchInvoiceAndProducts).toBeCalledTimes(1);
20
- expect(mockFetchInvoiceAndProducts).toBeCalledWith(
21
- expect.objectContaining({
22
- data: {
23
- id: invoiceId,
24
- },
25
- }),
26
- );
27
- });
28
-
29
- it('Should use correct invoice id for transaction with invoiceIds', async () => {
30
- const mockFetchInvoiceAndProducts = vi.fn();
31
- const invoiceId = 'test-invoice-id';
32
- StorefontTestingInstance({
33
- data: {
34
- transaction: new TransactionModel({
35
- invoiceIds: [invoiceId],
36
- }),
37
- },
38
- });
39
-
40
- await fetchData({
41
- fetchInvoiceAndProducts: mockFetchInvoiceAndProducts,
42
- });
43
-
44
- expect(mockFetchInvoiceAndProducts).toBeCalledTimes(1);
45
- expect(mockFetchInvoiceAndProducts).toBeCalledWith(
46
- expect.objectContaining({
47
- data: {
48
- id: invoiceId,
49
- },
50
- }),
51
- );
52
- });
53
-
54
- it('Should not fetch invoice for transaction with no invoice Ids', async () => {
55
- const mockFetchInvoiceAndProducts = vi.fn();
56
- StorefontTestingInstance({
57
- options: {},
58
- data: {
59
- transaction: new TransactionModel({
60
- invoiceIds: [],
61
- }),
62
- },
63
- });
64
-
65
- fetchData({
66
- fetchInvoiceAndProducts: mockFetchInvoiceAndProducts,
67
- });
68
-
69
- expect(mockFetchInvoiceAndProducts).toBeCalledTimes(0);
70
- });
71
-
72
- it('Should fetch account when JWT is supplied', async () => {
73
- const mockFetchAccountAndWebsite = vi.fn();
74
- StorefontTestingInstance({
75
- options: {
76
- jwt: 'TEST_JWT',
77
- },
78
- });
79
-
80
- await fetchData({
81
- fetchAccountAndWebsite: mockFetchAccountAndWebsite,
82
- });
83
-
84
- expect(mockFetchAccountAndWebsite).toBeCalledTimes(1);
85
- });
86
-
87
- it('Should not fetch account when there JWT is not supplied', async () => {
88
- const mockFetchAccountAndWebsite = vi.fn();
89
- StorefontTestingInstance({
90
- options: {},
91
- });
92
-
93
- await fetchData({
94
- fetchAccountAndWebsite: mockFetchAccountAndWebsite,
95
- });
96
-
97
- expect(mockFetchAccountAndWebsite).toBeCalledTimes(0);
98
- });
99
- });
100
-
101
- describe('DataInstance', () => {
102
- describe('Determining amountAndCurrency', () => {
103
- it('should use money option', () => {
104
- const expectedAmountAndCurrency = {
105
- amount: 10,
106
- currency: 'USD',
107
- };
108
- StorefontTestingInstance({
109
- options: {
110
- money: expectedAmountAndCurrency,
111
- },
112
- });
113
- const fetchedData = new DataInstance();
114
-
115
- expect(fetchedData.amountAndCurrency).toEqual(
116
- expectedAmountAndCurrency,
117
- );
118
- });
119
-
120
- it('should use preview Purchase', () => {
121
- const expectedAmountAndCurrency = {
122
- amount: 10,
123
- currency: 'USD',
124
- };
125
- StorefontTestingInstance();
126
-
127
- const fetchedData = new DataInstance({
128
- previewPurchase: {
129
- total: expectedAmountAndCurrency.amount,
130
- currency: expectedAmountAndCurrency.currency,
131
- },
132
- });
133
- expect(fetchedData.amountAndCurrency).toEqual(
134
- expect.objectContaining(expectedAmountAndCurrency),
135
- );
136
- });
137
-
138
- it('should use invoice', () => {
139
- const expectedAmountAndCurrency = {
140
- amount: 10,
141
- currency: 'USD',
142
- };
143
- StorefontTestingInstance();
144
-
145
- const fetchedData = new DataInstance({
146
- invoice: {
147
- ...expectedAmountAndCurrency,
148
- },
149
- });
150
- expect(fetchedData.amountAndCurrency).toEqual(
151
- expect.objectContaining(expectedAmountAndCurrency),
152
- );
153
- });
154
-
155
- it('should use transaction', () => {
156
- const expectedAmountAndCurrency = {
157
- amount: 10,
158
- currency: 'USD',
159
- };
160
- StorefontTestingInstance();
161
-
162
- const fetchedData = new DataInstance({
163
- transaction: {
164
- ...expectedAmountAndCurrency,
165
- },
166
- });
167
- expect(fetchedData.amountAndCurrency).toEqual(
168
- expect.objectContaining(expectedAmountAndCurrency),
169
- );
170
- });
171
-
172
- it('should handle empty value', () => {
173
- StorefontTestingInstance();
174
-
175
- const fetchedData = new DataInstance();
176
- expect(fetchedData.amountAndCurrency).toEqual(
177
- expect.objectContaining({
178
- amount: undefined,
179
- currency: undefined,
180
- }),
181
- );
182
- });
183
- });
184
-
185
- describe('isPayment', () => {
186
- it('money is truthy', () => {
187
- StorefontTestingInstance({
188
- options: {
189
- money: true,
190
- },
191
- });
192
- const fetchedData = new DataInstance();
193
- expect(fetchedData.isPayment).toBeTruthy();
194
- });
195
-
196
- it('invoice is truthy', () => {
197
- StorefontTestingInstance();
198
- const fetchedData = new DataInstance({
199
- invoice: true,
200
- });
201
- expect(fetchedData.isPayment).toBeTruthy();
202
- });
203
-
204
- it('transaction is truthy', () => {
205
- StorefontTestingInstance();
206
- const fetchedData = new DataInstance({
207
- transaction: true,
208
- });
209
- expect(fetchedData.isPayment).toBeTruthy();
210
- });
211
-
212
- it('previewPurchase is falsy', () => {
213
- StorefontTestingInstance();
214
- const fetchedData = new DataInstance({
215
- previewPurchase: true,
216
- });
217
- expect(fetchedData.isPayment).toBeFalsy();
218
- });
219
-
220
- it('empty is falsy', () => {
221
- StorefontTestingInstance();
222
- const fetchedData = new DataInstance({});
223
- expect(fetchedData.isPayment).toBeFalsy();
224
- });
225
- });
226
-
227
- describe('isPurchase', () => {
228
- it('previewPurchase is truthy', () => {
229
- StorefontTestingInstance();
230
- const fetchedData = new DataInstance({
231
- previewPurchase: true,
232
- });
233
- expect(fetchedData.isPurchase).toBeTruthy();
234
- });
235
-
236
- it('money is falsy', () => {
237
- StorefontTestingInstance({
238
- options: {
239
- money: true,
240
- },
241
- });
242
- const fetchedData = new DataInstance();
243
- expect(fetchedData.isPurchase).toBeFalsy();
244
- });
245
-
246
- it('invoice is falsy', () => {
247
- StorefontTestingInstance();
248
- const fetchedData = new DataInstance({
249
- invoice: true,
250
- });
251
- expect(fetchedData.isPurchase).toBeFalsy();
252
- });
253
-
254
- it('transaction is falsy', () => {
255
- StorefontTestingInstance();
256
- const fetchedData = new DataInstance({
257
- transaction: true,
258
- });
259
- expect(fetchedData.isPurchase).toBeFalsy();
260
- });
261
-
262
- it('empty is falsy', () => {
263
- StorefontTestingInstance();
264
- const fetchedData = new DataInstance({});
265
- expect(fetchedData.isPurchase).toBeFalsy();
266
- });
267
- });
268
-
269
- it('Should get summaryItems', () => {
270
- const expectedSummaryItems = {
271
- discountsAmount: 1,
272
- shippingAmount: 2,
273
- subtotalAmount: 3,
274
- taxAmount: 4,
275
- };
276
-
277
- let fetchedData = new DataInstance({
278
- previewPurchase: {
279
- ...expectedSummaryItems,
280
- },
281
- });
282
- expect(fetchedData.summaryItems).toEqual(
283
- expect.objectContaining(expectedSummaryItems),
284
- );
285
-
286
- fetchedData = new DataInstance({
287
- invoice: {
288
- ...expectedSummaryItems,
289
- },
290
- });
291
- expect(fetchedData.summaryItems).toEqual(
292
- expect.objectContaining(expectedSummaryItems),
293
- );
294
- });
295
-
296
- it('Should get summaryLineItems', () => {
297
- const expectedSummaryLineItems = [
298
- {
299
- line: 'item',
300
- },
301
- ];
302
-
303
- let fetchedData = new DataInstance({
304
- previewPurchase: {
305
- lineItems: expectedSummaryLineItems,
306
- },
307
- });
308
- expect(fetchedData.summaryLineItems).toEqual(expectedSummaryLineItems);
309
-
310
- fetchedData = new DataInstance({
311
- invoice: {
312
- items: expectedSummaryLineItems,
313
- },
314
- });
315
- expect(fetchedData.summaryLineItems).toEqual(expectedSummaryLineItems);
316
- });
317
- });
@@ -1,46 +0,0 @@
1
- const collectedProperties = [
2
- // Google UTM fields
3
- 'utm_source',
4
- 'utm_medium',
5
- 'utm_campaign',
6
- 'utm_term',
7
- 'utm_content',
8
- // FramePay specific fields
9
- 'affiliate',
10
- 'subAffiliate',
11
- 'clickId',
12
- 'salesAgent',
13
- ];
14
- const prefix = /utm_/g;
15
-
16
- /**
17
- * Generates the lead source data object that framepay expects as part of
18
- * the extraData property. We generate it in the top level of instruments
19
- * so that it has the correct path and query parameters.
20
- * @returns {Object} The lead source data object
21
- */
22
- export const getLeadSourceData = () => {
23
- const { href, origin, pathname, search } = window.location;
24
-
25
- const leadSource = {
26
- // Large query parameters including JWTs will cause issues, so make sure we dont
27
- // use anything longer than 255 characters.
28
- path: href.length <= 255 ? href : `${origin}${pathname}`,
29
- };
30
-
31
- const query = search.replace('?', '');
32
-
33
- // Process query parameters and add them to the leadSource object
34
- return query
35
- .split('&')
36
- .map((field) => field.split('='))
37
- .filter((field) => {
38
- const [name] = field;
39
- return collectedProperties.indexOf(name) > -1;
40
- })
41
- .reduce((data, field) => {
42
- const [name, value = null] = field;
43
- data[name.replace(prefix, '')] = value;
44
- return data;
45
- }, leadSource);
46
- };
@@ -1,38 +0,0 @@
1
- import { getLeadSourceData } from './get-lead-source-data';
2
-
3
- export const mockLocation = (url) => {
4
- const location = new URL(url);
5
- delete window.location;
6
- window.location = location;
7
- };
8
-
9
- describe('getLeadSourceData', () => {
10
- it('should return an object with a path property that matches the current URL', () => {
11
- const result = getLeadSourceData();
12
- expect(result.path).toBe(window.location.href);
13
- });
14
-
15
- it('should return the path without the query if the query is longer than 255 characters', () => {
16
- const query = Array(300).fill('a').join('');
17
-
18
- const pathWithoutQuery = 'http://www.example.com/path';
19
- mockLocation(`${pathWithoutQuery}?${query}`);
20
-
21
- const result = getLeadSourceData();
22
- expect(result.path).toBe(pathWithoutQuery);
23
- });
24
-
25
- it('should add the query parameters to the returned object', () => {
26
- const query = 'utm_source=google&utm_medium=cpc&utm_campaign=example';
27
- mockLocation(`http://www.example.com/path?${query}`);
28
-
29
- expect(getLeadSourceData()).toMatchInlineSnapshot(`
30
- {
31
- "campaign": "example",
32
- "medium": "cpc",
33
- "path": "http://www.example.com/path?utm_source=google&utm_medium=cpc&utm_campaign=example",
34
- "source": "google",
35
- }
36
- `);
37
- });
38
- });