@@ -11,6 +11,7 @@ const consoleErrorSpy = jest.spyOn(console, "error");
11
11
12
12
import { useCustomerOrders } from "../src/hooks/useCustomerOrders" ;
13
13
import { prepareRootContextMock } from "./contextRunner" ;
14
+ import { EntityResult , Order } from "@shopware-pwa/commons" ;
14
15
describe ( "Composables - useCustomerOrders" , ( ) => {
15
16
const rootContextMock = prepareRootContextMock ( ) ;
16
17
@@ -35,17 +36,19 @@ describe("Composables - useCustomerOrders", () => {
35
36
describe ( "methods" , ( ) => {
36
37
describe ( "loadOrders" , ( ) => {
37
38
it ( "should load customer's orders from different endpoint" , async ( ) => {
38
- const ordersResponse = [
39
- {
40
- id : "12345" ,
41
- orderNumber : "100123" ,
42
- } ,
43
- ] ;
39
+ const ordersResponse = {
40
+ elements : [
41
+ {
42
+ id : "12345" ,
43
+ orderNumber : "100123" ,
44
+ } ,
45
+ ]
46
+ } ;
44
47
mockedApiClient . getCustomerOrders . mockResolvedValueOnce (
45
48
ordersResponse as any
46
49
) ;
47
50
const { orders, loadOrders } = useCustomerOrders ( ) ;
48
- expect ( orders . value ) . toBeNull ( ) ;
51
+ expect ( orders . value ) . toStrictEqual ( [ ] ) ;
49
52
await loadOrders ( ) ;
50
53
expect ( orders . value ) . toHaveLength ( 1 ) ;
51
54
} ) ;
@@ -65,5 +68,197 @@ describe("Composables - useCustomerOrders", () => {
65
68
expect ( orderDetails ) . toBe ( orderResponse ) ;
66
69
} ) ;
67
70
} ) ;
71
+
72
+ describe ( "getTotal" , ( ) => {
73
+ it ( "should return 0 as default" , ( ) => {
74
+ const { getTotal } = useCustomerOrders ( ) ;
75
+ expect ( getTotal . value ) . toEqual ( 0 ) ;
76
+ } ) ;
77
+
78
+ it ( "should return 0 when there is no orders" , ( ) => {
79
+ const ordersResponse = {
80
+ elements : [ ]
81
+ } ;
82
+ mockedApiClient . getCustomerOrders . mockResolvedValueOnce (
83
+ ordersResponse as any
84
+ ) ;
85
+ const { getTotal } = useCustomerOrders ( ) ;
86
+
87
+ expect ( getTotal . value ) . toEqual ( 0 ) ;
88
+ } ) ;
89
+
90
+ it ( "should return total same value with count field" , async ( ) => {
91
+ const ordersResponse = {
92
+ aggregations : {
93
+ 'count-id' : {
94
+ count : 20
95
+ }
96
+ } ,
97
+ elements : Array ( 20 ) . fill ( {
98
+ id : "11111" ,
99
+ orderNumber : "100120" ,
100
+ } )
101
+ } ;
102
+ mockedApiClient . getCustomerOrders . mockResolvedValueOnce (
103
+ ordersResponse as any
104
+ ) ;
105
+
106
+ const { getTotal, loadOrders } = useCustomerOrders ( ) ;
107
+ await loadOrders ( ) ;
108
+ expect ( getTotal . value ) . toEqual ( 20 ) ;
109
+ } ) ;
110
+ } ) ;
111
+
112
+ describe ( "getLimit" , ( ) => {
113
+ it ( "should return 10 when there is no configuration set" , async ( ) => {
114
+ const ordersResponse = {
115
+ elements : [
116
+ {
117
+ id : "12345" ,
118
+ orderNumber : "100123" ,
119
+ } ,
120
+ ] ,
121
+ } ;
122
+ mockedApiClient . getCustomerOrders . mockResolvedValueOnce (
123
+ ordersResponse as any
124
+ ) ;
125
+ const { getLimit, loadOrders } = useCustomerOrders ( ) ;
126
+ expect ( getLimit . value ) . toEqual ( 10 ) ;
127
+ await loadOrders ( ) ;
128
+ expect ( getLimit . value ) . toEqual ( 10 ) ;
129
+ } ) ;
130
+
131
+ it ( "should return limit from current orders" , async ( ) => {
132
+ const ordersResponse = {
133
+ elements : [
134
+ {
135
+ id : "12345" ,
136
+ orderNumber : "100123" ,
137
+ } ,
138
+ ] ,
139
+ limit : 11
140
+ } ;
141
+ mockedApiClient . getCustomerOrders . mockResolvedValueOnce (
142
+ ordersResponse as any
143
+ ) ;
144
+ const { getLimit, loadOrders } = useCustomerOrders ( ) ;
145
+ await loadOrders ( ) ;
146
+ expect ( getLimit . value ) . toEqual ( 11 ) ;
147
+ } ) ;
148
+ } ) ;
149
+
150
+ describe ( "getTotalPagesCount" , ( ) => {
151
+ it ( "should return 0 when there is no currentListing" , async ( ) => {
152
+ const ordersResponse = {
153
+ aggregations : {
154
+ 'count-id' : {
155
+ count : 0
156
+ }
157
+ } ,
158
+ elements : [ ]
159
+ } ;
160
+ mockedApiClient . getCustomerOrders . mockResolvedValueOnce (
161
+ ordersResponse as any
162
+ ) ;
163
+
164
+ const { getTotalPagesCount, loadOrders } = useCustomerOrders ( ) ;
165
+ await loadOrders ( ) ;
166
+ expect ( getTotalPagesCount . value ) . toEqual ( 0 ) ;
167
+ } ) ;
168
+
169
+ it ( "should return ceiled pages count from current listing" , async ( ) => {
170
+ const ordersResponse = {
171
+ aggregations : {
172
+ 'count-id' : {
173
+ count : 22
174
+ }
175
+ } ,
176
+ elements : Array ( 22 ) . fill ( {
177
+ id : "11111" ,
178
+ orderNumber : "100120" ,
179
+ } )
180
+ } ;
181
+ mockedApiClient . getCustomerOrders . mockResolvedValueOnce (
182
+ ordersResponse as any
183
+ ) ;
184
+
185
+ const { getTotalPagesCount, loadOrders } = useCustomerOrders ( ) ;
186
+ await loadOrders ( ) ;
187
+ expect ( getTotalPagesCount . value ) . toEqual ( 3 ) ;
188
+ } ) ;
189
+ } ) ;
190
+
191
+ describe ( "getCurrentPage" , ( ) => {
192
+ it ( "should return 1 as default" , async ( ) => {
193
+ const { getCurrentPage } = useCustomerOrders ( ) ;
194
+ expect ( getCurrentPage . value ) . toEqual ( 1 ) ;
195
+ } ) ;
196
+
197
+ it ( "should return 1 when there is no orders" , async ( ) => {
198
+ const ordersResponse = {
199
+ aggregations : {
200
+ 'count-id' : {
201
+ count : 0
202
+ }
203
+ } ,
204
+ elements : [ ]
205
+ } ;
206
+ mockedApiClient . getCustomerOrders . mockResolvedValueOnce (
207
+ ordersResponse as any
208
+ ) ;
209
+
210
+ const { getCurrentPage, loadOrders } = useCustomerOrders ( ) ;
211
+ await loadOrders ( ) ;
212
+ expect ( getCurrentPage . value ) . toEqual ( 1 ) ;
213
+ } ) ;
214
+
215
+ it ( "should return page number from current listing" , async ( ) => {
216
+ const ordersResponse = {
217
+ aggregations : {
218
+ 'count-id' : {
219
+ count : 3
220
+ }
221
+ } ,
222
+ page : 3 ,
223
+ limit : 1 ,
224
+ elements : [
225
+ {
226
+ id : "12345" ,
227
+ orderNumber : "100123" ,
228
+ } ,
229
+ {
230
+ id : "12346" ,
231
+ orderNumber : "100123" ,
232
+ }
233
+ ]
234
+ } ;
235
+ mockedApiClient . getCustomerOrders . mockResolvedValueOnce (
236
+ ordersResponse as any
237
+ ) ;
238
+
239
+ const { getCurrentPage, loadOrders } = useCustomerOrders ( ) ;
240
+ await loadOrders ( ) ;
241
+ expect ( getCurrentPage . value ) . toEqual ( 3 ) ;
242
+ } ) ;
243
+ } ) ;
244
+
245
+ describe ( "changeCurrentPage" , ( ) => {
246
+ it ( "should invoke search with changed page number" , async ( ) => {
247
+ const ordersResponse = ( page : number ) : EntityResult < "order" , Order [ ] > => ( {
248
+ aggregations : [ ] ,
249
+ page,
250
+ limit : 10 ,
251
+ entity : 'order' ,
252
+ total : 10 ,
253
+ apiAlias : '' ,
254
+ elements : [ ]
255
+ } ) ;
256
+ mockedApiClient . getCustomerOrders . mockImplementationOnce ( async ( params ) => Promise . resolve ( ordersResponse ( params ?. page || 1 ) ) ) ;
257
+
258
+ const { changeCurrentPage, getCurrentPage } = useCustomerOrders ( ) ;
259
+ await changeCurrentPage ( 3 ) ;
260
+ expect ( getCurrentPage . value ) . toEqual ( 3 ) ;
261
+ } ) ;
262
+ } ) ;
68
263
} ) ;
69
264
} ) ;
1 commit comments
vercel[bot] commentedon Jun 14, 2022
Successfully deployed to the following URLs:
shopware-pwa-docs – ./
shopware-pwa-docs.vercel.app
shopware-pwa-docs-git-master-shopware-pwa.vercel.app
shopware-pwa-docs.vuestorefront.io
shopware-pwa-docs-shopware-pwa.vercel.app