Skip to content

Commit c285f65

Browse files
committed
fix unit tests
1 parent 927e59b commit c285f65

File tree

2 files changed

+85
-20
lines changed

2 files changed

+85
-20
lines changed

Diff for: integration-libs/punchout/core/facade/punchout.service.spec.ts

+81-14
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,12 @@ class MockRoutingService implements Partial<RoutingService> {
116116
go = () => Promise.resolve(true);
117117
}
118118

119-
// class MockMultiCartFacade implements Partial<MultiCartFacade> {
120-
// loadCart = () => {};
121-
// removeEntry = () => {};
122-
// addEntries = () => {};
123-
// getCart = () => of(mockCart);
124-
// isStable = () => of(true);
125-
// }
126-
127119
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));
120+
loadCart = () => {};
121+
removeEntry = () => {};
122+
addEntries = () => {};
123+
getCart = () => of(mockCart);
124+
isStable = () => of(true);
132125
}
133126

134127
class MockUserIdService implements Partial<UserIdService> {
@@ -335,7 +328,7 @@ describe('Punchoutservice', () => {
335328
});
336329
});
337330

338-
it('should closePunchoutSession ', (done) => {
331+
it('should closePunchoutSession revertToInitialCart in EDIT operation ', (done) => {
339332
const mockState: PunchoutState = {
340333
...mockPunchoutState,
341334
punchoutInitialCart: mockInitialCart,
@@ -351,11 +344,85 @@ describe('Punchoutservice', () => {
351344
// spyOn(multiCartFacade, 'deleteCart').and.callThrough();
352345
spyOn(punchoutStoreService, 'updatePunchoutState').and.callThrough();
353346
spyOn(multiCartFacade, 'addEntries').and.callThrough();
354-
spyOn(multiCartFacade, 'isStable').and.callThrough();
347+
spyOn(multiCartFacade, 'removeEntry').and.callThrough();
348+
spyOn(multiCartFacade, 'isStable').and.returnValue(of(true));
349+
spyOn(multiCartFacade, 'getCart').and.returnValue(of(mockCart));
355350

356351
service.closePunchoutSession().subscribe({
357352
next: () => {
358353
expect(punchoutStoreService.updatePunchoutState).toHaveBeenCalled();
354+
expect(multiCartFacade.removeEntry).toHaveBeenCalledTimes(
355+
mockCart.entries.length
356+
);
357+
expect(multiCartFacade.isStable).toHaveBeenCalledTimes(2);
358+
expect(multiCartFacade.addEntries).toHaveBeenCalledWith(
359+
mockState.punchoutSession?.customerId as string,
360+
mockState.punchoutSession?.cartId as string,
361+
mockState.punchoutInitialCart?.entries as {
362+
productCode: string;
363+
quantity: number;
364+
}[]
365+
);
366+
done();
367+
},
368+
});
369+
});
370+
371+
it('should closePunchoutSession set cancelRequisition in CREATE operation ', (done) => {
372+
const mockState: PunchoutState = {
373+
...mockPunchoutState,
374+
punchoutInitialCart: mockInitialCart,
375+
punchoutSession: {
376+
...mockPunchoutSession,
377+
punchOutOperation: PunchOutOperation.CREATE,
378+
},
379+
};
380+
spyOn(routingService, 'go').and.returnValue(Promise.resolve(true));
381+
spyOn(punchoutStoreService, 'getPunchoutState').and.returnValue(
382+
of(mockState)
383+
);
384+
385+
spyOn(punchoutStoreService, 'updatePunchoutState').and.callThrough();
386+
spyOn(multiCartFacade, 'addEntries').and.callThrough();
387+
spyOn(multiCartFacade, 'removeEntry').and.callThrough();
388+
389+
service.closePunchoutSession().subscribe({
390+
next: () => {
391+
expect(punchoutStoreService.updatePunchoutState).toHaveBeenCalledWith({
392+
cancelRequisition: true,
393+
});
394+
expect(multiCartFacade.addEntries).not.toHaveBeenCalled();
395+
expect(multiCartFacade.removeEntry).not.toHaveBeenCalled();
396+
expect(routingService.go).toHaveBeenCalled();
397+
done();
398+
},
399+
});
400+
});
401+
402+
it('should closePunchoutSession only go to requisition page in INSPECT operation ', (done) => {
403+
const mockState: PunchoutState = {
404+
...mockPunchoutState,
405+
punchoutInitialCart: mockInitialCart,
406+
punchoutSession: {
407+
...mockPunchoutSession,
408+
punchOutOperation: PunchOutOperation.INSPECT,
409+
},
410+
};
411+
spyOn(routingService, 'go').and.returnValue(Promise.resolve(true));
412+
spyOn(punchoutStoreService, 'getPunchoutState').and.returnValue(
413+
of(mockState)
414+
);
415+
416+
spyOn(punchoutStoreService, 'updatePunchoutState').and.callThrough();
417+
spyOn(multiCartFacade, 'addEntries').and.callThrough();
418+
spyOn(multiCartFacade, 'removeEntry').and.callThrough();
419+
420+
service.closePunchoutSession().subscribe({
421+
next: () => {
422+
expect(punchoutStoreService.updatePunchoutState).not.toHaveBeenCalled();
423+
expect(multiCartFacade.addEntries).not.toHaveBeenCalled();
424+
expect(multiCartFacade.removeEntry).not.toHaveBeenCalled();
425+
expect(routingService.go).toHaveBeenCalled();
359426
done();
360427
},
361428
});

Diff for: integration-libs/punchout/core/facade/punchout.service.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,10 @@ export class PunchoutService implements PunchoutFacade {
321321
this.multiCartFacade.addEntries(
322322
state.punchoutSession?.customerId as string,
323323
state.punchoutSession?.cartId as string,
324-
state.punchoutInitialCart?.entries?.map((e) => {
325-
return {
326-
productCode: e.productCode,
327-
quantity: e.quantity,
328-
};
329-
}) as { productCode: string; quantity: number }[]
324+
state.punchoutInitialCart?.entries as {
325+
productCode: string;
326+
quantity: number;
327+
}[]
330328
);
331329
})
332330
);

0 commit comments

Comments
 (0)