90873: issue 1380 - Added tests

This commit is contained in:
Alexandre Vryghem
2022-04-27 13:34:23 +02:00
parent d51af2739e
commit 2ad87c50d1
2 changed files with 91 additions and 9 deletions

View File

@@ -21,18 +21,23 @@ import { createPaginatedList } from '../../../shared/testing/utils.test';
import { RouterStub } from '../../../shared/testing/router.stub'; import { RouterStub } from '../../../shared/testing/router.stub';
import { NotificationsServiceStub } from '../../../shared/testing/notifications-service.stub'; import { NotificationsServiceStub } from '../../../shared/testing/notifications-service.stub';
import { AuthServiceStub } from '../../../shared/testing/auth-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', () => { describe('UploadBitstreamComponent', () => {
let comp: UploadBitstreamComponent; let comp: UploadBitstreamComponent;
let fixture: ComponentFixture<UploadBitstreamComponent>; let fixture: ComponentFixture<UploadBitstreamComponent>;
const customName = 'customBundleName';
const bundle = Object.assign(new Bundle(), { const bundle = Object.assign(new Bundle(), {
id: 'bundle', id: 'bundle',
uuid: 'bundle', uuid: 'bundle',
name: customName,
metadata: { metadata: {
'dc.title': [ 'dc.title': [
{ {
value: 'bundleName', value: customName,
language: null language: null
} }
] ]
@@ -41,14 +46,15 @@ describe('UploadBitstreamComponent', () => {
self: { href: 'bundle-selflink' } self: { href: 'bundle-selflink' }
} }
}); });
const customName = 'Custom Name'; const customCreatedName = 'customCreatedBundleName';
const createdBundle = Object.assign(new Bundle(), { const createdBundle = Object.assign(new Bundle(), {
id: 'created-bundle', id: 'created-bundle',
uuid: 'created-bundle', uuid: 'created-bundle',
name: customCreatedName,
metadata: { metadata: {
'dc.title': [ 'dc.title': [
{ {
value: customName, value: customCreatedName,
language: null language: null
} }
] ]
@@ -71,13 +77,14 @@ describe('UploadBitstreamComponent', () => {
}, },
bundles: createSuccessfulRemoteDataObject$(createPaginatedList([bundle])) bundles: createSuccessfulRemoteDataObject$(createPaginatedList([bundle]))
}); });
const standardBundleSuggestions = environment.bundle.standardBundles;
let routeStub; let routeStub;
const routerStub = new RouterStub(); const routerStub = new RouterStub();
const restEndpoint = 'fake-rest-endpoint'; const restEndpoint = 'fake-rest-endpoint';
const mockItemDataService = jasmine.createSpyObj('mockItemDataService', { const mockItemDataService = jasmine.createSpyObj('mockItemDataService', {
getBitstreamsEndpoint: observableOf(restEndpoint), getBitstreamsEndpoint: observableOf(restEndpoint),
createBundle: createSuccessfulRemoteDataObject$(createdBundle), createBundle: createSuccessfulRemoteDataObject$(createdBundle),
getBundles: createSuccessfulRemoteDataObject$([bundle]) getBundles: createSuccessfulRemoteDataObject$(buildPaginatedList(new PageInfo(), [bundle])),
}); });
const bundleService = jasmine.createSpyObj('bundleService', { const bundleService = jasmine.createSpyObj('bundleService', {
getBitstreamsEndpoint: observableOf(restEndpoint), getBitstreamsEndpoint: observableOf(restEndpoint),
@@ -147,12 +154,24 @@ describe('UploadBitstreamComponent', () => {
}); });
describe('and bundle name changed', () => { 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(); comp.bundleNameChange();
expect(comp.selectedBundleId).toBeUndefined();
}); });
it('should clear out the selected id', () => { it('should select the correct id if the name exist', () => {
expect(comp.selectedBundleId).toBeUndefined(); 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 * Setup an UploadBitstreamComponent testing module with custom queryParams for the route
* @param queryParams * @param queryParams

View File

@@ -141,11 +141,11 @@ export class UploadBitstreamComponent implements OnInit, OnDestroy {
})); }));
this.selectedBundleId = this.route.snapshot.queryParams.bundle; this.selectedBundleId = this.route.snapshot.queryParams.bundle;
if (isNotEmpty(this.selectedBundleId)) { if (isNotEmpty(this.selectedBundleId)) {
this.bundleService.findById(this.selectedBundleId).pipe( this.subs.push(this.bundleService.findById(this.selectedBundleId).pipe(
getFirstSucceededRemoteDataPayload() getFirstSucceededRemoteDataPayload()
).subscribe((bundle: Bundle) => { ).subscribe((bundle: Bundle) => {
this.selectedBundleName = bundle.name; this.selectedBundleName = bundle.name;
}); }));
this.setUploadUrl(); this.setUploadUrl();
} }
this.subs.push(bundlesRD$.subscribe()); this.subs.push(bundlesRD$.subscribe());