diff --git a/src/app/core/store/customer/basket/basket.effects.spec.ts b/src/app/core/store/customer/basket/basket.effects.spec.ts index 368b8a89ba..9a90bbeb45 100644 --- a/src/app/core/store/customer/basket/basket.effects.spec.ts +++ b/src/app/core/store/customer/basket/basket.effects.spec.ts @@ -11,6 +11,7 @@ import { anyString, anything, instance, mock, verify, when } from 'ts-mockito'; import { Basket } from 'ish-core/models/basket/basket.model'; import { BasketService } from 'ish-core/services/basket/basket.service'; import { CoreStoreModule } from 'ish-core/store/core/core-store.module'; +import { loadServerConfigSuccess } from 'ish-core/store/core/server-config'; import { CustomerStoreModule } from 'ish-core/store/customer/customer-store.module'; import { resetOrderErrors } from 'ish-core/store/customer/orders'; import { ApiTokenService } from 'ish-core/utils/api-token/api-token.service'; @@ -62,7 +63,7 @@ describe('Basket Effects', () => { TestBed.configureTestingModule({ declarations: [DummyComponent], imports: [ - CoreStoreModule.forTesting(['router']), + CoreStoreModule.forTesting(['router', 'serverConfig', 'configuration']), CustomerStoreModule.forTesting('user', 'basket'), RouterTestingModule.withRoutes([{ path: '**', component: DummyComponent }]), ], @@ -194,6 +195,37 @@ describe('Basket Effects', () => { }); }); + describe('recalculateBasketAfterCurrencyChange$', () => { + beforeEach(() => { + store$.dispatch( + loadServerConfigSuccess({ + config: { + general: { + defaultLocale: 'de_DE', + defaultCurrency: 'EUR', + locales: ['en_US', 'de_DE', 'fr_BE', 'nl_BE'], + currencies: ['USD', 'EUR'], + }, + }, + }) + ); + }); + + it('should trigger a basket recalculation if the basket currency differs from current currency', done => { + const id = 'BID'; + + actions$ = of(loadBasketSuccess({ basket: { id, purchaseCurrency: 'USD' } as Basket })); + + effects.recalculateBasketAfterCurrencyChange$.subscribe(action => { + expect(action).toMatchInlineSnapshot(` + [Basket Internal] Update Basket: + update: {"calculated":true} + `); + done(); + }); + }); + }); + describe('createBasket$', () => { beforeEach(() => { when(basketServiceMock.createBasket()).thenCall(() => of({ id: 'BID' } as Basket)); diff --git a/src/app/core/store/customer/basket/basket.effects.ts b/src/app/core/store/customer/basket/basket.effects.ts index 0803fb21db..c72de31ff7 100644 --- a/src/app/core/store/customer/basket/basket.effects.ts +++ b/src/app/core/store/customer/basket/basket.effects.ts @@ -15,11 +15,13 @@ import { sample, startWith, switchMap, + take, withLatestFrom, } from 'rxjs/operators'; import { Basket } from 'ish-core/models/basket/basket.model'; import { BasketService } from 'ish-core/services/basket/basket.service'; +import { getCurrentCurrency } from 'ish-core/store/core/configuration'; import { mapToRouterState } from 'ish-core/store/core/router'; import { resetOrderErrors } from 'ish-core/store/customer/orders'; import { createUser, loadUserByAPIToken, loginUser, loginUserSuccess } from 'ish-core/store/customer/user'; @@ -114,6 +116,20 @@ export class BasketEffects { ) ); + /** + * Recalculate basket if the basket currency doesn't match the current currency. + */ + recalculateBasketAfterCurrencyChange$ = createEffect(() => + this.actions$.pipe( + ofType(loadBasketSuccess), + mapToPayloadProperty('basket'), + withLatestFrom(this.store.select(getCurrentCurrency)), + filter(([basket, currency]) => basket.purchaseCurrency !== currency), + take(1), + mapTo(updateBasket({ update: { calculated: true } })) + ) + ); + /** * Creates a basket that is used for all subsequent basket operations with a fixed basket id instead of 'current'. */