file-download-link.component.spec.ts (dspace-angular-dspace-7.0) | : | file-download-link.component.spec.ts (dspace-angular-dspace-7.1) | ||
---|---|---|---|---|
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; | import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; | |||
import { FileDownloadLinkComponent } from './file-download-link.component'; | import { FileDownloadLinkComponent } from './file-download-link.component'; | |||
import { AuthService } from '../../core/auth/auth.service'; | ||||
import { FileService } from '../../core/shared/file.service'; | ||||
import { of as observableOf } from 'rxjs'; | ||||
import { Bitstream } from '../../core/shared/bitstream.model'; | import { Bitstream } from '../../core/shared/bitstream.model'; | |||
import { By } from '@angular/platform-browser'; | import { By } from '@angular/platform-browser'; | |||
import { URLCombiner } from '../../core/url-combiner/url-combiner'; | import { URLCombiner } from '../../core/url-combiner/url-combiner'; | |||
import { getBitstreamModuleRoute } from '../../app-routing-paths'; | import { getBitstreamModuleRoute } from '../../app-routing-paths'; | |||
import { AuthorizationDataService } from '../../core/data/feature-authorization/ | ||||
authorization-data.service'; | ||||
import { cold, getTestScheduler } from 'jasmine-marbles'; | ||||
import { FeatureID } from '../../core/data/feature-authorization/feature-id'; | ||||
import { Item } from '../../core/shared/item.model'; | ||||
import { getItemModuleRoute } from '../../item-page/item-page-routing-paths'; | ||||
import { RouterLinkDirectiveStub } from '../testing/router-link-directive.stub'; | ||||
describe('FileDownloadLinkComponent', () => { | describe('FileDownloadLinkComponent', () => { | |||
let component: FileDownloadLinkComponent; | let component: FileDownloadLinkComponent; | |||
let fixture: ComponentFixture<FileDownloadLinkComponent>; | let fixture: ComponentFixture<FileDownloadLinkComponent>; | |||
let authService: AuthService; | let scheduler; | |||
let fileService: FileService; | let authorizationService: AuthorizationDataService; | |||
let bitstream: Bitstream; | let bitstream: Bitstream; | |||
let item: Item; | ||||
function init() { | function init() { | |||
authService = jasmine.createSpyObj('authService', { | authorizationService = jasmine.createSpyObj('authorizationService', { | |||
isAuthenticated: observableOf(true) | isAuthorized: cold('-a', {a: true}) | |||
}); | }); | |||
fileService = jasmine.createSpyObj('fileService', ['downloadFile']); | ||||
bitstream = Object.assign(new Bitstream(), { | bitstream = Object.assign(new Bitstream(), { | |||
uuid: 'bitstreamUuid', | uuid: 'bitstreamUuid', | |||
_links: { | ||||
self: {href: 'obj-selflink'} | ||||
} | ||||
}); | ||||
item = Object.assign(new Item(), { | ||||
uuid: 'itemUuid', | ||||
_links: { | ||||
self: {href: 'obj-selflink'} | ||||
} | ||||
}); | }); | |||
} | } | |||
beforeEach(waitForAsync(() => { | function initTestbed() { | |||
init(); | ||||
TestBed.configureTestingModule({ | TestBed.configureTestingModule({ | |||
declarations: [FileDownloadLinkComponent], | declarations: [FileDownloadLinkComponent, RouterLinkDirectiveStub], | |||
providers: [ | providers: [ | |||
{ provide: AuthService, useValue: authService }, | {provide: AuthorizationDataService, useValue: authorizationService}, | |||
{ provide: FileService, useValue: fileService }, | ||||
] | ] | |||
}) | }) | |||
.compileComponents(); | .compileComponents(); | |||
})); | } | |||
beforeEach(() => { | ||||
fixture = TestBed.createComponent(FileDownloadLinkComponent); | ||||
component = fixture.componentInstance; | ||||
component.bitstream = bitstream; | ||||
fixture.detectChanges(); | ||||
}); | ||||
describe('init', () => { | describe('init', () => { | |||
describe('getBitstreamPath', () => { | describe('getBitstreamPath', () => { | |||
it('should set the bitstreamPath based on the input bitstream', () => { | describe('when the user has download rights', () => { | |||
expect(component.bitstreamPath).toEqual(new URLCombiner(getBitstreamModu | beforeEach(waitForAsync(() => { | |||
leRoute(), bitstream.uuid, 'download').toString()); | scheduler = getTestScheduler(); | |||
init(); | ||||
initTestbed(); | ||||
})); | ||||
beforeEach(() => { | ||||
fixture = TestBed.createComponent(FileDownloadLinkComponent); | ||||
component = fixture.componentInstance; | ||||
component.bitstream = bitstream; | ||||
component.item = item; | ||||
fixture.detectChanges(); | ||||
}); | ||||
it('should return the bitstreamPath based on the input bitstream', () => | ||||
{ | ||||
expect(component.bitstreamPath$).toBeObservable(cold('-a', {a: { route | ||||
rLink: new URLCombiner(getBitstreamModuleRoute(), bitstream.uuid, 'download').to | ||||
String(), queryParams: {} }})); | ||||
expect(component.canDownload$).toBeObservable(cold('--a', {a: true})); | ||||
}); | ||||
it('should init the component', () => { | ||||
scheduler.flush(); | ||||
fixture.detectChanges(); | ||||
const link = fixture.debugElement.query(By.css('a')); | ||||
expect(link.injector.get(RouterLinkDirectiveStub).routerLink).toContai | ||||
n(new URLCombiner(getBitstreamModuleRoute(), bitstream.uuid, 'download').toStrin | ||||
g()); | ||||
const lock = fixture.debugElement.query(By.css('.fa-lock')); | ||||
expect(lock).toBeNull(); | ||||
}); | ||||
}); | ||||
describe('when the user has no download rights but has the right to reques | ||||
t a copy', () => { | ||||
beforeEach(waitForAsync(() => { | ||||
scheduler = getTestScheduler(); | ||||
init(); | ||||
(authorizationService.isAuthorized as jasmine.Spy).and.callFake((featu | ||||
reId, object) => { | ||||
if (featureId === FeatureID.CanDownload) { | ||||
return cold('-a', {a: false}); | ||||
} | ||||
return cold('-a', {a: true}); | ||||
}); | ||||
initTestbed(); | ||||
})); | ||||
beforeEach(() => { | ||||
fixture = TestBed.createComponent(FileDownloadLinkComponent); | ||||
component = fixture.componentInstance; | ||||
component.item = item; | ||||
component.bitstream = bitstream; | ||||
fixture.detectChanges(); | ||||
}); | ||||
it('should return the bitstreamPath based on the input bitstream', () => | ||||
{ | ||||
expect(component.bitstreamPath$).toBeObservable(cold('-a', {a: { route | ||||
rLink: new URLCombiner(getItemModuleRoute(), item.uuid, 'request-a-copy').toStri | ||||
ng(), queryParams: { bitstream: bitstream.uuid } }})); | ||||
expect(component.canDownload$).toBeObservable(cold('--a', {a: false})) | ||||
; | ||||
}); | ||||
it('should init the component', () => { | ||||
scheduler.flush(); | ||||
fixture.detectChanges(); | ||||
const link = fixture.debugElement.query(By.css('a')); | ||||
expect(link.injector.get(RouterLinkDirectiveStub).routerLink).toContai | ||||
n(new URLCombiner(getItemModuleRoute(), item.uuid, 'request-a-copy').toString()) | ||||
; | ||||
const lock = fixture.debugElement.query(By.css('.fa-lock')).nativeElem | ||||
ent; | ||||
expect(lock).toBeTruthy(); | ||||
}); | ||||
}); | ||||
describe('when the user has no download rights and no request a copy right | ||||
s', () => { | ||||
beforeEach(waitForAsync(() => { | ||||
scheduler = getTestScheduler(); | ||||
init(); | ||||
(authorizationService.isAuthorized as jasmine.Spy).and.returnValue(col | ||||
d('-a', {a: false})); | ||||
initTestbed(); | ||||
})); | ||||
beforeEach(() => { | ||||
fixture = TestBed.createComponent(FileDownloadLinkComponent); | ||||
component = fixture.componentInstance; | ||||
component.bitstream = bitstream; | ||||
component.item = item; | ||||
fixture.detectChanges(); | ||||
}); | ||||
it('should return the bitstreamPath based on the input bitstream', () => | ||||
{ | ||||
expect(component.bitstreamPath$).toBeObservable(cold('-a', {a: { route | ||||
rLink: new URLCombiner(getBitstreamModuleRoute(), bitstream.uuid, 'download').to | ||||
String(), queryParams: {} }})); | ||||
expect(component.canDownload$).toBeObservable(cold('--a', {a: false})) | ||||
; | ||||
}); | ||||
it('should init the component', () => { | ||||
scheduler.flush(); | ||||
fixture.detectChanges(); | ||||
const link = fixture.debugElement.query(By.css('a')); | ||||
expect(link.injector.get(RouterLinkDirectiveStub).routerLink).toContai | ||||
n(new URLCombiner(getBitstreamModuleRoute(), bitstream.uuid, 'download').toStrin | ||||
g()); | ||||
const lock = fixture.debugElement.query(By.css('.fa-lock')).nativeElem | ||||
ent; | ||||
expect(lock).toBeTruthy(); | ||||
}); | ||||
}); | }); | |||
}); | }); | |||
it('should init the component', () => { | ||||
const link = fixture.debugElement.query(By.css('a')).nativeElement; | ||||
expect(link.href).toContain(new URLCombiner(getBitstreamModuleRoute(), bit | ||||
stream.uuid, 'download').toString()); | ||||
}); | ||||
}); | }); | |||
}); | }); | |||
End of changes. 14 change blocks. | ||||
32 lines changed or deleted | 137 lines changed or added |