1
1
import { TestBed } from '@angular/core/testing' ;
2
- import { MultiCartFacade , OrderEntry } from '@spartacus/cart/base/root' ;
2
+ import { MultiCartFacade } from '@spartacus/cart/base/root' ;
3
3
import { CommandService , RoutingService , UserIdService } from '@spartacus/core' ;
4
4
import {
5
5
PUNCHOUT_ERROR_PAGE_URL ,
6
- PunchoutInitialCart ,
7
6
PunchOutLevel ,
8
7
PunchOutOperation ,
9
8
PunchoutRequisition ,
@@ -37,6 +36,11 @@ const mockPunchoutRequisitionResponse: PunchoutRequisition = {
37
36
orderAsCXML : 'mockCXML' ,
38
37
} ;
39
38
39
+ const mockPunchoutInitialRequisition : PunchoutRequisition = {
40
+ browseFormPostUrl : 'mockInitialFormUrl' ,
41
+ orderAsCXML : 'mockInitialCXML' ,
42
+ } ;
43
+
40
44
const mockSessionId = '123abc' ;
41
45
42
46
const mockPunchoutSession : PunchoutSession = {
@@ -53,39 +57,11 @@ const mockPunchoutSession: PunchoutSession = {
53
57
const mockPunchoutState : PunchoutState = {
54
58
punchoutSessionId : mockSessionId ,
55
59
punchoutSession : mockPunchoutSession ,
56
- punchoutInitialCart : undefined ,
60
+ punchoutInitialRequisition : undefined ,
57
61
cancelRequisition : undefined ,
62
+ closePunchoutSession : undefined ,
58
63
} ;
59
64
60
- const mockEntries : OrderEntry [ ] = [
61
- {
62
- quantity : 1 ,
63
- product : { name : 'product1' , code : 'code1' } ,
64
- } ,
65
- {
66
- quantity : 1 ,
67
- product : { name : 'product2' , code : 'cod2' } ,
68
- } ,
69
- ] ;
70
-
71
- const mockStateEntries : { productCode : string ; quantity : number } [ ] = [
72
- {
73
- quantity : 1 ,
74
- productCode : 'code1' ,
75
- } ,
76
- {
77
- quantity : 1 ,
78
- productCode : 'code2' ,
79
- } ,
80
- {
81
- quantity : 2 ,
82
- productCode : 'code3' ,
83
- } ,
84
- ] ;
85
-
86
- const mockInitialCart : PunchoutInitialCart = { entries : mockStateEntries } ;
87
- const mockCart = { entries : mockEntries } ;
88
-
89
65
class MockPunchoutStoreService implements Partial < PunchoutStoreService > {
90
66
setPunchoutState = ( ) => { } ;
91
67
getPunchoutState = ( ) => of ( mockPunchoutState ) ;
@@ -118,10 +94,6 @@ class MockRoutingService implements Partial<RoutingService> {
118
94
119
95
class MockMultiCartFacade implements Partial < MultiCartFacade > {
120
96
loadCart = ( ) => { } ;
121
- removeEntry = ( ) => { } ;
122
- addEntries = ( ) => { } ;
123
- getCart = ( ) => of ( mockCart ) ;
124
- isStable = ( ) => of ( true ) ;
125
97
}
126
98
127
99
class MockUserIdService implements Partial < UserIdService > {
@@ -162,7 +134,6 @@ describe('Punchoutservice', () => {
162
134
} ) ;
163
135
164
136
it ( 'should getPunchoutSession calls connector' , ( done ) => {
165
- spyOn ( multiCartFacade , 'getCart' ) . and . returnValue ( of ( mockCart ) ) ;
166
137
spyOn ( connector , 'getPunchoutSession' ) . and . returnValue (
167
138
of ( mockPunchoutSessionResponse )
168
139
) ;
@@ -220,7 +191,7 @@ describe('Punchoutservice', () => {
220
191
221
192
it ( 'should getPunchoutSession stays on page when isPageRefresh is true' , ( done ) => {
222
193
spyOn ( routingService , 'go' ) . and . returnValue ( Promise . resolve ( true ) ) ;
223
- spyOn ( multiCartFacade , 'getCart' ) . and . returnValue ( of ( mockCart ) ) ;
194
+
224
195
spyOn ( connector , 'getPunchoutSessionRequisition' ) . and . returnValue (
225
196
of ( mockPunchoutRequisitionResponse )
226
197
) ;
@@ -281,25 +252,31 @@ describe('Punchoutservice', () => {
281
252
282
253
it ( 'should getPunchoutSession opens cart page when no product item and EDIT Level ' , ( done ) => {
283
254
spyOn ( routingService , 'go' ) . and . returnValue ( Promise . resolve ( true ) ) ;
284
- spyOn ( multiCartFacade , 'getCart' ) . and . returnValue ( of ( mockCart ) ) ;
255
+
285
256
spyOn ( connector , 'getPunchoutSession' ) . and . returnValue (
286
257
of ( {
287
258
...mockPunchoutSessionResponse ,
288
259
selectedItem : '' ,
289
260
} )
290
261
) ;
291
-
262
+ spyOn ( connector , 'getPunchoutSessionRequisition' ) . and . returnValue (
263
+ of ( mockPunchoutInitialRequisition )
264
+ ) ;
265
+ spyOn ( punchoutStoreService , 'updatePunchoutState' ) . and . callThrough ( ) ;
292
266
service . getPunchoutSession ( mockSessionInput ) . subscribe ( {
293
267
next : ( ) => {
294
268
expect ( routingService . go ) . toHaveBeenCalledWith ( { cxRoute : 'cart' } ) ;
269
+ expect ( punchoutStoreService . updatePunchoutState ) . toHaveBeenCalledWith ( {
270
+ punchoutInitialRequisition : { ...mockPunchoutInitialRequisition } ,
271
+ } ) ;
295
272
done ( ) ;
296
273
} ,
297
274
} ) ;
298
275
} ) ;
299
276
300
277
it ( 'should getPunchoutSession opens pdp when selectedItem is present ' , ( done ) => {
301
278
spyOn ( routingService , 'go' ) . and . returnValue ( Promise . resolve ( true ) ) ;
302
- spyOn ( multiCartFacade , 'getCart' ) . and . returnValue ( of ( mockCart ) ) ;
279
+
303
280
spyOn ( connector , 'getPunchoutSession' ) . and . returnValue (
304
281
of ( mockPunchoutSessionResponse )
305
282
) ;
@@ -335,7 +312,8 @@ describe('Punchoutservice', () => {
335
312
it ( 'should closePunchoutSession revertToInitialCart in EDIT operation ' , ( done ) => {
336
313
const mockState : PunchoutState = {
337
314
...mockPunchoutState ,
338
- punchoutInitialCart : mockInitialCart ,
315
+ punchoutInitialRequisition : mockPunchoutInitialRequisition ,
316
+ closePunchoutSession : true ,
339
317
punchoutSession : {
340
318
...mockPunchoutSession ,
341
319
punchOutOperation : PunchOutOperation . EDIT ,
@@ -347,26 +325,12 @@ describe('Punchoutservice', () => {
347
325
) ;
348
326
// spyOn(multiCartFacade, 'deleteCart').and.callThrough();
349
327
spyOn ( punchoutStoreService , 'updatePunchoutState' ) . and . callThrough ( ) ;
350
- spyOn ( multiCartFacade , 'addEntries' ) . and . callThrough ( ) ;
351
- spyOn ( multiCartFacade , 'removeEntry' ) . and . callThrough ( ) ;
352
- spyOn ( multiCartFacade , 'isStable' ) . and . returnValue ( of ( true ) ) ;
353
- spyOn ( multiCartFacade , 'getCart' ) . and . returnValue ( of ( mockCart ) ) ;
328
+ spyOn ( connector , 'getPunchoutSessionRequisition' ) . and . callThrough ( ) ;
354
329
355
330
service . closePunchoutSession ( ) . subscribe ( {
356
331
next : ( ) => {
357
332
expect ( punchoutStoreService . updatePunchoutState ) . toHaveBeenCalled ( ) ;
358
- expect ( multiCartFacade . removeEntry ) . toHaveBeenCalledTimes (
359
- mockCart . entries . length
360
- ) ;
361
- expect ( multiCartFacade . isStable ) . toHaveBeenCalledTimes ( 2 ) ;
362
- expect ( multiCartFacade . addEntries ) . toHaveBeenCalledWith (
363
- mockState . punchoutSession ?. customerId as string ,
364
- mockState . punchoutSession ?. cartId as string ,
365
- mockState . punchoutInitialCart ?. entries as {
366
- productCode : string ;
367
- quantity : number ;
368
- } [ ]
369
- ) ;
333
+ expect ( connector . getPunchoutSessionRequisition ) . not . toHaveBeenCalled ( ) ;
370
334
done ( ) ;
371
335
} ,
372
336
} ) ;
@@ -375,7 +339,6 @@ describe('Punchoutservice', () => {
375
339
it ( 'should closePunchoutSession set cancelRequisition in CREATE operation ' , ( done ) => {
376
340
const mockState : PunchoutState = {
377
341
...mockPunchoutState ,
378
- punchoutInitialCart : mockInitialCart ,
379
342
punchoutSession : {
380
343
...mockPunchoutSession ,
381
344
punchOutOperation : PunchOutOperation . CREATE ,
@@ -406,7 +369,6 @@ describe('Punchoutservice', () => {
406
369
it ( 'should closePunchoutSession only go to requisition page in INSPECT operation ' , ( done ) => {
407
370
const mockState : PunchoutState = {
408
371
...mockPunchoutState ,
409
- punchoutInitialCart : mockInitialCart ,
410
372
punchoutSession : {
411
373
...mockPunchoutSession ,
412
374
punchOutOperation : PunchOutOperation . INSPECT ,
@@ -431,29 +393,4 @@ describe('Punchoutservice', () => {
431
393
} ,
432
394
} ) ;
433
395
} ) ;
434
-
435
- it ( 'should closePunchoutSession throw error when cartId is missing' , ( done ) => {
436
- const mockState : PunchoutState = {
437
- ...mockPunchoutState ,
438
- punchoutInitialCart : mockInitialCart ,
439
- punchoutSession : {
440
- ...mockPunchoutSession ,
441
- cartId : '' ,
442
- } ,
443
- } ;
444
- spyOn ( routingService , 'go' ) . and . returnValue ( Promise . resolve ( true ) ) ;
445
- spyOn ( punchoutStoreService , 'getPunchoutState' ) . and . returnValue (
446
- of ( mockState )
447
- ) ;
448
-
449
- spyOn ( punchoutStoreService , 'updatePunchoutState' ) . and . callThrough ( ) ;
450
- spyOn ( multiCartFacade , 'addEntries' ) . and . callThrough ( ) ;
451
- spyOn ( multiCartFacade , 'removeEntry' ) . and . callThrough ( ) ;
452
-
453
- service . closePunchoutSession ( ) . subscribe ( {
454
- error : ( ) => {
455
- done ( ) ;
456
- } ,
457
- } ) ;
458
- } ) ;
459
396
} ) ;
0 commit comments