@unchainedshop/core 3.1.1 → 3.1.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.
- package/package.json +4 -5
- package/src/directors/BaseDiscountAdapter.test.ts +20 -17
- package/src/directors/BasePricingSheet.test.ts +6 -4
- package/src/directors/MessagingDirector.test.ts +4 -1
- package/src/directors/ProductPricingSheet.test.ts +29 -27
- package/src/directors/periodForReferenceDate.test.ts +5 -3
- package/jest.config.js +0 -17
- package/tests/core.test.ts +0 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/core",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.2",
|
|
4
4
|
"main": "lib/core-index.js",
|
|
5
5
|
"types": "lib/core-index.d.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
"build": "tsc -b",
|
|
10
10
|
"prepublishOnly": "npm run clean && npm run build",
|
|
11
11
|
"watch": "tsc -w ",
|
|
12
|
-
"test": "
|
|
13
|
-
"test:watch": "
|
|
12
|
+
"test": "tsx --test",
|
|
13
|
+
"test:watch": "tsx --test --watch"
|
|
14
14
|
},
|
|
15
15
|
"repository": {
|
|
16
16
|
"type": "git",
|
|
@@ -54,8 +54,7 @@
|
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
56
|
"@types/node": "^22.13.4",
|
|
57
|
-
"
|
|
58
|
-
"ts-jest": "^29.2.5",
|
|
57
|
+
"tsx": "^4.19.2",
|
|
59
58
|
"typescript": "^5.7.3"
|
|
60
59
|
}
|
|
61
60
|
}
|
|
@@ -1,49 +1,52 @@
|
|
|
1
|
+
import { describe, it } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
1
3
|
import { BaseDiscountAdapter } from './BaseDiscountAdapter.js';
|
|
2
4
|
|
|
3
5
|
describe('BaseDiscountAdapter instantiation', () => {
|
|
4
6
|
const adapter = BaseDiscountAdapter;
|
|
5
7
|
const context = null as any;
|
|
6
8
|
it('should initialize with default values', async () => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
isValidForSystemTriggering
|
|
13
|
-
reserve
|
|
14
|
-
release
|
|
15
|
-
isValidForCodeTriggering
|
|
16
|
-
discountForPricingAdapterKey
|
|
17
|
-
|
|
9
|
+
assert.equal(adapter.orderIndex, 0);
|
|
10
|
+
assert.equal(typeof adapter.log, 'function');
|
|
11
|
+
assert.equal(await adapter.isManualAdditionAllowed(''), false);
|
|
12
|
+
assert.equal(await adapter.isManualRemovalAllowed(), false);
|
|
13
|
+
assert.deepStrictEqual(Object.keys(await adapter.actions(context)), [
|
|
14
|
+
'isValidForSystemTriggering',
|
|
15
|
+
'reserve',
|
|
16
|
+
'release',
|
|
17
|
+
'isValidForCodeTriggering',
|
|
18
|
+
'discountForPricingAdapterKey',
|
|
19
|
+
]);
|
|
18
20
|
});
|
|
19
21
|
|
|
20
22
|
describe('actions', () => {
|
|
21
23
|
it('isValidForSystemTriggering', async () => {
|
|
22
24
|
const result = await adapter.actions(context);
|
|
23
|
-
|
|
25
|
+
assert.equal(await result.isValidForSystemTriggering(), false);
|
|
24
26
|
});
|
|
25
27
|
|
|
26
28
|
it('reserve', async () => {
|
|
27
29
|
const result = await adapter.actions(context);
|
|
28
|
-
|
|
30
|
+
assert.deepEqual(await result.reserve({ code: '' }), {});
|
|
29
31
|
});
|
|
30
32
|
|
|
31
33
|
it('release', async () => {
|
|
32
34
|
const result = await adapter.actions(context);
|
|
33
|
-
|
|
35
|
+
assert.equal(await result.release(), null);
|
|
34
36
|
});
|
|
35
37
|
|
|
36
38
|
it('isValidForCodeTriggering', async () => {
|
|
37
39
|
const result = await adapter.actions(context);
|
|
38
|
-
|
|
40
|
+
assert.equal(await result.isValidForCodeTriggering({ code: '' }), false);
|
|
39
41
|
});
|
|
40
42
|
|
|
41
43
|
it('discountForPricingAdapterKey', async () => {
|
|
42
44
|
const result = await adapter.actions(context);
|
|
43
45
|
const calculationSheet = {};
|
|
44
|
-
|
|
46
|
+
assert.equal(
|
|
45
47
|
await result.discountForPricingAdapterKey({ calculationSheet, pricingAdapterKey: 'key' } as any),
|
|
46
|
-
|
|
48
|
+
null,
|
|
49
|
+
);
|
|
47
50
|
});
|
|
48
51
|
});
|
|
49
52
|
});
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { describe, it } from 'node:test';
|
|
1
2
|
import { resolveRatioAndTaxDivisorForPricingSheet } from './BasePricingSheet';
|
|
3
|
+
import assert from 'node:assert';
|
|
2
4
|
|
|
3
5
|
describe('resolveRatioAndTaxDivisorForPricingSheet', () => {
|
|
4
6
|
it('total is 0 and pricing is provided', () => {
|
|
@@ -8,7 +10,7 @@ describe('resolveRatioAndTaxDivisorForPricingSheet', () => {
|
|
|
8
10
|
net: () => 10,
|
|
9
11
|
};
|
|
10
12
|
const result = resolveRatioAndTaxDivisorForPricingSheet(pricing, 0);
|
|
11
|
-
|
|
13
|
+
assert.deepStrictEqual(result, { ratio: 1, taxDivisor: 1 });
|
|
12
14
|
});
|
|
13
15
|
|
|
14
16
|
it('gross - tax is 0', () => {
|
|
@@ -18,7 +20,7 @@ describe('resolveRatioAndTaxDivisorForPricingSheet', () => {
|
|
|
18
20
|
net: () => 0,
|
|
19
21
|
};
|
|
20
22
|
const result = resolveRatioAndTaxDivisorForPricingSheet(pricing, 20);
|
|
21
|
-
|
|
23
|
+
assert.deepStrictEqual(result, { ratio: 0, taxDivisor: 0 });
|
|
22
24
|
});
|
|
23
25
|
|
|
24
26
|
it('gross - tax is not 0', () => {
|
|
@@ -28,7 +30,7 @@ describe('resolveRatioAndTaxDivisorForPricingSheet', () => {
|
|
|
28
30
|
net: () => 10,
|
|
29
31
|
};
|
|
30
32
|
const result = resolveRatioAndTaxDivisorForPricingSheet(pricing, 20);
|
|
31
|
-
|
|
33
|
+
assert.deepStrictEqual(result, { ratio: 1, taxDivisor: 2 });
|
|
32
34
|
});
|
|
33
35
|
|
|
34
36
|
it('taxSum is 0', () => {
|
|
@@ -38,6 +40,6 @@ describe('resolveRatioAndTaxDivisorForPricingSheet', () => {
|
|
|
38
40
|
net: () => 20,
|
|
39
41
|
};
|
|
40
42
|
const result = resolveRatioAndTaxDivisorForPricingSheet(pricing, 20);
|
|
41
|
-
|
|
43
|
+
assert.deepStrictEqual(result, { ratio: 1, taxDivisor: 1 });
|
|
42
44
|
});
|
|
43
45
|
});
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { beforeEach, describe, it } from 'node:test';
|
|
2
|
+
import assert from 'node:assert';
|
|
1
3
|
import { ProductPricingSheet } from './ProductPricingSheet.js';
|
|
2
4
|
|
|
3
5
|
const TAX = { category: 'TAX', amount: 50, isNetPrice: false, isTaxable: false };
|
|
@@ -40,90 +42,90 @@ describe('ProductPricingSheet', () => {
|
|
|
40
42
|
});
|
|
41
43
|
|
|
42
44
|
it('should return an object that implements the IProductPricingSheet interface', () => {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
45
|
+
assert(pricingSheet);
|
|
46
|
+
assert.equal(pricingSheet.quantity, 2);
|
|
47
|
+
assert.equal(pricingSheet.currency, 'CHF');
|
|
48
|
+
assert.equal(typeof pricingSheet.addItem, 'function');
|
|
49
|
+
assert.equal(typeof pricingSheet.addDiscount, 'function');
|
|
50
|
+
assert.equal(typeof pricingSheet.addTax, 'function');
|
|
51
|
+
assert.equal(typeof pricingSheet.taxSum, 'function');
|
|
52
|
+
assert.equal(typeof pricingSheet.unitPrice, 'function');
|
|
53
|
+
assert.equal(typeof pricingSheet.discountPrices, 'function');
|
|
52
54
|
});
|
|
53
55
|
|
|
54
56
|
it('gross() should return the GROSS sum of all ProductPricingCalculation', () => {
|
|
55
|
-
|
|
57
|
+
assert.equal(pricingSheet.gross(), 535);
|
|
56
58
|
});
|
|
57
59
|
|
|
58
60
|
it('net() should return the NET sum of all ProductPricingCalculation (i.e before TAX)', () => {
|
|
59
|
-
|
|
61
|
+
assert.equal(pricingSheet.net(), 460);
|
|
60
62
|
});
|
|
61
63
|
|
|
62
64
|
describe('total()', () => {
|
|
63
65
|
it('should return sum of all ProductPricingCalculations ', () => {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
+
assert.deepEqual(pricingSheet.total(), { amount: 535, currency: 'CHF' });
|
|
67
|
+
assert.deepEqual(pricingSheet.total(), { amount: pricingSheet.gross(), currency: 'CHF' });
|
|
66
68
|
});
|
|
67
69
|
|
|
68
70
|
it('should return sum of all ProductPricingCalculations ', () => {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
+
assert.deepEqual(pricingSheet.total({ useNetPrice: true }), { amount: 460, currency: 'CHF' });
|
|
72
|
+
assert.deepEqual(pricingSheet.total({ useNetPrice: true }), {
|
|
71
73
|
amount: pricingSheet.net(),
|
|
72
74
|
currency: 'CHF',
|
|
73
75
|
});
|
|
74
76
|
});
|
|
75
77
|
|
|
76
78
|
it('should return sum of all ProductPricingCalculations for the provided category ', () => {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
79
|
+
assert.deepEqual(pricingSheet.total({ category: 'ITEM' }), { amount: 400, currency: 'CHF' });
|
|
80
|
+
assert.deepEqual(pricingSheet.total({ category: 'DISCOUNT' }), { amount: 60, currency: 'CHF' });
|
|
81
|
+
assert.deepEqual(pricingSheet.total({ category: 'TAX' }), { amount: 75, currency: 'CHF' });
|
|
80
82
|
});
|
|
81
83
|
});
|
|
82
84
|
|
|
83
85
|
describe('isValid()', () => {
|
|
84
86
|
it('should return true if there is at least 1 registered ProductPricingCalculation ', () => {
|
|
85
|
-
|
|
87
|
+
assert.equal(pricingSheet.isValid(), true);
|
|
86
88
|
});
|
|
87
89
|
it('should return false if there is no registered ProductPricingCalculation ', () => {
|
|
88
90
|
const sheet = ProductPricingSheet({ ...pricingSheetParams, calculation: [] });
|
|
89
|
-
|
|
91
|
+
assert.equal(sheet.isValid(), false);
|
|
90
92
|
});
|
|
91
93
|
});
|
|
92
94
|
|
|
93
95
|
describe('getRawPricingSheet', () => {
|
|
94
96
|
it('should return all registered ProductPricingCalculation', () => {
|
|
95
|
-
|
|
97
|
+
assert.deepEqual(pricingSheet.getRawPricingSheet(), calculations);
|
|
96
98
|
});
|
|
97
99
|
});
|
|
98
100
|
|
|
99
101
|
describe('taxSum', () => {
|
|
100
102
|
it('should return the sum of TAX calculation registered on the adapter', () => {
|
|
101
|
-
|
|
103
|
+
assert.equal(pricingSheet.taxSum(), 75);
|
|
102
104
|
});
|
|
103
105
|
});
|
|
104
106
|
|
|
105
107
|
describe('unitPrice', () => {
|
|
106
108
|
it('should return the GROSS sum for a product price useNetPrice:false', () => {
|
|
107
|
-
|
|
109
|
+
assert.deepEqual(pricingSheet.unitPrice({ useNetPrice: false }), { amount: 268, currency: 'CHF' });
|
|
108
110
|
});
|
|
109
111
|
|
|
110
112
|
it('should return the NET sum for a product price when useNetPrice:true', () => {
|
|
111
|
-
|
|
113
|
+
assert.deepEqual(pricingSheet.unitPrice({ useNetPrice: true }), { amount: 230, currency: 'CHF' });
|
|
112
114
|
});
|
|
113
115
|
});
|
|
114
116
|
|
|
115
117
|
describe('discountPrices', () => {
|
|
116
118
|
it('should return the sum of all discounts registered on the price sheet based on discountId', () => {
|
|
117
|
-
|
|
119
|
+
assert.deepEqual(pricingSheet.discountPrices('for-all'), [
|
|
118
120
|
{ amount: 40, currency: 'CHF', discountId: 'for-all' },
|
|
119
121
|
]);
|
|
120
|
-
|
|
122
|
+
assert.deepEqual(pricingSheet.discountPrices('special'), [
|
|
121
123
|
{ amount: 20, currency: 'CHF', discountId: 'special' },
|
|
122
124
|
]);
|
|
123
125
|
});
|
|
124
126
|
|
|
125
127
|
it('should return empty array if discount with the provided discountId is not found', () => {
|
|
126
|
-
|
|
128
|
+
assert.deepEqual(pricingSheet.discountPrices('non-existing'), []);
|
|
127
129
|
});
|
|
128
130
|
});
|
|
129
131
|
});
|
|
@@ -1,21 +1,23 @@
|
|
|
1
|
+
import { describe, it } from 'node:test';
|
|
1
2
|
import { periodForReferenceDate } from './EnrollmentAdapter.js';
|
|
3
|
+
import assert from 'node:assert';
|
|
2
4
|
|
|
3
5
|
describe('periodForReferenceDate', () => {
|
|
4
6
|
it('Should return 1 week interval from When passed a given date', () => {
|
|
5
|
-
|
|
7
|
+
assert.deepStrictEqual(periodForReferenceDate(new Date('2022-12-03T17:00:00.000Z')), {
|
|
6
8
|
start: new Date('2022-12-03T17:00:00.000Z'),
|
|
7
9
|
end: new Date('2022-12-10T17:00:00.000Z'),
|
|
8
10
|
});
|
|
9
11
|
});
|
|
10
12
|
it('Should return 2 week interval from When passed 2 as interval', () => {
|
|
11
|
-
|
|
13
|
+
assert.deepStrictEqual(periodForReferenceDate(new Date('2022-12-03T17:00:00.000Z'), 2), {
|
|
12
14
|
start: new Date('2022-12-03T17:00:00.000Z'),
|
|
13
15
|
end: new Date('2022-12-17T17:00:00.000Z'),
|
|
14
16
|
});
|
|
15
17
|
});
|
|
16
18
|
|
|
17
19
|
it('Should return 2 HOURS when interval is set to HOURS', () => {
|
|
18
|
-
|
|
20
|
+
assert.deepStrictEqual(periodForReferenceDate(new Date('2022-12-03T17:00:00.000Z'), 2, 'HOURS'), {
|
|
19
21
|
start: new Date('2022-12-03T17:00:00.000Z'),
|
|
20
22
|
end: new Date('2022-12-03T19:00:00.000Z'),
|
|
21
23
|
});
|
package/jest.config.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
|
|
2
|
-
export default {
|
|
3
|
-
preset: 'ts-jest/presets/default-esm', // or other ESM presets
|
|
4
|
-
testEnvironment: 'node',
|
|
5
|
-
extensionsToTreatAsEsm: ['.ts'],
|
|
6
|
-
transform: {
|
|
7
|
-
'^.+\\.tsx?$': [
|
|
8
|
-
'ts-jest',
|
|
9
|
-
{
|
|
10
|
-
useESM: true,
|
|
11
|
-
},
|
|
12
|
-
],
|
|
13
|
-
},
|
|
14
|
-
moduleNameMapper: {
|
|
15
|
-
'^(\\.{1,2}/.*)\\.js$': '$1',
|
|
16
|
-
},
|
|
17
|
-
};
|