From 11f3962326d62ff91344bdb5b9f9a1b5a50f0bee Mon Sep 17 00:00:00 2001 From: lotte Date: Wed, 24 Mar 2021 14:33:32 +0100 Subject: [PATCH] added tests --- .../edit-item-page.component.spec.ts | 107 ++++++++++++++++++ .../dso-page-feature.guard.spec.ts | 21 +++- 2 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 src/app/+item-page/edit-item-page/edit-item-page.component.spec.ts diff --git a/src/app/+item-page/edit-item-page/edit-item-page.component.spec.ts b/src/app/+item-page/edit-item-page/edit-item-page.component.spec.ts new file mode 100644 index 0000000000..6c9f79c36b --- /dev/null +++ b/src/app/+item-page/edit-item-page/edit-item-page.component.spec.ts @@ -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; + + class AcceptAllGuard implements CanActivate { + canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | Promise | boolean | UrlTree { + return observableOf(true); + } + } + + // tslint:disable-next-line:max-classes-per-file + class AcceptNoneGuard implements CanActivate { + canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | Promise | 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); + }); + }); +}); diff --git a/src/app/core/data/feature-authorization/feature-authorization-guard/dso-page-feature.guard.spec.ts b/src/app/core/data/feature-authorization/feature-authorization-guard/dso-page-feature.guard.spec.ts index 4041e588ed..f98e3f1837 100644 --- a/src/app/core/data/feature-authorization/feature-authorization-guard/dso-page-feature.guard.spec.ts +++ b/src/app/core/data/feature-authorization/feature-authorization-guard/dso-page-feature.guard.spec.ts @@ -32,6 +32,8 @@ describe('DsoPageAdministratorGuard', () => { let authService: AuthService; let resolver: Resolve>; let object: DSpaceObject; + let route; + let parentRoute; function init() { object = { @@ -50,6 +52,16 @@ describe('DsoPageAdministratorGuard', () => { authService = jasmine.createSpyObj('authService', { isAuthenticated: observableOf(true) }); + parentRoute = { + params: { + id: '3e1a5327-dabb-41ff-af93-e6cab9d032f0' + } + }; + route = { + params: { + }, + parent: parentRoute + }; guard = new DsoPageFeatureGuardImpl(resolver, authorizationService, router, authService, undefined); } @@ -59,10 +71,17 @@ describe('DsoPageAdministratorGuard', () => { describe('getObjectUrl', () => { 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); 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); + }); + }); });