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

Add block navigation guards #54

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
71 changes: 71 additions & 0 deletions src/app/guards/navigation-blocked/navigation-blocked.guard.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Component } from '@angular/core';
import { TestBed, async, ComponentFixture, inject } from '@angular/core/testing';
import { Observable } from 'rxjs';
import { AlertController } from '@ionic/angular';

import { NavigationBlockedGuard, HasNavigationBlocked } from './navigation-blocked.guard';

@Component({
template: '<div></div>'
})
class HasNavigationBlockedMockComponent implements HasNavigationBlocked {
hasNavigationBlockedValue: Observable<boolean> | Promise<boolean> | boolean;

hasNavigationBlocked(): Observable<boolean> | Promise<boolean> | boolean {
return this.hasNavigationBlockedValue;
}
}

describe('NavigationBlockedGuard', () => {
let component: HasNavigationBlockedMockComponent;
let fixture: ComponentFixture<HasNavigationBlockedMockComponent>;
let guard: NavigationBlockedGuard;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [],
declarations: [ HasNavigationBlockedMockComponent ],
schemas: [],
providers: [],
})
.compileComponents();

guard = TestBed.inject(NavigationBlockedGuard);
});

beforeEach(() => {
fixture = TestBed.createComponent(HasNavigationBlockedMockComponent);

component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(guard).toBeTruthy();
});

it('should block navigation', (done) => {

component.hasNavigationBlockedValue = true;

guard.canDeactivate(component, {} as any, {} as any)
.then((canDeactivate) => {
expect(canDeactivate).toBeFalsy();
done();
})
.catch(done);
});

it('should unblock navigation', (done) => {

component.hasNavigationBlockedValue = false;

guard.canDeactivate(component, {} as any, {} as any)
.then((canDeactivate) => {
expect(canDeactivate).toBeTruthy();
done();
})
.catch(done);
});

});
22 changes: 22 additions & 0 deletions src/app/guards/navigation-blocked/navigation-blocked.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot, CanDeactivate } from '@angular/router';
import { Observable } from 'rxjs';

export interface HasNavigationBlocked {
hasNavigationBlocked: () => Observable<boolean> | Promise<boolean> | boolean;
}

@Injectable({
providedIn: 'root'
})
export class NavigationBlockedGuard implements CanDeactivate<HasNavigationBlocked> {
constructor() {}

async canDeactivate(component: HasNavigationBlocked,
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot) {

return !component.hasNavigationBlocked();
}
}