74200: DsoPageEditButtonComponent test cases

This commit is contained in:
Kristof De Langhe
2020-10-27 10:46:33 +01:00
parent 06769493e1
commit 18577f016c
2 changed files with 73 additions and 7 deletions

View File

@@ -1,25 +1,76 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { DsoPageEditButtonComponent } from './dso-page-edit-button.component'; import { DsoPageEditButtonComponent } from './dso-page-edit-button.component';
import { DSpaceObject } from '../../../core/shared/dspace-object.model';
import { Item } from '../../../core/shared/item.model';
import { AuthorizationDataService } from '../../../core/data/feature-authorization/authorization-data.service';
import { of as observableOf } from 'rxjs';
import { TranslateModule } from '@ngx-translate/core';
import { RouterTestingModule } from '@angular/router/testing';
import { FeatureID } from '../../../core/data/feature-authorization/feature-id';
import { By } from '@angular/platform-browser';
import { TooltipModule } from 'ngx-bootstrap';
describe('DsoPageEditButtonComponent', () => { describe('DsoPageEditButtonComponent', () => {
let component: DsoPageEditButtonComponent; let component: DsoPageEditButtonComponent;
let fixture: ComponentFixture<DsoPageEditButtonComponent>; let fixture: ComponentFixture<DsoPageEditButtonComponent>;
let authorizationService: AuthorizationDataService;
let dso: DSpaceObject;
beforeEach(async(() => { beforeEach(async(() => {
dso = Object.assign(new Item(), {
id: 'test-item',
_links: {
self: { href: 'test-item-selflink' }
}
});
authorizationService = jasmine.createSpyObj('authorizationService', {
isAuthorized: observableOf(true)
});
TestBed.configureTestingModule({ TestBed.configureTestingModule({
declarations: [ DsoPageEditButtonComponent ] declarations: [ DsoPageEditButtonComponent ],
}) imports: [TranslateModule.forRoot(), RouterTestingModule.withRoutes([]), TooltipModule.forRoot()],
.compileComponents(); providers: [
{ provide: AuthorizationDataService, useValue: authorizationService }
]
}).compileComponents();
})); }));
beforeEach(() => { beforeEach(() => {
fixture = TestBed.createComponent(DsoPageEditButtonComponent); fixture = TestBed.createComponent(DsoPageEditButtonComponent);
component = fixture.componentInstance; component = fixture.componentInstance;
component.dso = dso;
component.pageRoutePrefix = 'test';
fixture.detectChanges(); fixture.detectChanges();
}); });
it('should create', () => { it('should check the authorization of the current user', () => {
expect(component).toBeTruthy(); expect(authorizationService.isAuthorized).toHaveBeenCalledWith(FeatureID.CanEditMetadata, dso.self);
});
describe('when the user is authorized', () => {
beforeEach(() => {
(authorizationService.isAuthorized as jasmine.Spy).and.returnValue(observableOf(true));
component.ngOnInit();
fixture.detectChanges();
});
it('should render a link', () => {
const link = fixture.debugElement.query(By.css('a'));
expect(link).not.toBeNull();
});
});
describe('when the user is not authorized', () => {
beforeEach(() => {
(authorizationService.isAuthorized as jasmine.Spy).and.returnValue(observableOf(false));
component.ngOnInit();
fixture.detectChanges();
});
it('should not render a link', () => {
const link = fixture.debugElement.query(By.css('a'));
expect(link).toBeNull();
});
}); });
}); });

View File

@@ -9,14 +9,29 @@ import { FeatureID } from '../../../core/data/feature-authorization/feature-id';
templateUrl: './dso-page-edit-button.component.html', templateUrl: './dso-page-edit-button.component.html',
styleUrls: ['./dso-page-edit-button.component.scss'] styleUrls: ['./dso-page-edit-button.component.scss']
}) })
/**
* Display a button linking to the edit page of a DSpaceObject
*/
export class DsoPageEditButtonComponent implements OnInit { export class DsoPageEditButtonComponent implements OnInit {
/**
* The DSpaceObject to display a button to the edit page for
*/
@Input() dso: DSpaceObject; @Input() dso: DSpaceObject;
/**
* The prefix of the route to the edit page (before the object's UUID, e.g. "items")
*/
@Input() pageRoutePrefix: string; @Input() pageRoutePrefix: string;
/**
* A message for the tooltip on the button
* Supports i18n keys
*/
@Input() tooltipMsg: string; @Input() tooltipMsg: string;
/**
* Whether or not the current user is authorized to edit the DSpaceObject
*/
isAuthorized$: Observable<boolean>; isAuthorized$: Observable<boolean>;
constructor(protected authorizationService: AuthorizationDataService) { } constructor(protected authorizationService: AuthorizationDataService) { }