diff --git a/libs/cart/routing/src/guards/in-stock-items/in-stock-items.guard.spec.ts b/libs/cart/routing/src/guards/in-stock-items/in-stock-items.guard.spec.ts index 55feab2aad..e28682b42f 100644 --- a/libs/cart/routing/src/guards/in-stock-items/in-stock-items.guard.spec.ts +++ b/libs/cart/routing/src/guards/in-stock-items/in-stock-items.guard.spec.ts @@ -109,4 +109,49 @@ describe('@daffodil/cart/routing | DaffCartInStockItemsGuard', () => { }); }); }); + + describe('canActivateChild', () => { + describe('when there are only in stock items in the cart', () => { + beforeEach(() => { + cartItems = cartItemFactory.createMany(1, { + in_stock: true, + }); + store.dispatch(new DaffCartLoadSuccess({ + ...cart, + items: cartItems, + })); + }); + + it('should allow activation', () => { + const expected = cold('(a|)', { a: true }); + + expect(service.canActivateChild()).toBeObservable(expected); + }); + }); + + + describe('when there are out of stock items in the cart', () => { + beforeEach(() => { + cartItems = cartItemFactory.createMany(1, { + in_stock: false, + }); + spyOn(router, 'navigateByUrl'); + store.dispatch(new DaffCartLoadSuccess({ + ...cart, + items: cartItems, + })); + }); + + it('should not allow activation', () => { + const expected = cold('(a|)', { a: false }); + + expect(service.canActivateChild()).toBeObservable(expected); + }); + + it('should redirect to the given DaffCartInStockItemsGuardRedirectUrl', () => { + service.canActivate().subscribe(); + expect(router.navigateByUrl).toHaveBeenCalledWith(stubUrl); + }); + }); + }); }); diff --git a/libs/cart/routing/src/guards/in-stock-items/in-stock-items.guard.ts b/libs/cart/routing/src/guards/in-stock-items/in-stock-items.guard.ts index 155008944d..23e5620038 100644 --- a/libs/cart/routing/src/guards/in-stock-items/in-stock-items.guard.ts +++ b/libs/cart/routing/src/guards/in-stock-items/in-stock-items.guard.ts @@ -3,8 +3,12 @@ import { Inject, } from '@angular/core'; import { + ActivatedRouteSnapshot, CanActivate, + CanActivateChild, Router, + RouterStateSnapshot, + UrlTree, } from '@angular/router'; import { Observable } from 'rxjs'; import { @@ -27,13 +31,17 @@ import { DaffCartInStockItemsGuardRedirectUrl } from './in-stock-items-guard-red @Injectable({ providedIn: 'root', }) -export class DaffCartInStockItemsGuard implements CanActivate { +export class DaffCartInStockItemsGuard implements CanActivate, CanActivateChild { constructor( private facade: DaffCartFacade, private router: Router, @Inject(DaffCartInStockItemsGuardRedirectUrl) private redirectUrl: string, ) {} + canActivateChild(): Observable { + return this.canActivate(); + } + canActivate(): Observable { return this.facade.hasOutOfStockItems$.pipe( map(hasOutOfStockItems => !hasOutOfStockItems),