Files
dspace-angular/src/app/shared/dso-page/dso-page-orcid-button/dso-page-orcid-button.component.spec.ts

77 lines
2.7 KiB
TypeScript

import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
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 { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { DsoPageOrcidButtonComponent } from './dso-page-orcid-button.component';
describe('DsoPageOrcidButtonComponent', () => {
let component: DsoPageOrcidButtonComponent;
let fixture: ComponentFixture<DsoPageOrcidButtonComponent>;
let authorizationService: AuthorizationDataService;
let dso: DSpaceObject;
beforeEach(waitForAsync(() => {
dso = Object.assign(new Item(), {
id: 'test-item',
_links: {
self: { href: 'test-item-selflink' }
}
});
authorizationService = jasmine.createSpyObj('authorizationService', {
isAuthorized: observableOf(true)
});
TestBed.configureTestingModule({
declarations: [DsoPageOrcidButtonComponent],
imports: [TranslateModule.forRoot(), RouterTestingModule.withRoutes([]), NgbModule],
providers: [
{ provide: AuthorizationDataService, useValue: authorizationService }
]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(DsoPageOrcidButtonComponent);
component = fixture.componentInstance;
component.dso = dso;
component.pageRoute = 'test';
fixture.detectChanges();
});
it('should check the authorization of the current user', () => {
expect(authorizationService.isAuthorized).toHaveBeenCalledWith(FeatureID.CanSynchronizeWithORCID, 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();
});
});
});