-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.ts
More file actions
394 lines (351 loc) · 13.3 KB
/
Copy pathindex.test.ts
File metadata and controls
394 lines (351 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { I18n, CompositePrice, PriceInputMappings, PriceItem } from '../shared/types';
import {
orderWithCompositeItem,
orderWithCompositeItemResults,
orderWithMultiplePrices,
orderWithMultiplePricesResults,
initialOrderEntityData,
priceWithCorrectQuantity,
orderEntityDataWithEmptyLineItems,
invalidOrderEntityData,
} from './__tests__/orders.fixtures';
import { processOrderTableData } from './process-order-table-data';
import type { PriceDisplayType } from './types';
import { getHiddenAmountString, getPriceDisplayInJourneys, getQuantity, unitAmountApproved } from './utils';
const mockI18n = {
t: (key: string, fallback: string) => key || fallback,
language: 'de',
} as I18n;
beforeEach(() => {
vi.clearAllMocks();
});
describe('processOrderTableData', () => {
it('returns correctly', async () => {
const result = await processOrderTableData(orderWithCompositeItem as any, mockI18n);
expect(result.products.length).toBe(12);
expect(result.products[0]).toEqual(
expect.objectContaining({
quantity: 1,
price: expect.objectContaining({
quantity: '1',
quantity_billing_period: undefined,
}),
}),
);
expect(result.products[1]).toEqual(
expect.objectContaining({
quantity: 1,
price: expect.objectContaining({
quantity: '1',
quantity_billing_period: undefined,
}),
}),
);
expect(result.products[2]).toEqual(
expect.objectContaining({
quantity: 1,
price: expect.objectContaining({
quantity: '75 ',
quantity_billing_period: 'table_order.recurrences.billing_period.yearly',
}),
}),
);
expect(result.products[3]).toEqual(
expect.objectContaining({
quantity: 1,
price: expect.objectContaining({
quantity: '75 ',
quantity_billing_period: undefined,
}),
}),
);
expect(result.products[4]).toEqual(
expect.objectContaining({
quantity: 1,
price: expect.objectContaining({
quantity: '---',
quantity_billing_period: undefined,
}),
}),
);
expect(result.products[5]).toEqual(
expect.objectContaining({
quantity: 1,
price: expect.objectContaining({
quantity: '1',
quantity_billing_period: undefined,
}),
}),
);
expect(result.products[8]).toEqual(
expect.objectContaining({
price: expect.objectContaining({
unit_amount: 'show_as_on_request',
}),
}),
);
expect(result.products[11]).toEqual(
expect.objectContaining({
price: expect.objectContaining({
unit_amount: 'show_as_on_request',
}),
}),
);
expect(result.total_details.recurrences).toEqual(orderWithCompositeItemResults.total_details.recurrences);
});
it('leaves the line item unit_amount_net as a raw numeric value (mirroring unit_amount_gross)', async () => {
const result = await processOrderTableData(orderWithCompositeItem as any, mockI18n);
const item = result.products[7];
// The line item keeps the raw numeric net amount, it is NOT overwritten with a formatted string.
expect(item.unit_amount_net).toBe(65000);
expect(typeof item.unit_amount_net).toBe('number');
// The formatted value is still exposed under price.unit_amount_net for templates that render it.
expect(item.price.unit_amount_net).toBe('650,00\xa0€');
});
it('returns correctly the tax details', async () => {
const result = await processOrderTableData(orderWithMultiplePrices as any, mockI18n);
expect(result.total_details.recurrences).toEqual(orderWithMultiplePricesResults.total_details.recurrences);
});
it('returns correct data avoiding reprocessing of flatten items', async () => {
// when
const dataWithFlattenLineItems1 = await processOrderTableData(initialOrderEntityData as any, mockI18n);
// then
expect(dataWithFlattenLineItems1.products.length).toBe(12);
expect(dataWithFlattenLineItems1.products[1].price).toEqual(expect.objectContaining(priceWithCorrectQuantity));
// when
const dataWithFlattenLineItems2 = await processOrderTableData(dataWithFlattenLineItems1 as any, mockI18n);
// then
expect(dataWithFlattenLineItems2.products.length).toBe(12);
expect(dataWithFlattenLineItems2.products[1].price).toEqual(expect.objectContaining(priceWithCorrectQuantity));
});
it('return original data/skip processing if line items are empty', async () => {
// when
const data = await processOrderTableData(orderEntityDataWithEmptyLineItems as any, mockI18n);
// then
expect(data).toEqual(orderEntityDataWithEmptyLineItems);
});
it('return original data/skip processing if line items are invalid', async () => {
// when
const data = await processOrderTableData(invalidOrderEntityData as any, mockI18n);
// then
expect(data).toEqual(invalidOrderEntityData);
});
});
describe('getQuantity', () => {
const baseVariableItem = {
price_id: 'price_id',
_price: {
pricing_model: 'per_unit',
variable_price: true,
},
};
const baseNotVariableItem = {
price_id: 'price_id',
_price: {
pricing_model: 'per_unit',
variable_price: false,
},
};
const baseMappings = [{ price_id: 'price_id', value: 75 }];
const zeroBaseMappings = [{ price_id: 'price_id', value: 0 }];
const wrongBaseMappings = [{ price_id: 'price_id_1', value: 75 }];
it.each`
baseItem | parentItem | quantity | parentQuantity | mappings | expected
${baseNotVariableItem} | ${undefined} | ${1} | ${1} | ${undefined} | ${'1'}
${baseNotVariableItem} | ${undefined} | ${2} | ${2} | ${undefined} | ${'2'}
${baseNotVariableItem} | ${baseVariableItem} | ${1} | ${1} | ${undefined} | ${'1'}
${baseNotVariableItem} | ${baseVariableItem} | ${2} | ${2} | ${undefined} | ${'2 x 2'}
${baseVariableItem} | ${undefined} | ${1} | ${1} | ${undefined} | ${'---'}
${baseVariableItem} | ${undefined} | ${2} | ${2} | ${undefined} | ${'2 x ---'}
${baseVariableItem} | ${undefined} | ${1} | ${1} | ${baseMappings} | ${'75 '}
${baseVariableItem} | ${undefined} | ${2} | ${2} | ${baseMappings} | ${'2 x 75 '}
${baseVariableItem} | ${baseVariableItem} | ${1} | ${1} | ${undefined} | ${'---'}
${baseVariableItem} | ${baseVariableItem} | ${2} | ${2} | ${undefined} | ${'2 x ---'}
${baseVariableItem} | ${baseVariableItem} | ${1} | ${1} | ${baseMappings} | ${'75 '}
${baseVariableItem} | ${baseVariableItem} | ${2} | ${2} | ${baseMappings} | ${'2 x 75 '}
${baseVariableItem} | ${baseVariableItem} | ${2} | ${2} | ${wrongBaseMappings} | ${'2 x ---'}
${baseVariableItem} | ${baseVariableItem} | ${2} | ${2} | ${zeroBaseMappings} | ${'2 x 0 '}
${baseVariableItem} | ${baseVariableItem} | ${undefined} | ${2} | ${baseMappings} | ${'2 x 75 '}
${baseNotVariableItem} | ${baseNotVariableItem} | ${6} | ${2} | ${undefined} | ${'2 x 6'}
`(
'returns $expected for the inputs',
async ({
baseItem,
parentItem,
quantity,
parentQuantity,
mappings,
expected,
}: {
baseItem: PriceItem;
parentItem: PriceItem | undefined;
quantity: number;
parentQuantity: number;
mappings: PriceInputMappings;
expected: string;
}) => {
const mappedParentItem = parentItem && {
...parentItem,
...(parentQuantity && { quantity: parentQuantity }),
...(mappings && { price_mappings: mappings }),
};
const item = {
...baseItem,
...(quantity && { quantity }),
...(mappings && { price_mappings: mappings }),
};
const result = getQuantity(item, mappedParentItem);
expect(result).toBe(expected);
},
);
});
describe('unitAmountApproved', () => {
it('should return true when item._price.price_display_in_journeys is not "show_as_on_request"', async () => {
const item = {
_price: {
price_display_in_journeys: 'show_price',
},
};
expect(unitAmountApproved(item as any)).toBe(true);
});
it('should return true when item.is_composite_price is true and item_components has "show_price" price_display_in_journeys', async () => {
const item = {
is_composite_price: true,
item_components: [
{
_price: {
price_display_in_journeys: 'show_price',
},
},
{
_price: {
price_display_in_journeys: 'show_price',
},
},
],
};
expect(unitAmountApproved(item as any)).toBe(true);
});
it('should return true when item.on_request_approved is true', async () => {
const item = {
on_request_approved: true,
};
expect(unitAmountApproved(item as any)).toBe(true);
});
it('should return true when item.parent_item.on_request_approved is true', async () => {
const item = {
parent_item: {
on_request_approved: true,
},
};
expect(unitAmountApproved(item as any)).toBe(true);
});
it('should return false when all conditions are false', async () => {
const item = {
_price: {
price_display_in_journeys: 'show_as_on_request',
},
is_composite_price: true,
item_components: [
{
_price: {
price_display_in_journeys: 'show_as_on_request',
},
},
],
on_request_approved: false,
parent_item: {
on_request_approved: false,
},
};
expect(unitAmountApproved(item as any)).toBe(false);
});
});
describe('getHiddenAmountString', () => {
it('should return "---" for a composite price with no matching component', async () => {
const priceItem = {
is_composite_price: true,
_price: {
price_display_in_journeys: 'show_as_on_request',
},
item_components: [
{
_price: {
price_display_in_journeys: 'other_value',
},
},
],
} as CompositePrice;
const result = getHiddenAmountString(mockI18n.t, getPriceDisplayInJourneys(priceItem));
expect(result).toBe('show_as_on_request');
});
it('should return the translation for a composite price', async () => {
const priceItem = {
is_composite_price: true,
_price: {
price_display_in_journeys: 'show_as_on_request',
},
item_components: [
{
_price: {
price_display_in_journeys: 'other_value',
},
},
{
_price: {
price_display_in_journeys: 'show_as_starting_price',
},
},
],
} as CompositePrice;
const result = getHiddenAmountString(mockI18n.t, getPriceDisplayInJourneys(priceItem));
expect(result).toBe('show_as_on_request');
});
it('should return the translation for a non-composite price with a parentItem', async () => {
const priceItem = {
pricing_model: 'per_unit',
_price: {
price_display_in_journeys: 'other_value' as PriceDisplayType,
pricing_model: 'per_unit',
},
parent_item: {
_price: {
price_display_in_journeys: 'show_as_on_request',
},
},
} as PriceItem;
const result = getHiddenAmountString(mockI18n.t, getPriceDisplayInJourneys(priceItem));
expect(result).toBe('show_as_on_request');
});
it('should return the translation for a non-composite price with NO parentItem', async () => {
const priceItem = {
_price: {
price_display_in_journeys: 'show_as_on_request',
},
} as PriceItem;
const result = getHiddenAmountString(mockI18n.t, getPriceDisplayInJourneys(priceItem));
const resultWithAmount = getHiddenAmountString(mockI18n.t, getPriceDisplayInJourneys(priceItem), '€123.45');
expect(result).toBe('show_as_on_request');
expect(resultWithAmount).toBe('show_as_on_request');
});
it('should return the correct translations when price is starting at', async () => {
const priceItem = {
_price: {
price_display_in_journeys: 'show_as_starting_price',
},
} as PriceItem;
const resultWithFormattedString = getHiddenAmountString(
mockI18n.t,
getPriceDisplayInJourneys(priceItem),
'€123.45',
);
const resultWithZeroNumber = getHiddenAmountString(mockI18n.t, getPriceDisplayInJourneys(priceItem), 0);
const resultWithNumber = getHiddenAmountString(mockI18n.t, getPriceDisplayInJourneys(priceItem), 123);
const resultWithUndefined = getHiddenAmountString(mockI18n.t, getPriceDisplayInJourneys(priceItem), undefined);
expect(resultWithFormattedString).toBe('show_as_starting_price €123.45');
expect(resultWithZeroNumber).toBe('show_as_starting_price 0');
expect(resultWithNumber).toBe('show_as_starting_price 123');
expect(resultWithUndefined).toBe('show_as_starting_price');
});
});