Skip to content

Commit

Permalink
feat(cart): implement CanActivateChild in in stock items guard (#2558)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 committed Aug 31, 2023
1 parent 24d438f commit 669bef2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import {
Inject,
} from '@angular/core';
import {
ActivatedRouteSnapshot,
CanActivate,
CanActivateChild,
Router,
RouterStateSnapshot,
UrlTree,
} from '@angular/router';
import { Observable } from 'rxjs';
import {
Expand All @@ -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<boolean> {
return this.canActivate();
}

canActivate(): Observable<boolean> {
return this.facade.hasOutOfStockItems$.pipe(
map(hasOutOfStockItems => !hasOutOfStockItems),
Expand Down

0 comments on commit 669bef2

Please sign in to comment.