mirror of https://github.com/abpframework/abp
Merge pull request #1898 from abpframework/test/core-tests
refactor(core): remove unnecessary injected servicepull/1904/head
commit
191efc7ccf
@ -0,0 +1,35 @@
|
||||
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';
|
||||
import { RouterOutletComponent } from '../components';
|
||||
import { APP_BASE_HREF } from '@angular/common';
|
||||
|
||||
describe('AuthGuard', () => {
|
||||
let spectator: SpectatorService<AuthGuard>;
|
||||
let guard: AuthGuard;
|
||||
const createService = createServiceFactory({
|
||||
service: AuthGuard,
|
||||
mocks: [OAuthService],
|
||||
imports: [RouterModule.forRoot([{ path: '', component: RouterOutletComponent }])],
|
||||
declarations: [RouterOutletComponent],
|
||||
providers: [{ provide: APP_BASE_HREF, useValue: '/' }],
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
spectator = createService();
|
||||
guard = spectator.service;
|
||||
});
|
||||
|
||||
it('should return true when user logged in', () => {
|
||||
spectator.get(OAuthService).hasValidAccessToken.andReturn(true);
|
||||
expect(guard.canActivate(null, null)).toBe(true);
|
||||
});
|
||||
|
||||
it('should return url tree when user not logged in', () => {
|
||||
const router = spectator.get(Router);
|
||||
const expectedUrlTree = router.createUrlTree(['/account/login'], { state: { redirectUrl: '/' } });
|
||||
spectator.get(OAuthService).hasValidAccessToken.andReturn(false);
|
||||
expect(guard.canActivate(null, { url: '/' } as any) as UrlTree).toEqual(expectedUrlTree);
|
||||
});
|
||||
});
|
@ -0,0 +1,46 @@
|
||||
import { PermissionDirective } from '../directives/permission.directive';
|
||||
import { SpectatorDirective, createDirectiveFactory, SpyObject } from '@ngneat/spectator/jest';
|
||||
import { Store } from '@ngxs/store';
|
||||
import { of, Subject } from 'rxjs';
|
||||
|
||||
describe('PermissionDirective', () => {
|
||||
let spectator: SpectatorDirective<PermissionDirective>;
|
||||
let directive: PermissionDirective;
|
||||
const grantedPolicy$ = new Subject();
|
||||
|
||||
const createDirective = createDirectiveFactory({
|
||||
directive: PermissionDirective,
|
||||
providers: [{ provide: Store, useValue: { select: () => grantedPolicy$ } }],
|
||||
});
|
||||
|
||||
describe('with condition', () => {
|
||||
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', () => {
|
||||
grantedPolicy$.next(true);
|
||||
expect(spectator.query('#test-element')).toBeTruthy();
|
||||
grantedPolicy$.next(false);
|
||||
expect(spectator.query('#test-element')).toBeFalsy();
|
||||
});
|
||||
});
|
||||
|
||||
describe('without condition', () => {
|
||||
beforeEach(() => {
|
||||
spectator = createDirective('<div id="test-element" abpPermission>Testing Permission Directive</div>');
|
||||
directive = spectator.directive;
|
||||
});
|
||||
|
||||
it('should do nothing when condition is undefined', () => {
|
||||
const spy = jest.spyOn(spectator.get(Store), 'select');
|
||||
grantedPolicy$.next(false);
|
||||
expect(spy.mock.calls).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
});
|
@ -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…
Reference in new issue