|
| 1 | +import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; |
| 2 | +import { By } from '@angular/platform-browser'; |
| 3 | +import { RouterTestingModule } from '@angular/router/testing'; |
| 4 | + |
| 5 | +import { of as observableOf } from 'rxjs'; |
| 6 | +import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; |
| 7 | +import { TranslateModule } from '@ngx-translate/core'; |
| 8 | + |
| 9 | +import { OrcidViewPageMenuComponent } from './orcid-view-page-menu.component'; |
| 10 | +import { DsoPageOrcidButtonComponent } from '../../dso-page/dso-page-orcid-button/dso-page-orcid-button.component'; |
| 11 | +import { AuthorizationDataService } from '../../../core/data/feature-authorization/authorization-data.service'; |
| 12 | +import { DSpaceObject } from '../../../core/shared/dspace-object.model'; |
| 13 | +import { Item } from '../../../core/shared/item.model'; |
| 14 | +import { FeatureID } from '../../../core/data/feature-authorization/feature-id'; |
| 15 | +import { DSpaceObjectType } from '../../../core/shared/dspace-object-type.model'; |
| 16 | + |
| 17 | +describe('OrcidViewPageMenuComponent', () => { |
| 18 | + let component: DsoPageOrcidButtonComponent; |
| 19 | + let fixture: ComponentFixture<DsoPageOrcidButtonComponent>; |
| 20 | + |
| 21 | + let authorizationService: AuthorizationDataService; |
| 22 | + let dso: DSpaceObject; |
| 23 | + |
| 24 | + beforeEach(waitForAsync(() => { |
| 25 | + dso = Object.assign(new Item(), { |
| 26 | + id: 'test-item', |
| 27 | + _links: { |
| 28 | + self: { href: 'test-item-selflink' } |
| 29 | + } |
| 30 | + }); |
| 31 | + authorizationService = jasmine.createSpyObj('authorizationService', { |
| 32 | + isAuthorized: observableOf(true) |
| 33 | + }); |
| 34 | + TestBed.configureTestingModule({ |
| 35 | + declarations: [DsoPageOrcidButtonComponent], |
| 36 | + imports: [TranslateModule.forRoot(), RouterTestingModule.withRoutes([]), NgbModule], |
| 37 | + providers: [ |
| 38 | + { provide: 'contextMenuObjectProvider', useValue: dso }, |
| 39 | + { provide: 'contextMenuObjectTypeProvider', useValue: DSpaceObjectType.ITEM }, |
| 40 | + { provide: AuthorizationDataService, useValue: authorizationService } |
| 41 | + ] |
| 42 | + }).compileComponents(); |
| 43 | + })); |
| 44 | + |
| 45 | + beforeEach(() => { |
| 46 | + fixture = TestBed.createComponent(DsoPageOrcidButtonComponent); |
| 47 | + component = fixture.componentInstance; |
| 48 | + fixture.detectChanges(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should check the authorization of the current user', () => { |
| 52 | + expect(authorizationService.isAuthorized).toHaveBeenCalledWith(FeatureID.CanSynchronizeWithORCID, dso.self); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('when the user is authorized', () => { |
| 56 | + beforeEach(() => { |
| 57 | + (authorizationService.isAuthorized as jasmine.Spy).and.returnValue(observableOf(true)); |
| 58 | + component.ngOnInit(); |
| 59 | + fixture.detectChanges(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should render a link', () => { |
| 63 | + const link = fixture.debugElement.query(By.css('button')); |
| 64 | + expect(link).not.toBeNull(); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('when the user is not authorized', () => { |
| 69 | + beforeEach(() => { |
| 70 | + (authorizationService.isAuthorized as jasmine.Spy).and.returnValue(observableOf(false)); |
| 71 | + component.ngOnInit(); |
| 72 | + fixture.detectChanges(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should not render a link', () => { |
| 76 | + const link = fixture.debugElement.query(By.css('button')); |
| 77 | + expect(link).toBeNull(); |
| 78 | + }); |
| 79 | + }); |
| 80 | +}); |
0 commit comments