From ed2c935f7e0ef088850053f644acb91d573a5a33 Mon Sep 17 00:00:00 2001 From: Kristof De Langhe Date: Sat, 14 Mar 2020 15:00:19 +0100 Subject: [PATCH] 68346: Test cases --- .../upload/upload-bitstream.component.spec.ts | 99 ++++++++++++++++++- src/app/core/data/item-data.service.spec.ts | 22 ++++- 2 files changed, 116 insertions(+), 5 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 3fe1afad19..f7ec9b16fb 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 @@ -23,18 +23,43 @@ import { VarDirective } from '../../../shared/utils/var.directive'; import { Bitstream } from '../../../core/shared/bitstream.model'; import { BundleDataService } from '../../../core/data/bundle-data.service'; import { Bundle } from '../../../core/shared/bundle.model'; +import { By } from '@angular/platform-browser'; -describe('UploadBistreamComponent', () => { +fdescribe('UploadBistreamComponent', () => { let comp: UploadBitstreamComponent; let fixture: ComponentFixture; const bundle = Object.assign(new Bundle(), { id: 'bundle', uuid: 'bundle', + metadata: { + 'dc.title': [ + { + value: 'bundleName', + language: null + } + ] + }, _links: { self: { href: 'bundle-selflink' } } }); + const customName = 'Custom Name'; + const createdBundle = Object.assign(new Bundle(), { + id: 'created-bundle', + uuid: 'created-bundle', + metadata: { + 'dc.title': [ + { + value: customName, + language: null + } + ] + }, + _links: { + self: { href: 'created-bundle-selflink' } + } + }); const itemName = 'fake-name'; const mockItem = Object.assign(new Item(), { id: 'fake-id', @@ -53,16 +78,19 @@ describe('UploadBistreamComponent', () => { const routerStub = new RouterStub(); const restEndpoint = 'fake-rest-endpoint'; const mockItemDataService = jasmine.createSpyObj('mockItemDataService', { - getBitstreamsEndpoint: observableOf(restEndpoint) + getBitstreamsEndpoint: observableOf(restEndpoint), + createBundle: createSuccessfulRemoteDataObject$(createdBundle) }); const bundleService = jasmine.createSpyObj('bundleService', { - getBitstreamsEndpoint: observableOf(restEndpoint) + getBitstreamsEndpoint: observableOf(restEndpoint), + findById: createSuccessfulRemoteDataObject$(bundle) }); const authToken = 'fake-auth-token'; const authServiceStub = Object.assign(new AuthServiceStub(), { buildAuthHeader: () => authToken }); const notificationsServiceStub = new NotificationsServiceStub(); + const uploaderComponent = jasmine.createSpyObj('uploaderComponent', ['ngOnInit']); describe('when a file is uploaded', () => { beforeEach(async(() => { @@ -98,6 +126,65 @@ describe('UploadBistreamComponent', () => { }); }); + describe('when a bundle url parameter is present', () => { + beforeEach(async(() => { + createUploadBitstreamTestingModule({ + bundle: bundle.id + }); + })); + + beforeEach(() => { + loadFixtureAndComp(); + }); + + it('should set the selected id to the bundle\'s id', () => { + expect(comp.selectedBundleId).toEqual(bundle.id); + }); + + it('should set the selected name to the bundle\'s name', () => { + expect(comp.selectedBundleName).toEqual(bundle.name); + }); + + describe('and bundle name changed', () => { + beforeEach(() => { + comp.bundleNameChange(); + }); + + it('should clear out the selected id', () => { + expect(comp.selectedBundleId).toBeUndefined(); + }); + }); + }); + + describe('when a name is filled in, but no ID is selected', () => { + beforeEach(async(() => { + createUploadBitstreamTestingModule({}); + })); + + beforeEach(() => { + loadFixtureAndComp(); + comp.selectedBundleName = customName; + }); + + describe('createBundle', () => { + beforeEach(() => { + comp.createBundle(); + }); + + it('should create a new bundle', () => { + expect(mockItemDataService.createBundle).toHaveBeenCalledWith(mockItem.id, customName); + }); + + it('should set the selected id to the id of the new bundle', () => { + expect(comp.selectedBundleId).toEqual(createdBundle.id); + }); + + it('should display a success notification', () => { + expect(notificationsServiceStub.success).toHaveBeenCalled(); + }); + }); + }); + /** * Setup an UploadBitstreamComponent testing module with custom queryParams for the route * @param queryParams @@ -109,7 +196,10 @@ describe('UploadBistreamComponent', () => { }), queryParams: observableOf(queryParams), snapshot: { - queryParams: queryParams + queryParams: queryParams, + params: { + id: mockItem.id + } } }; @@ -135,6 +225,7 @@ describe('UploadBistreamComponent', () => { function loadFixtureAndComp() { fixture = TestBed.createComponent(UploadBitstreamComponent); comp = fixture.componentInstance; + comp.uploaderComponent = uploaderComponent; fixture.detectChanges(); } diff --git a/src/app/core/data/item-data.service.spec.ts b/src/app/core/data/item-data.service.spec.ts index 06adfd5143..2519c90973 100644 --- a/src/app/core/data/item-data.service.spec.ts +++ b/src/app/core/data/item-data.service.spec.ts @@ -47,6 +47,9 @@ describe('ItemDataService', () => { return cold('a', { a: itemEndpoint }); } } as HALEndpointService; + const bundleService = jasmine.createSpyObj('bundleService', { + findByHref: {} + }); const scopeID = '4af28e99-6a9c-4036-a199-e1b587046d39'; const options = Object.assign(new FindListOptions(), { @@ -87,7 +90,8 @@ describe('ItemDataService', () => { halEndpointService, notificationsService, http, - comparator + comparator, + bundleService ); } @@ -212,4 +216,20 @@ describe('ItemDataService', () => { }); }); + describe('createBundle', () => { + const itemId = '3de6ea60-ec39-419b-ae6f-065930ac1429'; + const bundleName = 'ORIGINAL'; + let result; + + beforeEach(() => { + service = initTestService(); + spyOn(requestService, 'configure'); + result = service.createBundle(itemId, bundleName); + }); + + it('should configure a POST request', () => { + result.subscribe(() => expect(requestService.configure).toHaveBeenCalledWith(jasmine.any(PostRequest))); + }); + }); + });