Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cart): implement CanActivateChild in in stock items guard #2558

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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