test(core): add permission.guard.spec

pull/1898/head
mehmet-erim 5 years ago
parent 7dfce0ef58
commit edea62e938

@ -1,4 +1,4 @@
import { createServiceFactory, SpectatorService } from '@ngneat/spectator';
import { createServiceFactory, SpectatorService } from '@ngneat/spectator/jest';
import { AuthGuard } from '../guards/auth.guard';
import { OAuthService } from 'angular-oauth2-oidc';
import { RouterModule, UrlTree, Router } from '@angular/router';

@ -0,0 +1,43 @@
import { createServiceFactory, SpectatorService, SpyObject } from '@ngneat/spectator/jest';
import { Store } from '@ngxs/store';
import { of } from 'rxjs';
import { PermissionGuard } from '../guards/permission.guard';
import { RestOccurError } from '../actions';
describe('PermissionGuard', () => {
let spectator: SpectatorService<PermissionGuard>;
let guard: PermissionGuard;
let store: SpyObject<Store>;
const createService = createServiceFactory({
service: PermissionGuard,
mocks: [Store],
});
beforeEach(() => {
spectator = createService();
guard = spectator.service;
store = spectator.get(Store);
});
it('should return true when the grantedPolicy is true', done => {
store.select.andReturn(of(true));
const spy = jest.spyOn(store, 'dispatch');
guard.canActivate({ data: { requiredPolicy: '' } } as any).subscribe(res => {
expect(res).toBe(true);
expect(spy.mock.calls).toHaveLength(0);
done();
});
});
it('should return false and dispatch RestOccurError when the grantedPolicy is false', done => {
store.select.andReturn(of(false));
const spy = jest.spyOn(store, 'dispatch');
guard.canActivate({ data: { requiredPolicy: '' } } as any).subscribe(res => {
expect(res).toBe(false);
expect(spy.mock.calls[0][0] instanceof RestOccurError).toBeTruthy();
expect((spy.mock.calls[0][0] as RestOccurError).payload).toEqual({ status: 403 });
done();
});
});
});
Loading…
Cancel
Save