Skip to content

Commit

Permalink
feat: recalculate basket after currency change (#1005)
Browse files Browse the repository at this point in the history
  • Loading branch information
SGrueber authored Feb 10, 2022
1 parent 120dac3 commit 04b44bc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/app/core/store/customer/basket/basket.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 }]),
],
Expand Down Expand Up @@ -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));
Expand Down
16 changes: 16 additions & 0 deletions src/app/core/store/customer/basket/basket.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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'.
*/
Expand Down

0 comments on commit 04b44bc

Please sign in to comment.