1
1
import { calculatePrices } from './calculator' ;
2
2
3
3
test ( 'no items' , ( ) => {
4
- const price = calculatePrices ( null , "USA" , "USD" , null , [ ] ) ;
4
+ const price = calculatePrices ( null , null , "USA" , "USD" , null , [ ] ) ;
5
5
expect ( price . total ) . toBe ( 0 ) ;
6
6
} ) ;
7
7
8
8
test ( 'no taxes' , ( ) => {
9
- const price = calculatePrices ( null , "USA" , "USD" , null , [ { price : { cents : 100 } , type : "test" } ] ) ;
9
+ const price = calculatePrices ( null , null , "USA" , "USD" , null , [ { price : { cents : 100 } , type : "test" } ] ) ;
10
10
expect ( price . subtotal ) . toBe ( 100 ) ;
11
11
expect ( price . taxes ) . toBe ( 0 ) ;
12
12
expect ( price . discount ) . toBe ( 0 ) ;
13
13
expect ( price . total ) . toBe ( 100 ) ;
14
14
} ) ;
15
15
16
16
test ( 'fixed vat' , ( ) => {
17
- const price = calculatePrices ( null , "USA" , "USD" , null , [ { price : { cents : 100 } , vat : 9 , type : "test" } ] ) ;
17
+ const price = calculatePrices ( null , null , "USA" , "USD" , null , [ { price : { cents : 100 } , vat : 9 , type : "test" } ] ) ;
18
18
expect ( price . subtotal ) . toBe ( 100 ) ;
19
19
expect ( price . taxes ) . toBe ( 9 ) ;
20
20
expect ( price . discount ) . toBe ( 0 ) ;
21
21
expect ( price . total ) . toBe ( 109 ) ;
22
22
} )
23
23
24
24
test ( 'fixed vat when prices include taxes' , ( ) => {
25
- const price = calculatePrices ( { prices_include_taxes : true } , "USA" , "USD" , null , [ { price : { cents : 100 } , vat : 9 , type : "test" } ] ) ;
25
+ const price = calculatePrices ( { prices_include_taxes : true } , null , "USA" , "USD" , null , [ { price : { cents : 100 } , vat : 9 , type : "test" } ] ) ;
26
26
expect ( price . subtotal ) . toBe ( 92 ) ;
27
27
expect ( price . taxes ) . toBe ( 8 ) ;
28
28
expect ( price . discount ) . toBe ( 0 ) ;
@@ -31,7 +31,7 @@ test('fixed vat when prices include taxes', () => {
31
31
32
32
test ( 'country based VAT' , ( ) => {
33
33
const settings = { taxes : [ { percentage : 21 , product_types : [ "test" ] , countries : [ "USA" ] } ] } ;
34
- const price = calculatePrices ( settings , "USA" , "USD" , null , [ { price : { cents : 100 } , type : "test" } ] ) ;
34
+ const price = calculatePrices ( settings , null , "USA" , "USD" , null , [ { price : { cents : 100 } , type : "test" } ] ) ;
35
35
expect ( price . subtotal ) . toBe ( 100 ) ;
36
36
expect ( price . taxes ) . toBe ( 21 ) ;
37
37
expect ( price . discount ) . toBe ( 0 ) ;
@@ -40,31 +40,31 @@ test('country based VAT', () => {
40
40
41
41
test ( 'country based VAT when prices include taxes' , ( ) => {
42
42
const settings = { prices_include_taxes : true , taxes : [ { percentage : 21 , product_types : [ "test" ] , countries : [ "USA" ] } ] } ;
43
- const price = calculatePrices ( settings , "USA" , "USD" , null , [ { price : { cents : 100 } , type : "test" } ] ) ;
43
+ const price = calculatePrices ( settings , null , "USA" , "USD" , null , [ { price : { cents : 100 } , type : "test" } ] ) ;
44
44
expect ( price . subtotal ) . toBe ( 83 ) ;
45
45
expect ( price . taxes ) . toBe ( 17 ) ;
46
46
expect ( price . discount ) . toBe ( 0 ) ;
47
47
expect ( price . total ) . toBe ( 100 ) ;
48
48
} ) ;
49
49
50
50
test ( 'coupon with no taxes' , ( ) => {
51
- const price = calculatePrices ( null , "USA" , "USD" , { percentage : 10 , product_types : [ "test" ] } , [ { price : { cents : 100 } , type : "test" } ] ) ;
51
+ const price = calculatePrices ( null , null , "USA" , "USD" , { percentage : 10 , product_types : [ "test" ] } , [ { price : { cents : 100 } , type : "test" } ] ) ;
52
52
expect ( price . subtotal ) . toBe ( 100 ) ;
53
53
expect ( price . taxes ) . toBe ( 0 ) ;
54
54
expect ( price . discount ) . toBe ( 10 ) ;
55
55
expect ( price . total ) . toBe ( 90 ) ;
56
56
} ) ;
57
57
58
58
test ( 'coupon with vat' , ( ) => {
59
- const price = calculatePrices ( null , "USA" , "USD" , { percentage : 10 , product_types : [ "test" ] } , [ { price : { cents : 100 } , vat : 9 , type : "test" } ] ) ;
59
+ const price = calculatePrices ( null , null , "USA" , "USD" , { percentage : 10 , product_types : [ "test" ] } , [ { price : { cents : 100 } , vat : 9 , type : "test" } ] ) ;
60
60
expect ( price . subtotal ) . toBe ( 100 ) ;
61
61
expect ( price . taxes ) . toBe ( 9 ) ;
62
62
expect ( price . discount ) . toBe ( 10 ) ;
63
63
expect ( price . total ) . toBe ( 99 ) ;
64
64
} ) ;
65
65
66
66
test ( 'coupon with vat when prices include taxes' , ( ) => {
67
- const price = calculatePrices ( { prices_include_taxes : true } , "USA" , "USD" , { percentage : 10 , product_types : [ "test" ] } , [ { price : { cents : 100 } , vat : 9 , type : "test" } ] ) ;
67
+ const price = calculatePrices ( { prices_include_taxes : true } , null , "USA" , "USD" , { percentage : 10 , product_types : [ "test" ] } , [ { price : { cents : 100 } , vat : 9 , type : "test" } ] ) ;
68
68
expect ( price . subtotal ) . toBe ( 92 ) ;
69
69
expect ( price . taxes ) . toBe ( 8 ) ;
70
70
expect ( price . discount ) . toBe ( 10 ) ;
@@ -83,7 +83,7 @@ test('pricing items', () => {
83
83
countries : [ "DE" ]
84
84
} ]
85
85
} ;
86
- const price = calculatePrices ( settings , "DE" , "EUR" , null , [ {
86
+ const price = calculatePrices ( settings , null , "DE" , "EUR" , null , [ {
87
87
price : { cents : 100 , items : [ { cents : 80 , type : "book" } , { cents : 20 , type : "ebook" } ] } ,
88
88
type : "book"
89
89
} ] ) ;
@@ -94,9 +94,31 @@ test('pricing items', () => {
94
94
} ) ;
95
95
96
96
test ( 'quantity' , ( ) => {
97
- const price = calculatePrices ( null , "USA" , "USD" , { percentage : 10 , product_types : [ "test" ] } , [ { price : { cents : 100 } , quantity : 2 , vat : 9 , type : "test" } ] ) ;
97
+ const price = calculatePrices ( null , null , "USA" , "USD" , { percentage : 10 , product_types : [ "test" ] } , [ { price : { cents : 100 } , quantity : 2 , vat : 9 , type : "test" } ] ) ;
98
98
expect ( price . subtotal ) . toBe ( 200 ) ;
99
99
expect ( price . taxes ) . toBe ( 18 ) ;
100
100
expect ( price . discount ) . toBe ( 20 ) ;
101
101
expect ( price . total ) . toBe ( 198 ) ;
102
- } )
102
+ } ) ;
103
+
104
+ test ( 'member discounts' , ( ) => {
105
+ const settings = {
106
+ "member_discounts" : [
107
+ {
108
+ "claims" : { "app_metadata.subscriptions.members" : "supporter" } ,
109
+ "percentage" : 10
110
+ }
111
+ ]
112
+ } ;
113
+ const price = calculatePrices (
114
+ settings ,
115
+ { app_metadata : { subscriptions : { members : "supporter" } } } ,
116
+ "USA" , "USD" , null ,
117
+ [ { price : { cents : 100 } , type : "test" } ]
118
+ ) ;
119
+ expect ( price . subtotal ) . toBe ( 100 ) ;
120
+ expect ( price . taxes ) . toBe ( 0 ) ;
121
+ expect ( price . discount ) . toBe ( 10 ) ;
122
+ expect ( price . total ) . toBe ( 90 ) ;
123
+
124
+ } ) ;
0 commit comments