@rebilly/instruments 2.0.0-beta → 3.0.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.
Files changed (197) hide show
  1. package/dist/events/index.js +2 -1
  2. package/dist/functions/mount/fetch-data.js +187 -0
  3. package/dist/functions/mount/fetch-data.spec.js +189 -0
  4. package/dist/functions/mount/index.js +132 -164
  5. package/dist/functions/mount/mount.spec.js +2 -4
  6. package/dist/functions/mount/setup-element.js +40 -0
  7. package/dist/functions/mount/setup-framepay-theme.js +95 -0
  8. package/dist/functions/mount/setup-framepay.js +5 -1
  9. package/dist/functions/mount/setup-i18n.js +33 -0
  10. package/dist/functions/mount/setup-options.js +68 -47
  11. package/dist/functions/mount/setup-options.spec.js +66 -0
  12. package/dist/functions/mount/setup-storefront.js +6 -4
  13. package/dist/functions/mount/setup-styles.js +4 -2
  14. package/dist/functions/purchase.js +129 -24
  15. package/dist/functions/purchase.spec.js +13 -10
  16. package/dist/functions/setup.js +85 -0
  17. package/dist/functions/setup.spec.js +87 -0
  18. package/dist/functions/show.js +8 -4
  19. package/dist/functions/show.spec.js +9 -5
  20. package/dist/functions/update.js +39 -24
  21. package/dist/functions/update.spec.js +0 -4
  22. package/dist/i18n/en.json +5 -2
  23. package/dist/i18n/es.json +4 -1
  24. package/dist/index.js +17 -3
  25. package/dist/index.spec.js +3 -16
  26. package/dist/loader/index.js +4 -3
  27. package/dist/storefront/index.js +33 -0
  28. package/dist/storefront/invoices.js +27 -0
  29. package/dist/storefront/models/base-model.js +18 -0
  30. package/dist/storefront/models/invoice-model.js +14 -0
  31. package/dist/storefront/models/plan-model.js +4 -35
  32. package/dist/storefront/models/product-model.js +4 -23
  33. package/dist/storefront/models/summary-model.js +12 -25
  34. package/dist/storefront/models/transaction-model.js +31 -0
  35. package/dist/storefront/payment-instruments.js +47 -0
  36. package/dist/storefront/payment-instruments.spec.js +55 -0
  37. package/dist/storefront/plans.js +10 -18
  38. package/dist/storefront/plans.spec.js +3 -13
  39. package/dist/storefront/products.js +10 -13
  40. package/dist/storefront/products.spec.js +12 -19
  41. package/dist/storefront/purchase.js +23 -12
  42. package/dist/storefront/purchase.spec.js +1 -20
  43. package/dist/storefront/ready-to-pay.js +18 -15
  44. package/dist/storefront/ready-to-pay.spec.js +2 -12
  45. package/dist/storefront/summary.js +21 -17
  46. package/dist/storefront/summary.spec.js +4 -15
  47. package/dist/storefront/transactions.js +27 -0
  48. package/dist/style/base/__snapshots__/theme.spec.js.snap +188 -45
  49. package/dist/style/base/default-theme.js +699 -0
  50. package/dist/style/base/index.js +48 -16
  51. package/dist/style/base/theme.js +16 -48
  52. package/dist/style/base/theme.spec.js +4 -15
  53. package/dist/style/components/address.js +3 -3
  54. package/dist/style/components/button.js +32 -22
  55. package/dist/style/components/divider.js +9 -9
  56. package/dist/style/components/forms/checkbox.js +12 -9
  57. package/dist/style/components/forms/field.js +18 -6
  58. package/dist/style/components/forms/form.js +2 -2
  59. package/dist/style/components/forms/input.js +54 -13
  60. package/dist/style/components/forms/label.js +39 -18
  61. package/dist/style/components/forms/select.js +54 -22
  62. package/dist/style/components/forms/validation.js +53 -6
  63. package/dist/style/components/icons.js +4 -4
  64. package/dist/style/components/loader.js +5 -3
  65. package/dist/style/components/methods.js +18 -15
  66. package/dist/style/components/overlay.js +5 -5
  67. package/dist/style/helpers/index.js +46 -46
  68. package/dist/style/index.js +3 -1
  69. package/dist/style/payment-instruments/payment-card.js +4 -4
  70. package/dist/style/utils/border.js +47 -0
  71. package/dist/style/utils/color-values.js +39 -3
  72. package/dist/style/utils/remove-empty-null.js +20 -0
  73. package/dist/style/vendor/framepay.js +11 -8
  74. package/dist/style/vendor/postmate.js +2 -2
  75. package/dist/style/views/confirmation.js +13 -13
  76. package/dist/style/views/method-selector.js +2 -2
  77. package/dist/style/views/modal.js +6 -6
  78. package/dist/style/views/result.js +4 -4
  79. package/dist/style/views/summary.js +26 -22
  80. package/dist/views/__snapshots__/summary.spec.js.snap +77 -119
  81. package/dist/views/common/iframe/base-iframe.js +2 -0
  82. package/dist/views/common/iframe/modal-iframe.js +50 -4
  83. package/dist/views/confirmation.js +19 -8
  84. package/dist/views/method-selector/generate-digital-wallet.js +12 -3
  85. package/dist/views/method-selector/generate-digital-wallet.spec.js +11 -0
  86. package/dist/views/method-selector/get-payment-methods.js +13 -2
  87. package/dist/views/method-selector/get-payment-methods.spec.js +21 -19
  88. package/dist/views/method-selector/index.js +23 -34
  89. package/dist/views/method-selector/method-selector.spec.js +50 -55
  90. package/dist/views/method-selector/mount-methods.js +5 -8
  91. package/dist/views/modal.js +8 -2
  92. package/dist/views/result.js +3 -4
  93. package/dist/views/summary.js +156 -97
  94. package/dist/views/summary.spec.js +53 -58
  95. package/package.json +4 -2
  96. package/src/events/index.js +2 -1
  97. package/src/functions/mount/fetch-data.js +152 -0
  98. package/src/functions/mount/fetch-data.spec.js +238 -0
  99. package/src/functions/mount/index.js +101 -158
  100. package/src/functions/mount/mount.spec.js +3 -5
  101. package/src/functions/mount/setup-element.js +26 -0
  102. package/src/functions/mount/setup-framepay-theme.js +82 -0
  103. package/src/functions/mount/setup-framepay.js +5 -1
  104. package/src/functions/mount/setup-i18n.js +19 -0
  105. package/src/functions/mount/setup-options.js +78 -48
  106. package/src/functions/mount/setup-options.spec.js +60 -0
  107. package/src/functions/mount/setup-storefront.js +6 -4
  108. package/src/functions/mount/setup-styles.js +4 -2
  109. package/src/functions/on.spec.js +1 -1
  110. package/src/functions/purchase.js +99 -23
  111. package/src/functions/purchase.spec.js +10 -10
  112. package/src/functions/setup.js +48 -0
  113. package/src/functions/setup.spec.js +98 -0
  114. package/src/functions/show.js +2 -4
  115. package/src/functions/show.spec.js +3 -4
  116. package/src/functions/update.js +40 -25
  117. package/src/functions/update.spec.js +0 -4
  118. package/src/i18n/en.json +5 -2
  119. package/src/i18n/es.json +4 -1
  120. package/src/index.js +9 -3
  121. package/src/index.spec.js +3 -21
  122. package/src/loader/index.js +3 -3
  123. package/src/storefront/index.js +28 -0
  124. package/src/storefront/invoices.js +11 -0
  125. package/src/storefront/models/base-model.js +10 -0
  126. package/src/storefront/models/invoice-model.js +3 -0
  127. package/src/storefront/models/plan-model.js +3 -35
  128. package/src/storefront/models/product-model.js +3 -23
  129. package/src/storefront/models/summary-model.js +12 -19
  130. package/src/storefront/models/transaction-model.js +19 -0
  131. package/src/storefront/payment-instruments.js +30 -0
  132. package/src/storefront/payment-instruments.spec.js +69 -0
  133. package/src/storefront/plans.js +6 -17
  134. package/src/storefront/plans.spec.js +4 -11
  135. package/src/storefront/products.js +8 -16
  136. package/src/storefront/products.spec.js +16 -22
  137. package/src/storefront/purchase.js +14 -16
  138. package/src/storefront/purchase.spec.js +2 -14
  139. package/src/storefront/ready-to-pay.js +13 -16
  140. package/src/storefront/ready-to-pay.spec.js +3 -10
  141. package/src/storefront/summary.js +19 -17
  142. package/src/storefront/summary.spec.js +5 -12
  143. package/src/storefront/transactions.js +11 -0
  144. package/src/style/base/__snapshots__/theme.spec.js.snap +188 -45
  145. package/src/style/base/default-theme.js +674 -0
  146. package/src/style/base/index.js +48 -16
  147. package/src/style/base/theme.js +17 -47
  148. package/src/style/base/theme.spec.js +4 -16
  149. package/src/style/components/address.js +3 -3
  150. package/src/style/components/button.js +32 -24
  151. package/src/style/components/divider.js +9 -9
  152. package/src/style/components/forms/checkbox.js +11 -11
  153. package/src/style/components/forms/field.js +18 -6
  154. package/src/style/components/forms/form.js +2 -2
  155. package/src/style/components/forms/input.js +54 -13
  156. package/src/style/components/forms/label.js +39 -18
  157. package/src/style/components/forms/select.js +54 -22
  158. package/src/style/components/forms/validation.js +53 -6
  159. package/src/style/components/icons.js +4 -4
  160. package/src/style/components/loader.js +4 -5
  161. package/src/style/components/methods.js +18 -15
  162. package/src/style/components/overlay.js +5 -5
  163. package/src/style/helpers/index.js +46 -46
  164. package/src/style/index.js +2 -1
  165. package/src/style/payment-instruments/payment-card.js +4 -4
  166. package/src/style/utils/border.js +34 -0
  167. package/src/style/utils/color-values.js +27 -1
  168. package/src/style/utils/remove-empty-null.js +10 -0
  169. package/src/style/vendor/framepay.js +11 -8
  170. package/src/style/vendor/postmate.js +2 -2
  171. package/src/style/views/confirmation.js +13 -13
  172. package/src/style/views/method-selector.js +2 -2
  173. package/src/style/views/modal.js +6 -6
  174. package/src/style/views/result.js +4 -4
  175. package/src/style/views/summary.js +26 -22
  176. package/src/views/__snapshots__/summary.spec.js.snap +77 -119
  177. package/src/views/common/iframe/base-iframe.js +2 -0
  178. package/src/views/common/iframe/modal-iframe.js +45 -3
  179. package/src/views/confirmation.js +15 -5
  180. package/src/views/method-selector/generate-digital-wallet.js +10 -3
  181. package/src/views/method-selector/generate-digital-wallet.spec.js +10 -0
  182. package/src/views/method-selector/get-payment-methods.js +7 -2
  183. package/src/views/method-selector/get-payment-methods.spec.js +26 -23
  184. package/src/views/method-selector/index.js +21 -28
  185. package/src/views/method-selector/method-selector.spec.js +49 -64
  186. package/src/views/method-selector/mount-methods.js +5 -8
  187. package/src/views/modal.js +6 -2
  188. package/src/views/result.js +4 -3
  189. package/src/views/summary.js +161 -117
  190. package/src/views/summary.spec.js +60 -75
  191. package/tests/mocks/rebilly-instruments-mock.js +37 -7
  192. package/tests/mocks/storefront-api-mock.js +8 -0
  193. package/tests/mocks/storefront-mock.js +17 -0
  194. package/dist/functions/mount/fetch-summary-data.js +0 -46
  195. package/dist/functions/mount/fetch-summary-data.spec.js +0 -43
  196. package/src/functions/mount/fetch-summary-data.js +0 -29
  197. package/src/functions/mount/fetch-summary-data.spec.js +0 -40
@@ -1,118 +1,170 @@
1
1
  import { collectData } from '@rebilly/risk-data-collector';
2
2
  import { formatCurrency } from '../utils';
3
- import { fetchSummaryData } from '../functions/mount/fetch-summary-data';
4
-
5
- export const baseSummaryHTML = `
6
- <div class="rebilly-instruments-content">
7
- <div class="rebilly-instruments-summary-line-items"></div>
8
- <div class="rebilly-instruments-summary-breakdown"></div>
9
- </div>
10
- `;
11
-
12
- export function mountSummary({ state, summaryOptions }) {
13
- const { summary, products } = summaryOptions;
14
- state.summary.innerHTML += baseSummaryHTML;
15
- const itemsContainer = document.querySelector(
16
- '.rebilly-instruments-summary-line-items'
17
- );
18
- const summaryBreakdown = document.querySelector(
19
- '.rebilly-instruments-summary-breakdown'
20
- );
3
+ import { fetchData } from '../functions/mount/fetch-data';
4
+
5
+ export function lineItemHTML({state, lineItem}) {
6
+ const {products} = state.data;
7
+ const {
8
+ currency = null,
9
+ } = state.data?.amountAndCurrency;
10
+
21
11
  const {
22
- items: optionItems
12
+ items: optionItems = []
23
13
  } = state.options;
24
14
  const hasThumbnails = optionItems?.some((optionItem) => optionItem.thumbnail);
15
+ const { thumbnail = null } = optionItems.find(
16
+ (optionItem) => optionItem.planId === lineItem.planId
17
+ ) || {};
25
18
 
26
- const {
27
- lineItems,
28
- currency,
29
- discountsAmount,
30
- shippingAmount,
31
- subtotalAmount,
32
- taxAmount,
33
- total
34
- } = summary;
35
-
36
- lineItems.forEach((lineItem) => {
37
- const { thumbnail } = optionItems.find(
38
- (optionItem) => optionItem.planId === lineItem.planId
39
- );
40
- const { name } = products.find(
41
- (product) => product.id === lineItem.productId
42
- );
43
-
44
- itemsContainer.innerHTML += `
45
- <div class="rebilly-instruments-summary-line-item">
46
- ${
47
- hasThumbnails
48
- ? `
49
- <figure class="rebilly-instruments-summary-line-item-figure">
50
- ${thumbnail ? `<img src="${thumbnail}" alt="${name}">` : ''}
51
- </figure>
52
- `
53
- : ''
54
- }
55
- <div class="rebilly-instruments-summary-line-item-synopsis">
56
- <p class="rebilly-instruments-summary-line-item-synopsis-title">${name}</p>
57
- ${
58
- lineItem.description
59
- ? `
60
- <p class="rebilly-instruments-summary-line-item-synopsis-description">${lineItem.description}</p>
61
- `
62
- : ''
63
- }
64
- </div>
65
- <div class="rebilly-instruments-summary-line-item-price-breakdown">
66
- <p class="rebilly-instruments-summary-line-item-price-breakdown-quantity">${
67
- lineItem.quantity
68
- }</p>
69
- <svg class="rebilly-instruments-icon" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
70
- <path d="M12 10.5858l2.8284-2.8284c.3906-.3906 1.0237-.3906 1.4142 0 .3906.3905.3906 1.0236 0 1.4142L13.4142 12l2.8284 2.8284c.3906.3906.3906 1.0237 0 1.4142-.3905.3906-1.0236.3906-1.4142 0L12 13.4142l-2.8284 2.8284c-.3906.3906-1.0237.3906-1.4142 0-.3906-.3905-.3906-1.0236 0-1.4142L10.5858 12 7.7574 9.1716c-.3906-.3906-.3906-1.0237 0-1.4142.3905-.3906 1.0236-.3906 1.4142 0L12 10.5858z" fill-rule="nonzero"/>
71
- </svg>
72
- <p class="rebilly-instruments-summary-line-item-price-breakdown-unit-price">${formatCurrency(
73
- lineItem.unitPrice,
74
- currency
75
- )}</p>
76
- </div>
19
+ const { name } = products.find(
20
+ (product) => product.id === lineItem.productId
21
+ );
22
+
23
+ return `
24
+ <div class="rebilly-instruments-summary-line-item">
25
+ ${
26
+ hasThumbnails
27
+ ? `
28
+ <figure class="rebilly-instruments-summary-line-item-figure">
29
+ ${thumbnail ? `<img src="${thumbnail}" alt="${name}">` : ''}
30
+ </figure>
31
+ `
32
+ : ''
33
+ }
34
+ <div class="rebilly-instruments-summary-line-item-synopsis">
35
+ <p class="rebilly-instruments-summary-line-item-synopsis-title">${name}</p>
36
+ ${
37
+ lineItem.description
38
+ ? `
39
+ <p class="rebilly-instruments-summary-line-item-synopsis-description">${lineItem.description}</p>
40
+ `
41
+ : ''
42
+ }
43
+ </div>
44
+ <div class="rebilly-instruments-summary-line-item-price-breakdown">
45
+ <p class="rebilly-instruments-summary-line-item-price-breakdown-quantity">${
46
+ lineItem.quantity
47
+ }</p>
48
+ <svg class="rebilly-instruments-icon" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
49
+ <path d="M12 10.5858l2.8284-2.8284c.3906-.3906 1.0237-.3906 1.4142 0 .3906.3905.3906 1.0236 0 1.4142L13.4142 12l2.8284 2.8284c.3906.3906.3906 1.0237 0 1.4142-.3905.3906-1.0236.3906-1.4142 0L12 13.4142l-2.8284 2.8284c-.3906.3906-1.0237.3906-1.4142 0-.3906-.3905-.3906-1.0236 0-1.4142L10.5858 12 7.7574 9.1716c-.3906-.3906-.3906-1.0237 0-1.4142.3905-.3906 1.0236-.3906 1.4142 0L12 10.5858z" fill-rule="nonzero"/>
50
+ </svg>
51
+ <p class="rebilly-instruments-summary-line-item-price-breakdown-unit-price">${formatCurrency(
52
+ lineItem.unitPrice,
53
+ currency
54
+ )}</p>
77
55
  </div>
78
- `;
79
- });
80
- summaryBreakdown.innerHTML = `
81
- <table>
82
- <colgroup>
83
- <col>
84
- <col>
85
- </colgroup>
86
- <tr class="rebilly-instruments-summary-breakdown-sub-total">
87
- <td data-rebilly-i18n="summary.subTotal">Sub Total</td>
88
- <td>${formatCurrency(subtotalAmount, currency)}</td>
89
- </tr>
90
- <tr class="rebilly-instruments-summary-breakdown-discounts">
91
- <td data-rebilly-i18n="summary.discounts">Discounts</td>
92
- <td>${formatCurrency(discountsAmount, currency)}</td>
93
- </tr>
94
- <tr class="rebilly-instruments-summary-breakdown-taxes">
95
- <td data-rebilly-i18n="summary.taxes">Taxes</td>
96
- <td>${formatCurrency(taxAmount, currency)}</td>
97
- </tr>
98
- <tr class="rebilly-instruments-summary-breakdown-taxes">
99
- <td data-rebilly-i18n="summary.shipping">Shipping</td>
100
- <td>${formatCurrency(shippingAmount, currency)}</td>
101
- </tr>
102
- </table>
103
- <div class="rebilly-instruments-summary-breakdown-total">
104
- <p data-rebilly-i18n="summary.total">Total</p>
105
- <p class="rebilly-instruments-summary-breakdown-total-amount">
106
- <span class="rebilly-instruments-summary-breakdown-total-amount-currency">${currency}</span>${formatCurrency(
107
- total,
108
- currency
109
- )}
110
- </p>
111
56
  </div>
112
57
  `;
58
+ }
113
59
 
114
- state.translate.translateItems();
60
+ export function summaryBreakdownHTML({state, element}) {
61
+ const {
62
+ amount = null,
63
+ currency = null,
64
+ discountsAmount = null,
65
+ shippingAmount = null,
66
+ subtotalAmount = null,
67
+ taxAmount = null,
68
+ } = {
69
+ ...state.data.amountAndCurrency,
70
+ ...state.data.summaryItems
71
+ };
72
+
73
+ let table = null;
74
+ [
75
+ {
76
+ label: 'Sub Total',
77
+ class: 'sub-total',
78
+ i18n: 'summary.subTotal',
79
+ value: subtotalAmount,
80
+ show: true,
81
+ },
82
+ {
83
+ label: 'Discounts',
84
+ class: 'discounts',
85
+ i18n: 'summary.discounts',
86
+ value: discountsAmount,
87
+ show: discountsAmount > 0
88
+ },
89
+ {
90
+ label: 'Taxes',
91
+ class: 'taxes',
92
+ i18n: 'summary.taxes',
93
+ value: taxAmount,
94
+ show: true,
95
+ },
96
+ {
97
+ label: 'Shipping',
98
+ class: 'shipping',
99
+ i18n: 'summary.shipping',
100
+ value: shippingAmount,
101
+ show: state.data.isShippingRequired,
102
+ },
103
+ ].filter(({value}) => value !== null)
104
+ .forEach(item => {
105
+ if (!table) {
106
+ table = document.createElement('table');
107
+ table.insertAdjacentHTML('beforeend', `
108
+ <colgroup>
109
+ <col>
110
+ <col>
111
+ </colgroup>
112
+ `);
113
+ }
115
114
 
115
+ if (item.show) {
116
+ const row = document.createElement('tr');
117
+ row.setAttribute('class', `rebilly-instruments-summary-breakdown-${item.class}`)
118
+ row.insertAdjacentHTML('beforeend', `
119
+ <td data-rebilly-i18n="${item.i18n}">${item.label}</td>
120
+ <td>${formatCurrency(item.value, currency)}</td>
121
+ `);
122
+ table.appendChild(row);
123
+ }
124
+ });
125
+ if (table) {
126
+ element.appendChild(table);
127
+ }
128
+
129
+ const totalElement = document.createElement('div');
130
+ totalElement.setAttribute('class', 'rebilly-instruments-summary-breakdown-total');
131
+ totalElement.insertAdjacentHTML('beforeend', `
132
+ <p data-rebilly-i18n="summary.total">Total</p>
133
+ <p class="rebilly-instruments-summary-breakdown-total-amount">
134
+ <span class="rebilly-instruments-summary-breakdown-total-amount-currency">
135
+ ${currency}
136
+ </span>
137
+ ${formatCurrency(amount, currency)}
138
+ <p>
139
+ `);
140
+ element.appendChild(totalElement);
141
+
142
+ return element;
143
+ }
144
+
145
+ export async function mountSummary({ state }) {
146
+ const contentContainer = document.createElement('div');
147
+ contentContainer.setAttribute('class', 'rebilly-instruments-content');
148
+
149
+ const lineItems = state.data.summaryLineItems;
150
+ if (lineItems) {
151
+ const lineItemsElement = document.createElement('div');
152
+ lineItemsElement.setAttribute('class', 'rebilly-instruments-summary-line-items');
153
+
154
+ lineItems.forEach((lineItem) => {
155
+ lineItemsElement.insertAdjacentHTML('beforeend', lineItemHTML({ state, lineItem }));
156
+ });
157
+ contentContainer.appendChild(lineItemsElement);
158
+ }
159
+
160
+ const summaryBreakdownElement = document.createElement('div');
161
+ summaryBreakdownElement.setAttribute('class', 'rebilly-instruments-summary-breakdown');
162
+ contentContainer.appendChild(summaryBreakdownHTML({ state, element: summaryBreakdownElement}));
163
+
164
+ state.summary.appendChild(contentContainer);
165
+ state.summary.style.minHeight = '';
166
+
167
+ state.translate.translateItems();
116
168
  state.loader.stopLoading({ section: 'summary', id: 'initSummary' });
117
169
  }
118
170
 
@@ -128,29 +180,21 @@ export async function updateSummary({ state, instrument = null }) {
128
180
  };
129
181
  }
130
182
 
131
- const {
132
- readyToPay,
133
- summary: summaryData,
134
- plans,
135
- products
136
- } = await fetchSummaryData({
183
+ state.data = await fetchData({
137
184
  riskMetadata,
138
185
  summaryPayload,
139
186
  state
140
187
  });
141
188
 
142
- const summaryOptions = {
143
- summary: summaryData,
144
- readyToPay,
145
- plans,
146
- products
147
- };
189
+ if (state.data.transaction && state.data.transaction?.type === 'setup') {
190
+ state.options.transactionType = 'setup';
191
+ }
148
192
 
149
193
  const itemsContainer = state.summary.querySelector(
150
194
  '.rebilly-instruments-content'
151
195
  );
152
196
  itemsContainer?.remove();
153
197
 
154
- mountSummary({ state, summaryOptions });
198
+ mountSummary({ state });
155
199
  }
156
200
 
@@ -4,7 +4,7 @@ import ProductModel from '@/storefront/models/product-model';
4
4
  import { Loader } from '../loader';
5
5
  import { Translate } from '../i18n';
6
6
  import { mountSummary } from './summary';
7
- import { RebillyInstrumentsInstance } from '../index';
7
+ import { DataInstance } from '../functions/mount/fetch-data';
8
8
 
9
9
  describe('Summary component', () => {
10
10
  let summaryElement;
@@ -19,12 +19,67 @@ describe('Summary component', () => {
19
19
  options = {},
20
20
  summary = summaryElement,
21
21
  loader = new Loader(),
22
- translate = new Translate()
22
+ translate = new Translate(),
23
+ data = {},
23
24
  } = {}) {
24
25
  this.options = options;
25
26
  this.summary = summary;
26
27
  this.loader = loader;
27
28
  this.translate = translate;
29
+ this.data = new DataInstance({
30
+ state: {options},
31
+ previewPurchase: new SummaryModel({
32
+ currency: 'USD',
33
+ lineItems: [
34
+ {
35
+ type: 'debit',
36
+ description: 'My Awesome Product',
37
+ unitPrice: 30,
38
+ quantity: 1,
39
+ price: 30,
40
+ productId: 'test-product-1',
41
+ planId: 'my-awesome-product'
42
+ },
43
+ {
44
+ type: 'debit',
45
+ description: 'Awesome T-Shirt',
46
+ unitPrice: 20,
47
+ quantity: 2,
48
+ price: 40,
49
+ productId: 'test-product-2',
50
+ planId: 'awesome-t-shirt'
51
+ }
52
+ ],
53
+ subtotalAmount: 70,
54
+ taxAmount: 0,
55
+ shippingAmount: 0,
56
+ discountsAmount: 0,
57
+ total: 70
58
+ }),
59
+ plans: [
60
+ new PlanModel({
61
+ name: 'My Awesome Product',
62
+ id: 'my-awesome-product',
63
+ productId: 'test-product-1'
64
+ }),
65
+ new PlanModel({
66
+ name: 'My Awesome T-Shirt',
67
+ id: 'awesome-t-shirt',
68
+ productId: 'test-product-2'
69
+ })
70
+ ],
71
+ products: [
72
+ new ProductModel({
73
+ name: 'My Awesome Product',
74
+ id: 'test-product-1'
75
+ }),
76
+ new ProductModel({
77
+ name: 'My Awesome T-Shirt',
78
+ id: 'test-product-2'
79
+ })
80
+ ],
81
+ ...data
82
+ });
28
83
  }
29
84
  }
30
85
 
@@ -45,59 +100,6 @@ describe('Summary component', () => {
45
100
  ]
46
101
  };
47
102
 
48
- const summaryData = new SummaryModel({
49
- currency: 'USD',
50
- lineItems: [
51
- {
52
- type: 'debit',
53
- description: 'My Awesome Product',
54
- unitPrice: 30,
55
- quantity: 1,
56
- price: 30,
57
- productId: 'test-product-1',
58
- planId: 'my-awesome-product'
59
- },
60
- {
61
- type: 'debit',
62
- description: 'Awesome T-Shirt',
63
- unitPrice: 20,
64
- quantity: 2,
65
- price: 40,
66
- productId: 'test-product-2',
67
- planId: 'awesome-t-shirt'
68
- }
69
- ],
70
- subtotalAmount: 70,
71
- taxAmount: 0,
72
- shippingAmount: 0,
73
- discountsAmount: 0,
74
- total: 70
75
- });
76
-
77
- const planData = [
78
- new PlanModel({
79
- name: 'My Awesome Product',
80
- id: 'my-awesome-product',
81
- productId: 'test-product-1'
82
- }),
83
- new PlanModel({
84
- name: 'My Awesome T-Shirt',
85
- id: 'awesome-t-shirt',
86
- productId: 'test-product-2'
87
- })
88
- ];
89
-
90
- const productData = [
91
- new ProductModel({
92
- description: 'My Awesome Product',
93
- id: 'test-product-1'
94
- }),
95
- new ProductModel({
96
- description: 'My Awesome T-Shirt',
97
- id: 'test-product-2'
98
- })
99
- ];
100
-
101
103
  it('should render the summary correctly', () => {
102
104
  const mountSummaryInstance = new TestMountSummaryInstance({
103
105
  options
@@ -106,14 +108,7 @@ describe('Summary component', () => {
106
108
  //TODO: improve design to avoid this manual change
107
109
  mountSummaryInstance.loader.DOM.summary = mountSummaryInstance.summary;
108
110
 
109
- mountSummary({
110
- state: mountSummaryInstance,
111
- summaryOptions: {
112
- summary: summaryData,
113
- plans: planData,
114
- products: productData
115
- }
116
- });
111
+ mountSummary({ state: mountSummaryInstance });
117
112
 
118
113
  // Number of line items
119
114
  const itemsContainer = document.querySelector(
@@ -129,27 +124,17 @@ describe('Summary component', () => {
129
124
  });
130
125
 
131
126
  it('should not render the plan description if its falsy', () => {
132
- const state = new RebillyInstrumentsInstance();
133
- state.options = options;
134
-
135
127
  const mountSummaryInstance = new TestMountSummaryInstance({
136
128
  options
137
129
  });
138
130
 
139
131
  // Making one product description falsy
140
- summaryData.lineItems[0].description = null;
132
+ mountSummaryInstance.data.previewPurchase.lineItems[0].description = null;
141
133
 
142
134
  //TODO: improve design to avoid this manual change
143
135
  mountSummaryInstance.loader.DOM.summary = mountSummaryInstance.summary;
144
136
 
145
- mountSummary({
146
- state: mountSummaryInstance,
147
- summaryOptions: {
148
- summary: summaryData,
149
- plans: planData,
150
- products: productData
151
- }
152
- });
137
+ mountSummary({ state: mountSummaryInstance });
153
138
 
154
139
  // Check that only one description is render
155
140
  const itemsSynopsysDescription = document.querySelectorAll(
@@ -6,9 +6,14 @@ import { storefrontURL } from 'tests/mocks/storefront-api-mock';
6
6
  import PlanModel from '@/storefront/models/plan-model';
7
7
  import ProductModel from '@/storefront/models/product-model';
8
8
  import SummaryModel from '@/storefront/models/summary-model';
9
+ import InvoiceModel from '@/storefront/models/invoice-model';
9
10
  import merge from 'lodash.merge';
11
+ import { DataInstance } from '../../src/functions/mount/fetch-data';
10
12
 
11
13
  export async function RenderMockRebillyInstruments(options = {}) {
14
+ const testInvoice = new InvoiceModel({
15
+ id: 'test-invoice-id'
16
+ });
12
17
  const testPlan = new PlanModel({ name: 'Test Plan', id: 'test-plan-id-1' });
13
18
  const testProduct = new ProductModel({
14
19
  description: 'My Awesome Product',
@@ -28,6 +33,12 @@ export async function RenderMockRebillyInstruments(options = {}) {
28
33
  });
29
34
  const framePayStyleUrl = 'https://dev.framepay.rebilly.com/rebilly.css';
30
35
 
36
+ when(get(`${storefrontURL}/invoices/*`)).thenReturn(
37
+ (() => {
38
+ return ok(testInvoice);
39
+ })()
40
+ );
41
+
31
42
  when(post(`${storefrontURL}/ready-to-pay`)).thenReturn(
32
43
  (() => {
33
44
  return ok([
@@ -67,17 +78,27 @@ export async function RenderMockRebillyInstruments(options = {}) {
67
78
  form: '.form-selector',
68
79
  summary: '.summary-selector',
69
80
  locale: 'auto',
70
- items: [
71
- {
72
- planId: 'test-plan-id-1',
73
- quantity: 1
74
- }
75
- ],
76
81
  _dev: {
77
82
  framePayStyleLink: framePayStyleUrl
78
83
  },
79
84
  };
80
- const mergedOptions = merge(defaultOptions, options);
85
+
86
+ const hasPurchaseData = [
87
+ 'items',
88
+ 'invoiceId',
89
+ 'transactionId',
90
+ 'money'
91
+ ].some(key => Object.keys(options).includes(key))
92
+ if(!hasPurchaseData) {
93
+ defaultOptions.items = [
94
+ {
95
+ planId: 'test-plan-id-1',
96
+ quantity: 1
97
+ }
98
+ ];
99
+ }
100
+
101
+ const mergedOptions = merge({...defaultOptions}, options);
81
102
 
82
103
  const framepayMock = createFramepayMock();
83
104
  global.Rebilly = framepayMock;
@@ -91,5 +112,14 @@ export async function RenderMockRebillyInstruments(options = {}) {
91
112
 
92
113
  await rebillyInstruments.mount(mergedOptions);
93
114
 
115
+ rebillyInstruments.mock = {
116
+ data: (data) => {
117
+ rebillyInstruments.state.data = new DataInstance({
118
+ state: rebillyInstruments.state,
119
+ ...data
120
+ });
121
+ }
122
+ };
123
+
94
124
  return rebillyInstruments;
95
125
  }
@@ -18,4 +18,12 @@ export const initStoreFrontApiMocks = (when) => {
18
18
  when(get(`${storefrontURL}/products`)).thenReturn((() => {
19
19
  return ok([])
20
20
  })());
21
+
22
+ when(post(`${storefrontURL}/payment-instruments`)).thenReturn((() => {
23
+ return ok([])
24
+ })());
25
+
26
+ when(post(`${storefrontURL}/payment-instruments/*/setup`)).thenReturn((() => {
27
+ return ok([])
28
+ })());
21
29
  };
@@ -1,5 +1,22 @@
1
1
  import Storefront from '@/storefront';
2
+ import { DataInstance } from '@/functions/mount/fetch-data';
2
3
 
3
4
  export function MockStorefront(config = {}) {
4
5
  return Storefront(config);
6
+ }
7
+
8
+ export function StorefontTestingInstance({
9
+ MockStorefrontConfig = {},
10
+ options = {},
11
+ data = {}
12
+ } = {}) {
13
+ class TestInstance {
14
+ constructor() {
15
+ this.options = options;
16
+ this.storefront = MockStorefront(MockStorefrontConfig);
17
+ this.data = new DataInstance({state: {options}, ...data});
18
+ }
19
+ }
20
+
21
+ return new TestInstance();
5
22
  }
@@ -1,46 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.fetchSummaryData = fetchSummaryData;
7
-
8
- var _plans = require("../../storefront/plans");
9
-
10
- var _products = require("../../storefront/products");
11
-
12
- var _readyToPay = require("../../storefront/ready-to-pay");
13
-
14
- var _summary = require("../../storefront/summary");
15
-
16
- async function fetchSummaryData({
17
- state = null,
18
- riskMetadata = null,
19
- summaryPayload = null
20
- }) {
21
- if (!riskMetadata) {
22
- throw new Error('riskMetadata is required for FetchSummaryData');
23
- }
24
-
25
- const [readyToPay, summary] = await Promise.all([(0, _readyToPay.fetchReadyToPay)({
26
- riskMetadata,
27
- state
28
- }), (0, _summary.fetchSummary)({
29
- data: summaryPayload,
30
- state
31
- })]);
32
- const plans = await (0, _plans.fetchPlans)({
33
- data: summary,
34
- state
35
- });
36
- const products = await (0, _products.fetchProducts)({
37
- data: plans,
38
- state
39
- });
40
- return {
41
- readyToPay,
42
- summary,
43
- plans,
44
- products
45
- };
46
- }
@@ -1,43 +0,0 @@
1
- "use strict";
2
-
3
- var _fetchSummaryData = require("./fetch-summary-data");
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
- }
15
-
16
- }
17
-
18
- it.skip('should fetch all the data', async () => {
19
- const instance = new TestInstance();
20
- await (0, _fetchSummaryData.fetchSummaryData)({
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);
28
- });
29
- it('should pass riskMetadata to ready to pay', async () => {
30
- const testRiskMetadata = {};
31
- const instance = new TestInstance();
32
- await (0, _fetchSummaryData.fetchSummaryData)({
33
- riskMetadata: testRiskMetadata,
34
- state: instance
35
- });
36
- expect(instance._fetchReadyToPay).toBeCalledWith(testRiskMetadata);
37
- expect(async () => {
38
- await (0, _fetchSummaryData.fetchSummaryData)({
39
- state: instance
40
- });
41
- }).rejects.toEqual(new Error('riskMetadata is required for FetchSummaryData'));
42
- });
43
- });