From 2ad87c50d1aa9342c963f98f643de71726efabe1 Mon Sep 17 00:00:00 2001 From: Alexandre Vryghem Date: Wed, 27 Apr 2022 13:34:23 +0200 Subject: [PATCH] 90873: issue 1380 - Added tests --- .../upload/upload-bitstream.component.spec.ts | 96 +++++++++++++++++-- .../upload/upload-bitstream.component.ts | 4 +- 2 files changed, 91 insertions(+), 9 deletions(-) diff --git a/src/app/item-page/bitstreams/upload/upload-bitstream.component.spec.ts b/src/app/item-page/bitstreams/upload/upload-bitstream.component.spec.ts index a040613082..e85dca2a98 100644 --- a/src/app/item-page/bitstreams/upload/upload-bitstream.component.spec.ts +++ b/src/app/item-page/bitstreams/upload/upload-bitstream.component.spec.ts @@ -21,18 +21,23 @@ import { createPaginatedList } from '../../../shared/testing/utils.test'; import { RouterStub } from '../../../shared/testing/router.stub'; import { NotificationsServiceStub } from '../../../shared/testing/notifications-service.stub'; import { AuthServiceStub } from '../../../shared/testing/auth-service.stub'; +import { environment } from '../../../../environments/environment'; +import { buildPaginatedList } from '../../../core/data/paginated-list.model'; +import { PageInfo } from '../../../core/shared/page-info.model'; describe('UploadBitstreamComponent', () => { let comp: UploadBitstreamComponent; let fixture: ComponentFixture; + const customName = 'customBundleName'; const bundle = Object.assign(new Bundle(), { id: 'bundle', uuid: 'bundle', + name: customName, metadata: { 'dc.title': [ { - value: 'bundleName', + value: customName, language: null } ] @@ -41,14 +46,15 @@ describe('UploadBitstreamComponent', () => { self: { href: 'bundle-selflink' } } }); - const customName = 'Custom Name'; + const customCreatedName = 'customCreatedBundleName'; const createdBundle = Object.assign(new Bundle(), { id: 'created-bundle', uuid: 'created-bundle', + name: customCreatedName, metadata: { 'dc.title': [ { - value: customName, + value: customCreatedName, language: null } ] @@ -71,13 +77,14 @@ describe('UploadBitstreamComponent', () => { }, bundles: createSuccessfulRemoteDataObject$(createPaginatedList([bundle])) }); + const standardBundleSuggestions = environment.bundle.standardBundles; let routeStub; const routerStub = new RouterStub(); const restEndpoint = 'fake-rest-endpoint'; const mockItemDataService = jasmine.createSpyObj('mockItemDataService', { getBitstreamsEndpoint: observableOf(restEndpoint), createBundle: createSuccessfulRemoteDataObject$(createdBundle), - getBundles: createSuccessfulRemoteDataObject$([bundle]) + getBundles: createSuccessfulRemoteDataObject$(buildPaginatedList(new PageInfo(), [bundle])), }); const bundleService = jasmine.createSpyObj('bundleService', { getBitstreamsEndpoint: observableOf(restEndpoint), @@ -147,12 +154,24 @@ describe('UploadBitstreamComponent', () => { }); describe('and bundle name changed', () => { - beforeEach(() => { + beforeEach(waitForAsync(() => { + jasmine.getEnv().allowRespy(true); + mockItemDataService.getBundles.and.returnValue(createSuccessfulRemoteDataObject$(buildPaginatedList(new PageInfo(), [bundle]))); + loadFixtureAndComp(); + })); + + it('should clear out the selected id if the name doesn\'t exist', () => { + comp.selectedBundleName = ''; comp.bundleNameChange(); + + expect(comp.selectedBundleId).toBeUndefined(); }); - it('should clear out the selected id', () => { - expect(comp.selectedBundleId).toBeUndefined(); + it('should select the correct id if the name exist', () => { + comp.selectedBundleName = customName; + comp.bundleNameChange(); + + expect(comp.selectedBundleId).toEqual(bundle.id); }); }); }); @@ -186,6 +205,69 @@ describe('UploadBitstreamComponent', () => { }); }); + describe('when item has no bundles yet', () => { + beforeEach(waitForAsync(() => { + createUploadBitstreamTestingModule({ + bundle: bundle.id + }); + jasmine.getEnv().allowRespy(true); + mockItemDataService.getBundles.and.returnValue(createSuccessfulRemoteDataObject$(buildPaginatedList(new PageInfo(), []))); + loadFixtureAndComp(); + })); + + it('should display only the standard bundles in dropdown', () => { + expect(comp.bundles.length).toEqual(standardBundleSuggestions.length); + for (let i = 0; i < standardBundleSuggestions.length; i++) { + // noinspection JSDeprecatedSymbols + expect(comp.bundles[i].name).toEqual(standardBundleSuggestions[i]); + } + }); + }); + + describe('when item has a custom bundle', () => { + beforeEach(waitForAsync(() => { + createUploadBitstreamTestingModule({ + bundle: bundle.id + }); + jasmine.getEnv().allowRespy(true); + mockItemDataService.getBundles.and.returnValue(createSuccessfulRemoteDataObject$(buildPaginatedList(new PageInfo(), [bundle]))); + loadFixtureAndComp(); + })); + + it('should still display existing bitstream bundles if the bitstream has bundles', () => { + const expectedSuggestions = [bundle.name, ...standardBundleSuggestions]; + expect(comp.bundles.length).toEqual(expectedSuggestions.length); + for (let i = 0; i < expectedSuggestions.length; i++) { + // noinspection JSDeprecatedSymbols + expect(comp.bundles[i].name).toEqual(expectedSuggestions[i]); + } + }); + }); + + describe('when item has a standard bundle', () => { + let clonedBundle; + beforeEach(waitForAsync(() => { + clonedBundle = { ...bundle }; + expect(standardBundleSuggestions.length).toBeGreaterThan(0); + clonedBundle.name = standardBundleSuggestions[0]; + createUploadBitstreamTestingModule({ + bundle: clonedBundle.id + }); + jasmine.getEnv().allowRespy(true); + mockItemDataService.getBundles.and.returnValue(createSuccessfulRemoteDataObject$(buildPaginatedList(new PageInfo(), [clonedBundle]))); + loadFixtureAndComp(); + })); + + it('should not show duplicate bundle names', () => { + const expectedSuggestions = [clonedBundle.name, ...standardBundleSuggestions.filter((standardBundleSuggestion: string) => standardBundleSuggestion !== clonedBundle.name)]; + expect(comp.bundles.length).toEqual(expectedSuggestions.length); + for (let i = 0; i < expectedSuggestions.length; i++) { + // noinspection JSDeprecatedSymbols + expect(comp.bundles[i].name).toEqual(expectedSuggestions[i]); + } + }); + }); + /** * Setup an UploadBitstreamComponent testing module with custom queryParams for the route * @param queryParams diff --git a/src/app/item-page/bitstreams/upload/upload-bitstream.component.ts b/src/app/item-page/bitstreams/upload/upload-bitstream.component.ts index d825a40174..1e5295a347 100644 --- a/src/app/item-page/bitstreams/upload/upload-bitstream.component.ts +++ b/src/app/item-page/bitstreams/upload/upload-bitstream.component.ts @@ -141,11 +141,11 @@ export class UploadBitstreamComponent implements OnInit, OnDestroy { })); this.selectedBundleId = this.route.snapshot.queryParams.bundle; if (isNotEmpty(this.selectedBundleId)) { - this.bundleService.findById(this.selectedBundleId).pipe( + this.subs.push(this.bundleService.findById(this.selectedBundleId).pipe( getFirstSucceededRemoteDataPayload() ).subscribe((bundle: Bundle) => { this.selectedBundleName = bundle.name; - }); + })); this.setUploadUrl(); } this.subs.push(bundlesRD$.subscribe());