Skip to content

Commit 2cd3ea6

Browse files
committed
rewrite tests and fix observable state
1 parent 929ba8d commit 2cd3ea6

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/app/shared/file-download-link/file-download-link.component.spec.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,11 @@ describe('FileDownloadLinkComponent', () => {
117117
component.item = item;
118118
fixture.detectChanges();
119119
});
120+
it('should return canDownload truthy', () => {
121+
expect(component.canDownload$).toBeObservable(cold('-a', { a: true }));
122+
});
120123
it('should return the bitstreamPath based on the input bitstream', () => {
121124
expect(component.bitstreamPath$).toBeObservable(cold('-a', { a: { routerLink: new URLCombiner(getBitstreamModuleRoute(), bitstream.uuid, 'download').toString(), queryParams: {} } }));
122-
expect(component.canDownload$).toBeObservable(cold('--a', { a: true }));
123125

124126
});
125127
it('should init the component', () => {
@@ -151,9 +153,11 @@ describe('FileDownloadLinkComponent', () => {
151153
component.bitstream = bitstream;
152154
fixture.detectChanges();
153155
});
156+
it('should return canDownload falsy', () => {
157+
expect(component.canDownload$).toBeObservable(cold('-a', { a: false }));
158+
});
154159
it('should return the bitstreamPath based on the input bitstream', () => {
155-
expect(component.bitstreamPath$).toBeObservable(cold('-a', { a: { routerLink: new URLCombiner(getItemModuleRoute(), item.uuid, 'request-a-copy').toString(), queryParams: { bitstream: bitstream.uuid } } }));
156-
expect(component.canDownload$).toBeObservable(cold('--a', { a: false }));
160+
expect(component.bitstreamPath$).toBeObservable(cold('--a', { a: { routerLink: new URLCombiner(getItemModuleRoute(), item.uuid, 'request-a-copy').toString(), queryParams: { bitstream: bitstream.uuid } } }));
157161

158162
});
159163
it('should init the component', () => {
@@ -180,9 +184,11 @@ describe('FileDownloadLinkComponent', () => {
180184
component.item = item;
181185
fixture.detectChanges();
182186
});
187+
it('should return canDownload falsy', () => {
188+
expect(component.canDownload$).toBeObservable(cold('-a', { a: false }));
189+
});
183190
it('should return the bitstreamPath based on the input bitstream', () => {
184-
expect(component.bitstreamPath$).toBeObservable(cold('-a', { a: { routerLink: new URLCombiner(getBitstreamModuleRoute(), bitstream.uuid, 'download').toString(), queryParams: {} } }));
185-
expect(component.canDownload$).toBeObservable(cold('--a', { a: false }));
191+
expect(component.bitstreamPath$).toBeObservable(cold('--a', { a: { routerLink: new URLCombiner(getBitstreamModuleRoute(), bitstream.uuid, 'download').toString(), queryParams: {} } }));
186192

187193
});
188194
it('should init the component and show the locked icon', () => {
@@ -209,9 +215,11 @@ describe('FileDownloadLinkComponent', () => {
209215
component.item = item;
210216
fixture.detectChanges();
211217
});
218+
it('should return canDownload falsy', () => {
219+
expect(component.canDownload$).toBeObservable(cold('-a', { a: false }));
220+
});
212221
it('should return the bitstreamPath based on the access token and request-a-copy path', () => {
213-
expect(component.bitstreamPath$).toBeObservable(cold('-a', { a: { routerLink: new URLCombiner(getBitstreamModuleRoute(), bitstream.uuid, 'download').toString(), queryParams: { accessToken: 'abc123' } } }));
214-
expect(component.canDownload$).toBeObservable(cold('--a', { a: false }));
222+
expect(component.bitstreamPath$).toBeObservable(cold('--a', { a: { routerLink: new URLCombiner(getBitstreamModuleRoute(), bitstream.uuid, 'download').toString(), queryParams: { accessToken: 'abc123' } } }));
215223

216224
});
217225
it('should init the component and show an open lock', () => {

src/app/shared/file-download-link/file-download-link.component.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,19 @@ export class FileDownloadLinkComponent implements OnInit {
115115
if (this.enableRequestACopy) {
116116
// Obtain item request data from the route snapshot
117117
this.itemRequest = this.route.snapshot.data.itemRequest;
118-
// Set up observable to test access rights for a normal download
118+
// Set up observable to evaluate access rights for a normal download
119119
this.canDownload$ = this.authorizationService.isAuthorized(FeatureID.CanDownload, isNotEmpty(this.bitstream) ? this.bitstream.self : undefined);
120120
// Only set up and execute other observables if canDownload emits false
121121
this.bitstreamPath$ = this.canDownload$.pipe(
122122
switchMap(canDownload => {
123123
if (canDownload) {
124124
return of(this.getBitstreamDownloadPath());
125125
}
126-
// Set up and combine observables to test access rights to a valid token download and the request-a-copy feature
126+
// Set up and combine observables to evaluate access rights to a valid token download and the request-a-copy feature
127127
this.canDownloadWithToken$ = of((this.itemRequest && this.itemRequest.acceptRequest && !this.itemRequest.accessExpired) ? (this.itemRequest.allfiles !== false || this.itemRequest.bitstreamId === this.bitstream.uuid) : false);
128128
this.canRequestACopy$ = this.authorizationService.isAuthorized(FeatureID.CanRequestACopy, isNotEmpty(this.bitstream) ? this.bitstream.self : undefined);
129+
// Set up canDownload observable so the template can read the state
130+
this.canDownload$ = of(false);
129131
return observableCombineLatest([this.canDownloadWithToken$, this.canRequestACopy$]).pipe(
130132
map(([canDownloadWithToken, canRequestACopy]) => this.getBitstreamPathForRequestACopy(canDownloadWithToken, canRequestACopy)),
131133
);

0 commit comments

Comments
 (0)