added tests

This commit is contained in:
lotte
2021-03-24 14:33:32 +01:00
parent 8d6156df37
commit 11f3962326
2 changed files with 127 additions and 1 deletions

View File

@@ -0,0 +1,107 @@
import { ComponentFixture, fakeAsync, TestBed, tick, waitForAsync } from '@angular/core/testing';
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { TranslateLoaderMock } from '../../shared/mocks/translate-loader.mock';
import { ChangeDetectionStrategy, Injector, NO_ERRORS_SCHEMA } from '@angular/core';
import { ActivatedRoute, ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router';
import { EditItemPageComponent } from './edit-item-page.component';
import { Observable, of as observableOf } from 'rxjs';
import { By } from '@angular/platform-browser';
import { createSuccessfulRemoteDataObject } from '../../shared/remote-data.utils';
import { Item } from '../../core/shared/item.model';
describe('ItemPageComponent', () => {
let comp: EditItemPageComponent;
let fixture: ComponentFixture<EditItemPageComponent>;
class AcceptAllGuard implements CanActivate {
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return observableOf(true);
}
}
// tslint:disable-next-line:max-classes-per-file
class AcceptNoneGuard implements CanActivate {
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
console.log('BLA');
return observableOf(false);
}
}
const accesiblePages = ['accessible'];
const inaccesiblePages = ['inaccessible', 'inaccessibleDoubleGuard'];
const mockRoute = {
snapshot: {
firstChild: {
routeConfig: {
path: accesiblePages[0]
}
},
routerState: {
snapshot: undefined
}
},
routeConfig: {
children: [
{
path: accesiblePages[0],
canActivate: [AcceptAllGuard]
}, {
path: inaccesiblePages[0],
canActivate: [AcceptNoneGuard]
}, {
path: inaccesiblePages[1],
canActivate: [AcceptAllGuard, AcceptNoneGuard]
},
]
},
data: observableOf({dso: createSuccessfulRemoteDataObject(new Item())})
};
const mockRouter = {
routerState: {
snapshot: undefined
},
events: observableOf(undefined)
};
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useClass: TranslateLoaderMock
}
})],
declarations: [EditItemPageComponent],
providers: [
{ provide: ActivatedRoute, useValue: mockRoute },
{ provide: Router, useValue: mockRouter },
AcceptAllGuard,
AcceptNoneGuard,
],
schemas: [NO_ERRORS_SCHEMA]
}).overrideComponent(EditItemPageComponent, {
set: { changeDetection: ChangeDetectionStrategy.Default }
}).compileComponents();
}));
beforeEach(waitForAsync(() => {
fixture = TestBed.createComponent(EditItemPageComponent);
comp = fixture.componentInstance;
spyOn((comp as any).injector, 'get').and.callFake((a) => new a());
fixture.detectChanges();
}));
describe('ngOnInit', () => {
it('should enable tabs that the user can activate', fakeAsync(() => {
const enabledItems = fixture.debugElement.queryAll(By.css('a.nav-link'));
expect(enabledItems.length).toBe(accesiblePages.length);
}));
it('should disable tabs that the user can not activate', () => {
const disabledItems = fixture.debugElement.queryAll(By.css('button.nav-link.disabled'));
expect(disabledItems.length).toBe(inaccesiblePages.length);
});
});
});

View File

@@ -32,6 +32,8 @@ describe('DsoPageAdministratorGuard', () => {
let authService: AuthService; let authService: AuthService;
let resolver: Resolve<RemoteData<any>>; let resolver: Resolve<RemoteData<any>>;
let object: DSpaceObject; let object: DSpaceObject;
let route;
let parentRoute;
function init() { function init() {
object = { object = {
@@ -50,6 +52,16 @@ describe('DsoPageAdministratorGuard', () => {
authService = jasmine.createSpyObj('authService', { authService = jasmine.createSpyObj('authService', {
isAuthenticated: observableOf(true) isAuthenticated: observableOf(true)
}); });
parentRoute = {
params: {
id: '3e1a5327-dabb-41ff-af93-e6cab9d032f0'
}
};
route = {
params: {
},
parent: parentRoute
};
guard = new DsoPageFeatureGuardImpl(resolver, authorizationService, router, authService, undefined); guard = new DsoPageFeatureGuardImpl(resolver, authorizationService, router, authService, undefined);
} }
@@ -59,10 +71,17 @@ describe('DsoPageAdministratorGuard', () => {
describe('getObjectUrl', () => { describe('getObjectUrl', () => {
it('should return the resolved object\'s selflink', (done) => { it('should return the resolved object\'s selflink', (done) => {
guard.getObjectUrl(undefined, undefined).subscribe((selflink) => { guard.getObjectUrl(route, undefined).subscribe((selflink) => {
expect(selflink).toEqual(object.self); expect(selflink).toEqual(object.self);
done(); done();
}); });
}); });
}); });
describe('getRouteWithDSOId', () => {
it('should return the route that has the UUID of the DSO', () => {
const foundRoute = (guard as any).getRouteWithDSOId(route);
expect(foundRoute).toBe(parentRoute);
});
});
}); });