Merge pull request #1898 from abpframework/test/core-tests

refactor(core): remove unnecessary injected service
pull/1904/head
Yasin Aydın 5 years ago committed by GitHub
commit 191efc7ccf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,15 +1,13 @@
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot, UrlTree, Router } from '@angular/router';
import { Store } from '@ngxs/store';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router';
import { OAuthService } from 'angular-oauth2-oidc';
import { Observable } from 'rxjs';
import { Navigate } from '@ngxs/router-plugin';
@Injectable({
providedIn: 'root',
})
export class AuthGuard implements CanActivate {
constructor(private oauthService: OAuthService, private store: Store, private router: Router) {}
constructor(private oauthService: OAuthService, private router: Router) {}
canActivate(_: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean | UrlTree {
const hasValidAccessToken = this.oauthService.hasValidAccessToken();

@ -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();
});
});
});

@ -62,7 +62,7 @@ describe('HttpClient testing', () => {
});
test('should handle the error', () => {
jest.spyOn(spectator.httpClient, 'request').mockReturnValue(throwError('An error'));
jest.spyOn(spectator.httpClient, 'request').mockReturnValue(throwError('Testing error'));
const spy = jest.spyOn(store, 'dispatch');
spectator.service
@ -78,7 +78,7 @@ describe('HttpClient testing', () => {
});
test('should not handle the error when skipHandleError is true', () => {
jest.spyOn(spectator.httpClient, 'request').mockReturnValue(throwError('An error'));
jest.spyOn(spectator.httpClient, 'request').mockReturnValue(throwError('Testing error'));
const spy = jest.spyOn(store, 'dispatch');
spectator.service

Loading…
Cancel
Save