@tapcart/mobile-components 0.8.48 → 0.8.49

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.
@@ -0,0 +1,387 @@
1
+ import { getVariablesCalculatedCartData } from "./variablesCart.util";
2
+ import { DiscountApplicationTargetType, } from "app-studio-types";
3
+ const baseCartData = {
4
+ id: "cart123",
5
+ subtotal: 100.0,
6
+ currency: "USD",
7
+ discounts: [],
8
+ attributes: [],
9
+ note: "",
10
+ items: [],
11
+ cost: {
12
+ subtotalAmount: {
13
+ amount: 0,
14
+ currencyCode: "",
15
+ },
16
+ totalAmount: {
17
+ amount: 0,
18
+ currencyCode: "",
19
+ },
20
+ },
21
+ };
22
+ describe("cart-provider.util", () => {
23
+ describe("getData", () => {
24
+ it("should calculate order level discounts correctly", () => {
25
+ const mockCart = Object.assign(Object.assign({}, baseCartData), { discounts: ["DISCOUNT10"], items: [], discountAllocations: [
26
+ {
27
+ targetType: DiscountApplicationTargetType.LineItem,
28
+ discountedAmount: { amount: "10.00", currencyCode: "USD" },
29
+ code: "DISCOUNT10",
30
+ },
31
+ ] });
32
+ const result = getVariablesCalculatedCartData(mockCart);
33
+ expect(result.orderAndLineItemDiscounts).toHaveLength(1);
34
+ expect(result.orderAndLineItemDiscounts[0]).toEqual({
35
+ id: "DISCOUNT10",
36
+ amount: 10,
37
+ name: "Discount - DISCOUNT10",
38
+ type: "ORDER_LEVEL",
39
+ });
40
+ });
41
+ it("should calculate gift cards correctly", () => {
42
+ const mockCart = Object.assign(Object.assign({}, baseCartData), { discounts: [], items: [], appliedGiftCards: [
43
+ {
44
+ id: "gid://shopify/AppliedGiftCard/abcd1234",
45
+ lastCharacters: "1234",
46
+ amountUsed: { amount: "25.00", currencyCode: "USD" },
47
+ presentmentAmountUsed: { amount: "25.00", currencyCode: "USD" },
48
+ },
49
+ ] });
50
+ const result = getVariablesCalculatedCartData(mockCart);
51
+ expect(result.appliedGiftCards).toHaveLength(1);
52
+ expect(result.appliedGiftCards[0]).toEqual({
53
+ id: "gid://shopify/AppliedGiftCard/abcd1234",
54
+ amount: 25,
55
+ name: "Gift Card - 1234",
56
+ });
57
+ });
58
+ it("should calculate sales amount correctly", () => {
59
+ const mockCart = Object.assign(Object.assign({}, baseCartData), { discounts: [], items: [
60
+ {
61
+ id: "line1",
62
+ quantity: 2,
63
+ productId: "prod1",
64
+ variantId: "var1",
65
+ price: 15.0,
66
+ compareAtPrice: 20.0,
67
+ currencyCode: "USD",
68
+ discounts: [],
69
+ cost: { totalAmount: { amount: "30.00", currencyCode: "USD" } },
70
+ },
71
+ ] });
72
+ const result = getVariablesCalculatedCartData(mockCart);
73
+ expect(result.salesAmount).toBe(10); // (20-15) * 2
74
+ });
75
+ it("should detect free shipping", () => {
76
+ const mockCart = Object.assign(Object.assign({}, baseCartData), { discounts: [], items: [], discountAllocations: [
77
+ {
78
+ targetType: DiscountApplicationTargetType.ShippingLine,
79
+ discountedAmount: { amount: "5.00", currencyCode: "USD" },
80
+ code: "FREESHIP",
81
+ },
82
+ ] });
83
+ const result = getVariablesCalculatedCartData(mockCart);
84
+ expect(result.isFreeShipping).toBe(true);
85
+ });
86
+ it("should calculate total discounted price correctly", () => {
87
+ const mockCart = Object.assign(Object.assign({}, baseCartData), { discounts: ["Discount10"], items: [
88
+ {
89
+ id: "line1",
90
+ quantity: 1,
91
+ productId: "prod1",
92
+ variantId: "var1",
93
+ price: 80.0,
94
+ compareAtPrice: 100.0,
95
+ currencyCode: "USD",
96
+ discounts: [
97
+ {
98
+ amount: 10,
99
+ type: "LINE_ITEM",
100
+ code: "DISCOUNT10",
101
+ },
102
+ ],
103
+ cost: { totalAmount: { amount: "80.00", currencyCode: "USD" } },
104
+ },
105
+ {
106
+ id: "line2",
107
+ quantity: 1,
108
+ productId: "prod2",
109
+ variantId: "var2",
110
+ price: 80.0,
111
+ compareAtPrice: 100.0,
112
+ currencyCode: "USD",
113
+ discounts: [
114
+ {
115
+ amount: 10,
116
+ type: "LINE_ITEM",
117
+ code: "DISCOUNT10",
118
+ },
119
+ ],
120
+ cost: { totalAmount: { amount: "80.00", currencyCode: "USD" } },
121
+ },
122
+ ], discountAllocations: [
123
+ {
124
+ targetType: DiscountApplicationTargetType.LineItem,
125
+ discountedAmount: { amount: "10.00", currencyCode: "USD" },
126
+ code: "DiScOUnT10",
127
+ },
128
+ ], appliedGiftCards: [
129
+ {
130
+ id: "gid://shopify/AppliedGiftCard/5678efgh",
131
+ lastCharacters: "1234",
132
+ amountUsed: { amount: "5.00", currencyCode: "USD" },
133
+ presentmentAmountUsed: { amount: "5.00", currencyCode: "USD" },
134
+ },
135
+ ] });
136
+ const result = getVariablesCalculatedCartData(mockCart);
137
+ // 100 (compare) - 10 (discount) - 20 (sale) - 5 (gift card) = 65
138
+ expect(result.totalDiscountedPrice).toBe(125);
139
+ });
140
+ it("should never return negative or NaN values for totalDiscountedPrice", () => {
141
+ // Test case 1: Normal case with valid data
142
+ const normalCart = Object.assign(Object.assign({}, baseCartData), { items: [
143
+ {
144
+ id: "line1",
145
+ quantity: 1,
146
+ productId: "prod1",
147
+ variantId: "var1",
148
+ price: 80.0,
149
+ compareAtPrice: 100.0,
150
+ currencyCode: "USD",
151
+ discounts: [],
152
+ cost: { totalAmount: { amount: "80.00", currencyCode: "USD" } },
153
+ },
154
+ ] });
155
+ let result = getVariablesCalculatedCartData(normalCart);
156
+ expect(result.totalDiscountedPrice).not.toBeLessThan(0);
157
+ expect(isNaN(result.totalDiscountedPrice)).toBe(false);
158
+ // Test case 2: Edge case with extreme discounts that exceed item price
159
+ const extremeDiscountCart = Object.assign(Object.assign({}, baseCartData), { items: [
160
+ {
161
+ id: "line1",
162
+ quantity: 1,
163
+ productId: "prod1",
164
+ variantId: "var1",
165
+ price: 20.0,
166
+ compareAtPrice: 50.0,
167
+ currencyCode: "USD",
168
+ discounts: [
169
+ {
170
+ amount: 100,
171
+ type: "LINE_ITEM",
172
+ code: "HUGE_DISCOUNT",
173
+ },
174
+ ],
175
+ cost: { totalAmount: { amount: "20.00", currencyCode: "USD" } },
176
+ },
177
+ ], discountAllocations: [
178
+ {
179
+ targetType: DiscountApplicationTargetType.LineItem,
180
+ discountedAmount: { amount: "100.00", currencyCode: "USD" },
181
+ code: "HUGE_DISCOUNT",
182
+ },
183
+ ] });
184
+ result = getVariablesCalculatedCartData(extremeDiscountCart);
185
+ expect(result.totalDiscountedPrice).not.toBeLessThan(0);
186
+ expect(isNaN(result.totalDiscountedPrice)).toBe(false);
187
+ // Test case 3: Edge case with missing price data
188
+ const missingDataCart = Object.assign(Object.assign({}, baseCartData), { items: [
189
+ {
190
+ id: "line1",
191
+ quantity: 1,
192
+ productId: "prod1",
193
+ variantId: "var1",
194
+ price: null,
195
+ compareAtPrice: null,
196
+ currencyCode: "USD",
197
+ discounts: [],
198
+ cost: {
199
+ totalAmount: { amount: undefined, currencyCode: "USD" },
200
+ },
201
+ },
202
+ ] });
203
+ result = getVariablesCalculatedCartData(missingDataCart);
204
+ expect(result.totalDiscountedPrice).not.toBeLessThan(0);
205
+ expect(isNaN(result.totalDiscountedPrice)).toBe(false);
206
+ });
207
+ it("should handle real-world CartState data correctly", () => {
208
+ // Real-world CartState object based on user data with type fixing
209
+ const realWorldCart = {
210
+ attributes: [
211
+ {
212
+ key: "discount",
213
+ value: "discount",
214
+ },
215
+ {
216
+ value: "20AA9E6F-A53F-4F81-85BC-62A1054CF1A2",
217
+ key: "tapcart_id",
218
+ },
219
+ ],
220
+ currency: "USD",
221
+ cost: {
222
+ totalAmount: {
223
+ currencyCode: "USD",
224
+ amount: 657.96,
225
+ },
226
+ subtotalAmount: {
227
+ currencyCode: "USD",
228
+ amount: 679.96,
229
+ },
230
+ },
231
+ id: "gid://shopify/Cart/Z2NwLXVzLXdlc3QxOjAxSlJYUDZRUk1CS1pSRllDSlNTNUgxWFQz?key=28d946aea201ce67bc7ed41d11419247",
232
+ note: "",
233
+ items: [
234
+ {
235
+ selectedOptions: [
236
+ {
237
+ name: "Size",
238
+ value: "S",
239
+ },
240
+ ],
241
+ id: "gid://shopify/CartLine/ba89bf02-1880-469e-9d2b-0408516eef03?cart=Z2NwLXVzLXdlc3QxOjAxSlJYUDZRUk1CS1pSRllDSlNTNUgxWFQz",
242
+ image: "https://cdn.shopify.com/s/files/1/0656/7939/2959/files/IMG_4797_x500.jpg?v=1720669849",
243
+ variantId: "43528195834047",
244
+ currencyCode: "USD",
245
+ totalAmount: 239.96,
246
+ title: "Tan Legend Faux Shearing Jacket",
247
+ featuredImage: "https://cdn.shopify.com/s/files/1/0656/7939/2959/files/IMG_4797.jpg?v=1720669849",
248
+ productId: "7907453993151",
249
+ compareAtPrice: 99.99,
250
+ productVendor: "Emmys Tapcart Boutique",
251
+ quantityAvailable: 11,
252
+ variantSku: "",
253
+ price: 59.989999999999995,
254
+ cost: {
255
+ totalAmount: {
256
+ amount: "239.96",
257
+ currencyCode: "USD",
258
+ },
259
+ },
260
+ discounts: [],
261
+ attributes: [],
262
+ quantity: 4,
263
+ },
264
+ {
265
+ discounts: [
266
+ {
267
+ code: "FALLRUG10",
268
+ amount: 0,
269
+ type: "manual",
270
+ },
271
+ ],
272
+ variantId: "43668884029631",
273
+ image: "https://cdn.shopify.com/s/files/1/0656/7939/2959/files/Screenshot2024-08-22at9.43.44AM_x500.png?v=1726022527",
274
+ title: "Fall Fancy Rug",
275
+ cost: {
276
+ totalAmount: {
277
+ amount: "220",
278
+ currencyCode: "USD",
279
+ },
280
+ },
281
+ id: "gid://shopify/CartLine/34775a95-d7c7-4b11-9f36-6ae923acfd9f?cart=Z2NwLXVzLXdlc3QxOjAxSlJYUDZRUk1CS1pSRllDSlNTNUgxWFQz",
282
+ productId: "7954296832191",
283
+ productVendor: "Emmys Tapcart Boutique",
284
+ selectedOptions: [
285
+ {
286
+ name: "Rug Padding",
287
+ value: "With Padding",
288
+ },
289
+ {
290
+ value: "2 x 3",
291
+ name: "Size",
292
+ },
293
+ ],
294
+ attributes: [],
295
+ featuredImage: "https://cdn.shopify.com/s/files/1/0656/7939/2959/files/Screenshot2024-08-22at9.44.04AM.png?v=1726022527",
296
+ compareAtPrice: 0,
297
+ quantity: 1,
298
+ totalAmount: 220,
299
+ currencyCode: "USD",
300
+ price: 220,
301
+ quantityAvailable: 99,
302
+ variantSku: "",
303
+ },
304
+ {
305
+ featuredImage: "https://cdn.shopify.com/s/files/1/0656/7939/2959/files/Screenshot2024-08-22at9.44.04AM.png?v=1726022527",
306
+ quantityAvailable: 99,
307
+ variantSku: "",
308
+ title: "Fall Fancy Rug",
309
+ productId: "7954296832191",
310
+ compareAtPrice: 0,
311
+ discounts: [
312
+ {
313
+ type: "manual",
314
+ code: "FALLRUG10",
315
+ amount: 22,
316
+ },
317
+ ],
318
+ id: "gid://shopify/CartLine/b642de72-78c4-4998-92da-5d4c4146057b?cart=Z2NwLXVzLXdlc3QxOjAxSlJYUDZRUk1CS1pSRllDSlNTNUgxWFQz",
319
+ attributes: [],
320
+ productVendor: "Emmys Tapcart Boutique",
321
+ image: "https://cdn.shopify.com/s/files/1/0656/7939/2959/files/Screenshot2024-08-22at9.43.44AM_x500.png?v=1726022527",
322
+ quantity: 1,
323
+ totalAmount: 198,
324
+ currencyCode: "USD",
325
+ selectedOptions: [
326
+ {
327
+ name: "Rug Padding",
328
+ value: "With Padding",
329
+ },
330
+ {
331
+ value: "2 x 3",
332
+ name: "Size",
333
+ },
334
+ ],
335
+ price: 220,
336
+ variantId: "43668884029631",
337
+ cost: {
338
+ totalAmount: {
339
+ amount: "198",
340
+ currencyCode: "USD",
341
+ },
342
+ },
343
+ },
344
+ ],
345
+ subtotal: 679.96,
346
+ discounts: ["Order10", "Fallrug10"],
347
+ // Added discountAllocations for testing
348
+ discountAllocations: [
349
+ {
350
+ targetType: DiscountApplicationTargetType.LineItem,
351
+ discountedAmount: { amount: "10.00", currencyCode: "USD" },
352
+ code: "ORDER10",
353
+ },
354
+ ],
355
+ // Added appliedGiftCards using the provided format
356
+ appliedGiftCards: [
357
+ {
358
+ id: "gid://shopify/AppliedGiftCard/3c208423-e985-46c7-bb34-656c59dc29b1",
359
+ lastCharacters: "oney",
360
+ amountUsed: { amount: "10.00", currencyCode: "USD" },
361
+ presentmentAmountUsed: { amount: "10.00", currencyCode: "USD" },
362
+ },
363
+ ],
364
+ };
365
+ const result = getVariablesCalculatedCartData(realWorldCart);
366
+ console.log(result);
367
+ // Test that the calculated data is correct
368
+ expect(result.orderAndLineItemDiscounts).toHaveLength(2);
369
+ expect(result.orderAndLineItemDiscounts[0].id).toBe("ORDER10");
370
+ expect(result.orderAndLineItemDiscounts[0].amount).toBe(10);
371
+ // Test applied gift cards
372
+ expect(result.appliedGiftCards).toHaveLength(1);
373
+ expect(result.appliedGiftCards[0].id).toBe("gid://shopify/AppliedGiftCard/3c208423-e985-46c7-bb34-656c59dc29b1");
374
+ expect(result.appliedGiftCards[0].amount).toBe(10);
375
+ expect(result.appliedGiftCards[0].name).toBe("Gift Card - oney");
376
+ // Test other calculated values
377
+ expect(result.discountsTotalAmount).toBe(32); // 22 + 10
378
+ expect(result.giftCardsTotalAmount).toBe(10);
379
+ // Test sales amount calculation - should be 4 * (99.99 - 59.99) = 4 * 40 = 160
380
+ expect(result.salesAmount).toBe(160);
381
+ // Total compare at price should include all items pricing
382
+ expect(result.totalCompareAtPrice).toBeGreaterThan(0);
383
+ // Free shipping isn't in the test data
384
+ expect(result.isFreeShipping).toBe(false);
385
+ });
386
+ });
387
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tapcart/mobile-components",
3
- "version": "0.8.48",
3
+ "version": "0.8.49",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "style": "dist/styles.css",
@@ -72,7 +72,7 @@
72
72
  "@radix-ui/react-toggle-group": "^1.0.4",
73
73
  "@tabler/icons-react": "^3.2.0",
74
74
  "@tanstack/react-virtual": "^3.13.6",
75
- "@tapcart/app-studio-sdk": "^2.1.1",
75
+ "@tapcart/app-studio-sdk": "^2.1.2",
76
76
  "@tapcart/search-client": "workspace:*",
77
77
  "apple-pay-button": "^1.1.7",
78
78
  "class-variance-authority": "^0.6.0",