blob: 2556c0dc1e3f211e5323aba39aa5db77e7523998 [file] [log] [blame]
/********************************************************************************
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
********************************************************************************/
import {Location} from "@angular/common";
import {NgZone} from "@angular/core";
import {async, TestBed} from "@angular/core/testing";
import {Router} from "@angular/router";
import {RouterTestingModule} from "@angular/router/testing";
import {MemoizedSelector} from "@ngrx/store";
import {MockStore, provideMockStore} from "@ngrx/store/testing";
import {appRoutes} from "../../../app-routing.module";
import {EAPIUserRoles} from "../../../core";
import {isLoadingSelector, userRolesSelector} from "../../../store";
describe("NewStatementRouteGuard", () => {
let router: Router;
let location: Location;
let userRolesSelectorMock: MemoizedSelector<any, string[]>;
function callInZone<T>(fn: () => T | Promise<T>): Promise<T> {
const ngZone = TestBed.inject(NgZone);
return new Promise<T>((res, rej) => {
ngZone.run(() => Promise.resolve().then(() => fn()).then(res).catch(rej));
});
}
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule.withRoutes(appRoutes),
],
providers: [
provideMockStore()
]
}).compileComponents();
router = TestBed.inject(Router);
location = TestBed.inject(Location);
const mockStore = TestBed.inject(MockStore);
userRolesSelectorMock = mockStore.overrideSelector(userRolesSelector, []);
const isLoadingSelectorMock = mockStore.overrideSelector(isLoadingSelector, true);
isLoadingSelectorMock.setResult(false);
}));
it("should allow access to /new if user is official in charge ", async () => {
userRolesSelectorMock.setResult([EAPIUserRoles.SPA_OFFICIAL_IN_CHARGE]);
const isRoutingSuccessful = await callInZone(() => router.navigate(["new"]));
expect(isRoutingSuccessful).toBeTruthy();
expect(location.path()).toBe("/new");
});
it("should prevent access to /new if user is no official in charge ", async () => {
const isRoutingSuccessful = await callInZone(() => router.navigate(["new"]));
expect(isRoutingSuccessful).toBeTruthy();
expect(location.path()).toBe("/");
});
});