Browse Source

test(core): add nest tests to permission directive and guard

pull/2261/head
mehmet-erim 7 years ago
parent
commit
ec7f1f3f87
  1. 22
      npm/ng-packs/packages/core/src/lib/tests/permission.directive.spec.ts
  2. 18
      npm/ng-packs/packages/core/src/lib/tests/permission.guard.spec.ts

22
npm/ng-packs/packages/core/src/lib/tests/permission.directive.spec.ts

@ -43,4 +43,26 @@ describe('PermissionDirective', () => {
expect(spy.mock.calls).toHaveLength(0);
});
});
describe('structural', () => {
beforeEach(() => {
spectator = createDirective(`<div id="test-element" *abpPermission="'test'">Testing Permission Directive</div>`);
directive = spectator.directive;
});
it('should be created', () => {
expect(directive).toBeTruthy();
});
it('should remove the element from DOM', () => {
expect(spectator.query('#test-element')).toBeFalsy();
grantedPolicy$.next(true);
expect(spectator.query('#test-element')).toBeTruthy();
grantedPolicy$.next(false);
expect(spectator.query('#test-element')).toBeFalsy();
grantedPolicy$.next(true);
grantedPolicy$.next(true);
expect(spectator.queryAll('#test-element')).toHaveLength(1);
});
});
});

18
npm/ng-packs/packages/core/src/lib/tests/permission.guard.spec.ts

@ -23,7 +23,7 @@ describe('PermissionGuard', () => {
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 => {
guard.canActivate({ data: { requiredPolicy: 'test' } } as any, null).subscribe(res => {
expect(res).toBe(true);
expect(spy.mock.calls).toHaveLength(0);
done();
@ -33,11 +33,25 @@ describe('PermissionGuard', () => {
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 => {
guard.canActivate({ data: { requiredPolicy: 'test' } } as any, null).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();
});
});
it('should find the requiredPolicy from child route', done => {
store.select.andReturn(of(false));
const spy = jest.spyOn(store, 'select');
guard
.canActivate(
{ data: {}, routeConfig: { children: [{ path: 'test', data: { requiredPolicy: 'TestPolicy' } }] } } as any,
{ url: 'test' } as any,
)
.subscribe(() => {
expect(spy.mock.calls[0][0]({ auth: { grantedPolicies: { TestPolicy: true } } })).toBe(true);
done();
});
});
});

Loading…
Cancel
Save