1
1
import { TestBed } from '@angular/core/testing' ;
2
- import { CommandService , RoutingService } from '@spartacus/core' ;
2
+ import { MultiCartFacade , OrderEntry } from '@spartacus/cart/base/root' ;
3
+ import { CommandService , RoutingService , UserIdService } from '@spartacus/core' ;
3
4
import {
4
5
PUNCHOUT_ERROR_PAGE_URL ,
6
+ PunchoutInitialCart ,
5
7
PunchOutLevel ,
6
8
PunchOutOperation ,
7
9
PunchoutRequisition ,
@@ -51,8 +53,39 @@ const mockPunchoutSession: PunchoutSession = {
51
53
const mockPunchoutState : PunchoutState = {
52
54
punchoutSessionId : mockSessionId ,
53
55
punchoutSession : mockPunchoutSession ,
56
+ punchoutInitialCart : undefined ,
57
+ cancelRequisition : undefined ,
54
58
} ;
55
59
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
+
56
89
class MockPunchoutStoreService implements Partial < PunchoutStoreService > {
57
90
setPunchoutState = ( ) => { } ;
58
91
getPunchoutState = ( ) => of ( mockPunchoutState ) ;
@@ -83,12 +116,32 @@ class MockRoutingService implements Partial<RoutingService> {
83
116
go = ( ) => Promise . resolve ( true ) ;
84
117
}
85
118
119
+ // class MockMultiCartFacade implements Partial<MultiCartFacade> {
120
+ // loadCart = () => {};
121
+ // removeEntry = () => {};
122
+ // addEntries = () => {};
123
+ // getCart = () => of(mockCart);
124
+ // isStable = () => of(true);
125
+ // }
126
+
127
+ class MockMultiCartFacade implements Partial < MultiCartFacade > {
128
+ getCart = createSpy ( ) . and . returnValue ( of ( mockCart ) ) ;
129
+ addEntry = createSpy ( ) ;
130
+ removeEntry = createSpy ( ) ;
131
+ isStable = createSpy ( ) . and . returnValue ( of ( true ) ) ;
132
+ }
133
+
134
+ class MockUserIdService implements Partial < UserIdService > {
135
+ takeUserId = ( ) => of ( mockPunchoutSession . customerId ) ;
136
+ }
137
+
86
138
describe ( 'Punchoutservice' , ( ) => {
87
139
let service : PunchoutService ;
88
140
let connector : PunchoutConnector ;
89
141
let routingService : RoutingService ;
90
142
let punchoutStoreService : PunchoutStoreService ;
91
143
let punchoutAuthService : PunchoutAuthService ;
144
+ let multiCartFacade : MultiCartFacade ;
92
145
93
146
beforeEach ( ( ) => {
94
147
TestBed . configureTestingModule ( {
@@ -99,13 +152,16 @@ describe('Punchoutservice', () => {
99
152
{ provide : CommandService , useValue : commandServiceMock } ,
100
153
{ provide : RoutingService , useClass : MockRoutingService } ,
101
154
{ provide : MockPunchoutStoreService , useClass : PunchoutStoreService } ,
155
+ { provide : MockMultiCartFacade , useClass : MultiCartFacade } ,
156
+ { provide : MockUserIdService , useClass : UserIdService } ,
102
157
] ,
103
158
} ) ;
104
159
service = TestBed . inject ( PunchoutService ) ;
105
160
connector = TestBed . inject ( PunchoutConnector ) ;
106
161
routingService = TestBed . inject ( RoutingService ) ;
107
162
punchoutStoreService = TestBed . inject ( PunchoutStoreService ) ;
108
163
punchoutAuthService = TestBed . inject ( PunchoutAuthService ) ;
164
+ multiCartFacade = TestBed . inject ( MultiCartFacade ) ;
109
165
} ) ;
110
166
111
167
it ( 'should be created' , ( ) => {
@@ -278,4 +334,30 @@ describe('Punchoutservice', () => {
278
334
} ,
279
335
} ) ;
280
336
} ) ;
337
+
338
+ it ( 'should closePunchoutSession ' , ( done ) => {
339
+ const mockState : PunchoutState = {
340
+ ...mockPunchoutState ,
341
+ punchoutInitialCart : mockInitialCart ,
342
+ punchoutSession : {
343
+ ...mockPunchoutSession ,
344
+ punchOutOperation : PunchOutOperation . EDIT ,
345
+ } ,
346
+ } ;
347
+ spyOn ( routingService , 'go' ) . and . returnValue ( Promise . resolve ( true ) ) ;
348
+ spyOn ( punchoutStoreService , 'getPunchoutState' ) . and . returnValue (
349
+ of ( mockState )
350
+ ) ;
351
+ // spyOn(multiCartFacade, 'deleteCart').and.callThrough();
352
+ spyOn ( punchoutStoreService , 'updatePunchoutState' ) . and . callThrough ( ) ;
353
+ spyOn ( multiCartFacade , 'addEntries' ) . and . callThrough ( ) ;
354
+ spyOn ( multiCartFacade , 'isStable' ) . and . callThrough ( ) ;
355
+
356
+ service . closePunchoutSession ( ) . subscribe ( {
357
+ next : ( ) => {
358
+ expect ( punchoutStoreService . updatePunchoutState ) . toHaveBeenCalled ( ) ;
359
+ done ( ) ;
360
+ } ,
361
+ } ) ;
362
+ } ) ;
281
363
} ) ;
0 commit comments