Skip to content

Commit 94350a1

Browse files
committed
Adjusted unit tests for the guard
1 parent 598d373 commit 94350a1

1 file changed

Lines changed: 81 additions & 7 deletions

File tree

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,90 @@
1+
// import { TestBed } from '@angular/core/testing';
2+
// import { CanActivateFn } from '@angular/router';
3+
//
4+
// import { NonAdminGuard } from './non-admin.guard';
5+
//
6+
// xdescribe('nonAdminGuard', () => {
7+
// const executeGuard: CanActivateFn = (...guardParameters) => TestBed.runInInjectionContext(() => NonAdminGuard(...guardParameters));
8+
//
9+
// beforeEach(() => {
10+
// TestBed.configureTestingModule({});
11+
// });
12+
//
13+
// it('should be created', () => {
14+
// expect(executeGuard).toBeTruthy();
15+
// });
16+
// });
117
import { TestBed } from '@angular/core/testing';
2-
import { CanActivateFn } from '@angular/router';
3-
18+
import { CanActivateFn, Router, UrlTree } from '@angular/router';
419
import { NonAdminGuard } from './non-admin.guard';
20+
import { MockStore, provideMockStore } from '@ngrx/store/testing';
21+
import { TUserRole } from 'src/app/shared/models/auth/user-role.type';
22+
import { userRoleSelector } from 'src/app/store/selectors/auth.selectors';
23+
import { isObservable } from 'rxjs';
24+
25+
describe('NonAdminGuard', () => {
26+
let store: MockStore;
27+
let routerSpy: jasmine.SpyObj<Router>;
528

6-
describe('nonAdminGuard', () => {
7-
const executeGuard: CanActivateFn = (...guardParameters) => TestBed.runInInjectionContext(() => NonAdminGuard(...guardParameters));
29+
const executeGuard: CanActivateFn = (...params) => TestBed.runInInjectionContext(() => NonAdminGuard(...params));
830

931
beforeEach(() => {
10-
TestBed.configureTestingModule({});
32+
routerSpy = jasmine.createSpyObj('Router', ['navigate']);
33+
TestBed.configureTestingModule({
34+
providers: [provideMockStore(), { provide: Router, useValue: routerSpy }]
35+
});
36+
37+
store = TestBed.inject(MockStore);
38+
});
39+
40+
const setupSelector = (role: TUserRole | null) => {
41+
store.overrideSelector(userRoleSelector, role);
42+
};
43+
44+
function runGuardAndAssert(role: TUserRole | null, url: string, expected: boolean, expectedRedirect?: string[], done?: DoneFn) {
45+
setupSelector(role);
46+
const result = executeGuard({} as any, { url } as any);
47+
const assert = (res: boolean | UrlTree) => {
48+
if (res instanceof UrlTree) {
49+
expect(expected).toBeTrue(); // assume redirect counts as "true"
50+
} else {
51+
expect(res).toBe(expected);
52+
}
53+
54+
if (expectedRedirect) {
55+
expect(routerSpy.navigate).toHaveBeenCalledWith(expectedRedirect);
56+
} else {
57+
expect(routerSpy.navigate).not.toHaveBeenCalled();
58+
}
59+
done?.();
60+
};
61+
62+
if (isObservable(result)) {
63+
result.subscribe(assert);
64+
} else if (result instanceof Promise) {
65+
result.then(assert);
66+
} else {
67+
assert(result);
68+
}
69+
}
70+
71+
it('should allow access for non-admin user', (done) => {
72+
runGuardAndAssert('ROLE_USER', '/some-non-exempt', true, undefined, done);
73+
});
74+
75+
it('should deny access for admin on non-exempt route and redirect', (done) => {
76+
runGuardAndAssert('ROLE_UBS_EMPLOYEE', '/ubs', false, ['/ubs/admin/orders'], done);
77+
});
78+
79+
it('should allow access for admin on exempt route', (done) => {
80+
runGuardAndAssert('ROLE_UBS_EMPLOYEE', '/chat-page', true, undefined, done);
81+
});
82+
83+
it('should redirect unauthenticated user on exempt route', (done) => {
84+
runGuardAndAssert(null, '/chat-page', true, ['/'], done);
1185
});
1286

13-
it('should be created', () => {
14-
expect(executeGuard).toBeTruthy();
87+
it('should allow unauthenticated user on non-exempt route (edge case)', (done) => {
88+
runGuardAndAssert(null, '/ubs', true, undefined, done);
1589
});
1690
});

0 commit comments

Comments
 (0)