Skip to content

Commit

Permalink
feat: active quote carts must not be swapped with saved carts (#18085)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristophHi authored Nov 9, 2023
1 parent 0cb44de commit e94b611
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,48 @@ describe('SavedCart Effects', () => {
GlobalMessageType.MSG_TYPE_CONFIRMATION
);
});

it('should restore a saved cart and make it active without saving active cart if active cart is linked to a quote', () => {
activeCart$.next({ ...mockActiveCart, quoteCode: 'quoteCode' });

const action = new SavedCartActions.RestoreSavedCart({
userId: mockUserId,
cartId: mockCartId,
});

const completion1 = new CartActions.LoadCartSuccess({
userId: mockUserId,
cartId: mockCartId,
cart: mockSavedCarts[0],
extraData: { active: true },
});
const completion2 = new SavedCartActions.RestoreSavedCartSuccess({
userId: mockUserId,
cartId: mockCartId,
});

actions$ = hot('-a', { a: action });
const expected = cold('-(bc)', {
b: completion1,
c: completion2,
});

expect(effects.restoreSavedCart$).toBeObservable(expected);
expect(connector.restoreSavedCart).toHaveBeenCalledWith(
mockUserId,
mockCartId
);
expect(globalMessageService.add).toHaveBeenCalledWith(
{
key: 'savedCartList.swapCartNoActiveCart',
params: {
cartName: mockCartId,
previousCartName: mockActiveCart.code,
},
},
GlobalMessageType.MSG_TYPE_CONFIRMATION
);
});
});

describe('saveCart$', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,29 +102,34 @@ export class SavedCartEffects {
switchMap(([{ userId, cartId }, activeCart]) => {
const actions: any[] = [];

if ((activeCart?.entries ?? []).length > 0) {
if (activeCart.code) {
/**
* Instead of calling the SaveCartAction, we are calling the edit saved cart
* because we do not want to clear the state when we swap carts between active and saved cart
*/
actions.push(
new SavedCartActions.EditSavedCart({
userId,
cartId: activeCart.code,
saveCartName: '',
saveCartDescription: '',
})
);
}
//We must not swap carts in case the active cart is linked to a quote.
//In that case the quote cart is available from the linked quote
if (
(activeCart?.entries ?? []).length > 0 &&
!activeCart.quoteCode &&
activeCart.code
) {
/**
* Instead of calling the SaveCartAction, we are calling the edit saved cart
* because we do not want to clear the state when we swap carts between active and saved cart
*/
actions.push(
new SavedCartActions.EditSavedCart({
userId,
cartId: activeCart.code,
saveCartName: '',
saveCartDescription: '',
})
);
}

return this.savedCartConnector.restoreSavedCart(userId, cartId).pipe(
switchMap((savedCart: Cart) => {
this.globalMessageService.add(
{
key:
(activeCart?.entries ?? []).length > 0
(activeCart?.entries ?? []).length > 0 &&
!activeCart.quoteCode
? 'savedCartList.swapCartWithActiveCart'
: 'savedCartList.swapCartNoActiveCart',
params: {
Expand Down

0 comments on commit e94b611

Please sign in to comment.