diff --git a/src/app/shared/comcol-forms/comcol-form/comcol-form.component.spec.ts b/src/app/shared/comcol-forms/comcol-form/comcol-form.component.spec.ts index c896614ca9..455e2ef4e0 100644 --- a/src/app/shared/comcol-forms/comcol-form/comcol-form.component.spec.ts +++ b/src/app/shared/comcol-forms/comcol-form/comcol-form.component.spec.ts @@ -15,6 +15,11 @@ import { NotificationsService } from '../../notifications/notifications.service' import { NotificationsServiceStub } from '../../testing/notifications-service-stub'; import { AuthService } from '../../../core/auth/auth.service'; import { AuthServiceMock } from '../../mocks/mock-auth.service'; +import { of as observableOf } from 'rxjs'; +import { RemoteData } from '../../../core/data/remote-data'; +import { RestRequestMethod } from '../../../core/data/rest-request-method'; +import { ErrorResponse, RestResponse } from '../../../core/cache/response.models'; +import { RequestError } from '../../../core/data/request.models'; describe('ComColFormComponent', () => { let comp: ComColFormComponent; @@ -53,6 +58,13 @@ describe('ComColFormComponent', () => { }) ]; + const logoEndpoint = 'rest/api/logo/endpoint'; + const dsoService = Object.assign({ + getLogoEndpoint: () => observableOf(logoEndpoint), + deleteLogo: () => observableOf({}) + }); + const notificationsService = new NotificationsServiceStub(); + /* tslint:disable:no-empty */ const locationStub = jasmine.createSpyObj('location', ['back']); /* tslint:enable:no-empty */ @@ -64,69 +76,174 @@ describe('ComColFormComponent', () => { providers: [ { provide: Location, useValue: locationStub }, { provide: DynamicFormService, useValue: formServiceStub }, - { provide: NotificationsService, useValue: new NotificationsServiceStub() }, + { provide: NotificationsService, useValue: notificationsService }, { provide: AuthService, useValue: new AuthServiceMock() } ], schemas: [NO_ERRORS_SCHEMA] }).compileComponents(); })); - beforeEach(() => { + describe('when the dso doesn\'t contain an ID (newly created)', () => { + beforeEach(() => { + initComponent(new Community()); + }); + + it('should initialize the uploadFilesOptions with a placeholder url', () => { + expect(comp.uploadFilesOptions.url.length).toBeGreaterThan(0); + }); + + describe('onSubmit', () => { + beforeEach(() => { + spyOn(comp.submitForm, 'emit'); + comp.formModel = formModel; + }); + + it('should emit the new version of the community', () => { + comp.dso = Object.assign( + new Community(), + { + metadata: { + ...titleMD, + ...randomMD + } + } + ); + + comp.onSubmit(); + + expect(comp.submitForm.emit).toHaveBeenCalledWith( + { + dso: Object.assign( + {}, + new Community(), + { + metadata: { + ...newTitleMD, + ...randomMD, + ...abstractMD + }, + type: Community.type + }, + ), + uploader: {} + } + ); + }) + }); + + describe('onCancel', () => { + it('should call the back method on the Location service', () => { + comp.onCancel(); + expect(locationStub.back).toHaveBeenCalled(); + }); + }); + + describe('onCompleteItem', () => { + beforeEach(() => { + spyOn(comp.finishUpload, 'emit'); + comp.onCompleteItem(); + }); + + it('should show a success notification', () => { + expect(notificationsService.success).toHaveBeenCalled(); + }); + + it('should emit finishUpload', () => { + expect(comp.finishUpload.emit).toHaveBeenCalled(); + }); + }); + + describe('onUploadError', () => { + beforeEach(() => { + spyOn(comp.finishUpload, 'emit'); + comp.onUploadError(); + }); + + it('should show an error notification', () => { + expect(notificationsService.error).toHaveBeenCalled(); + }); + + it('should emit finishUpload', () => { + expect(comp.finishUpload.emit).toHaveBeenCalled(); + }); + }); + }); + + describe('when the dso contains an ID (being edited)', () => { + describe('and the dso doesn\'t contain a logo', () => { + beforeEach(() => { + initComponent(Object.assign(new Community(), { + id: 'community-id', + logo: observableOf(new RemoteData(false, false, true, null, undefined)) + })); + }); + + it('should initialize the uploadFilesOptions with the logo\'s endpoint url', () => { + expect(comp.uploadFilesOptions.url).toEqual(logoEndpoint); + }); + + it('should initialize the uploadFilesOptions with a POST method', () => { + expect(comp.uploadFilesOptions.method).toEqual(RestRequestMethod.POST); + }); + }); + + describe('and the dso contains a logo', () => { + beforeEach(() => { + initComponent(Object.assign(new Community(), { + id: 'community-id', + logo: observableOf(new RemoteData(false, false, true, null, {})) + })); + }); + + it('should initialize the uploadFilesOptions with the logo\'s endpoint url', () => { + expect(comp.uploadFilesOptions.url).toEqual(logoEndpoint); + }); + + it('should initialize the uploadFilesOptions with a PUT method', () => { + expect(comp.uploadFilesOptions.method).toEqual(RestRequestMethod.PUT); + }); + + describe('deleteLogo', () => { + describe('when dsoService.deleteLogo returns a successful response', () => { + const response = new RestResponse(true, 200, 'OK'); + + beforeEach(() => { + spyOn(dsoService, 'deleteLogo').and.returnValue(observableOf(response)); + comp.deleteLogo(); + }); + + it('should display a success notification', () => { + expect(notificationsService.success).toHaveBeenCalled(); + }); + }); + + describe('when dsoService.deleteLogo returns an error response', () => { + const response = new ErrorResponse(new RequestError('errorMessage')); + + beforeEach(() => { + spyOn(dsoService, 'deleteLogo').and.returnValue(observableOf(response)); + comp.deleteLogo(); + }); + + it('should display an error notification', () => { + expect(notificationsService.error).toHaveBeenCalled(); + }); + }); + }); + }); + }); + + function initComponent(dso: Community) { fixture = TestBed.createComponent(ComColFormComponent); comp = fixture.componentInstance; comp.formModel = []; - comp.dso = new Community(); + comp.dso = dso; (comp as any).type = Community.type; comp.uploaderComponent = Object.assign({ uploader: {} }); + (comp as any).dsoService = dsoService; fixture.detectChanges(); location = (comp as any).location; - }); - - describe('onSubmit', () => { - beforeEach(() => { - spyOn(comp.submitForm, 'emit'); - comp.formModel = formModel; - }); - - it('should emit the new version of the community', () => { - comp.dso = Object.assign( - new Community(), - { - metadata: { - ...titleMD, - ...randomMD - } - } - ); - - comp.onSubmit(); - - expect(comp.submitForm.emit).toHaveBeenCalledWith( - { - dso: Object.assign( - {}, - new Community(), - { - metadata: { - ...newTitleMD, - ...randomMD, - ...abstractMD - }, - type: Community.type - }, - ), - uploader: {} - } - ); - }) - }); - - describe('onCancel', () => { - it('should call the back method on the Location service', () => { - comp.onCancel(); - expect(locationStub.back).toHaveBeenCalled(); - }); - }); + } }); diff --git a/src/app/shared/comcol-forms/create-comcol-page/create-comcol-page.component.spec.ts b/src/app/shared/comcol-forms/create-comcol-page/create-comcol-page.component.spec.ts index bc468fada1..99aac6757e 100644 --- a/src/app/shared/comcol-forms/create-comcol-page/create-comcol-page.component.spec.ts +++ b/src/app/shared/comcol-forms/create-comcol-page/create-comcol-page.component.spec.ts @@ -11,7 +11,6 @@ import { RouterTestingModule } from '@angular/router/testing'; import { NO_ERRORS_SCHEMA } from '@angular/core'; import { DSpaceObject } from '../../../core/shared/dspace-object.model'; import { CreateComColPageComponent } from './create-comcol-page.component'; -import { DataService } from '../../../core/data/data.service'; import { createFailedRemoteDataObject$, createSuccessfulRemoteDataObject$ @@ -32,6 +31,8 @@ describe('CreateComColPageComponent', () => { let routeServiceStub; let routerStub; + const logoEndpoint = 'rest/api/logo/endpoint'; + function initializeVars() { community = Object.assign(new Community(), { uuid: 'a20da287-e174-466a-9926-f66b9300d347', @@ -57,8 +58,8 @@ describe('CreateComColPageComponent', () => { value: community.name }] })), - create: (com, uuid?) => createSuccessfulRemoteDataObject$(newCommunity) - + create: (com, uuid?) => createSuccessfulRemoteDataObject$(newCommunity), + getLogoEndpoint: () => observableOf(logoEndpoint) }; routeServiceStub = { @@ -96,36 +97,86 @@ describe('CreateComColPageComponent', () => { describe('onSubmit', () => { let data; - beforeEach(() => { - data = { - dso: Object.assign(new Community(), { - metadata: [{ - key: 'dc.title', - value: 'test' - }] - }), - uploader: { - options: { - url: '' - }, - queue: [], - uploadAll: {} - } - }; - }); - it('should navigate when successful', () => { - spyOn(router, 'navigate'); - comp.onSubmit(data); - fixture.detectChanges(); - expect(router.navigate).toHaveBeenCalled(); + + describe('with an empty queue in the uploader', () => { + beforeEach(() => { + data = { + dso: Object.assign(new Community(), { + metadata: [{ + key: 'dc.title', + value: 'test' + }] + }), + uploader: { + options: { + url: '' + }, + queue: [], + /* tslint:disable:no-empty */ + uploadAll: () => {} + /* tslint:enable:no-empty */ + } + }; + }); + + it('should navigate when successful', () => { + spyOn(router, 'navigate'); + comp.onSubmit(data); + fixture.detectChanges(); + expect(router.navigate).toHaveBeenCalled(); + }); + + it('should not navigate on failure', () => { + spyOn(router, 'navigate'); + spyOn(dsoDataService, 'create').and.returnValue(createFailedRemoteDataObject$(newCommunity)); + comp.onSubmit(data); + fixture.detectChanges(); + expect(router.navigate).not.toHaveBeenCalled(); + }); }); - it('should not navigate on failure', () => { - spyOn(router, 'navigate'); - spyOn(dsoDataService, 'create').and.returnValue(createFailedRemoteDataObject$(newCommunity)); - comp.onSubmit(data); - fixture.detectChanges(); - expect(router.navigate).not.toHaveBeenCalled(); + describe('with at least one item in the uploader\'s queue', () => { + beforeEach(() => { + data = { + dso: Object.assign(new Community(), { + metadata: [{ + key: 'dc.title', + value: 'test' + }] + }), + uploader: { + options: { + url: '' + }, + queue: [ + {} + ], + /* tslint:disable:no-empty */ + uploadAll: () => {} + /* tslint:enable:no-empty */ + } + }; + }); + + it('should not navigate', () => { + spyOn(router, 'navigate'); + comp.onSubmit(data); + fixture.detectChanges(); + expect(router.navigate).not.toHaveBeenCalled(); + }); + + it('should set the uploader\'s url to the logo\'s endpoint', () => { + comp.onSubmit(data); + fixture.detectChanges(); + expect(data.uploader.options.url).toEqual(logoEndpoint); + }); + + it('should call the uploader\'s uploadAll', () => { + spyOn(data.uploader, 'uploadAll'); + comp.onSubmit(data); + fixture.detectChanges(); + expect(data.uploader.uploadAll).toHaveBeenCalled(); + }); }); }); }); diff --git a/src/app/shared/comcol-forms/edit-comcol-page/edit-comcol-page.component.spec.ts b/src/app/shared/comcol-forms/edit-comcol-page/edit-comcol-page.component.spec.ts index d9fb59fe9e..2a52049df5 100644 --- a/src/app/shared/comcol-forms/edit-comcol-page/edit-comcol-page.component.spec.ts +++ b/src/app/shared/comcol-forms/edit-comcol-page/edit-comcol-page.component.spec.ts @@ -10,12 +10,12 @@ import { RouterTestingModule } from '@angular/router/testing'; import { NO_ERRORS_SCHEMA } from '@angular/core'; import { DSpaceObject } from '../../../core/shared/dspace-object.model'; import { EditComColPageComponent } from './edit-comcol-page.component'; -import { DataService } from '../../../core/data/data.service'; import { createFailedRemoteDataObject$, createSuccessfulRemoteDataObject$ } from '../../testing/utils'; import { ComColDataService } from '../../../core/data/comcol-data.service'; +import { RemoteData } from '../../../core/data/remote-data'; describe('EditComColPageComponent', () => { let comp: EditComColPageComponent; @@ -29,6 +29,8 @@ describe('EditComColPageComponent', () => { let routerStub; let routeStub; + const logoEndpoint = 'rest/api/logo/endpoint'; + function initializeVars() { community = Object.assign(new Community(), { uuid: 'a20da287-e174-466a-9926-f66b9300d347', @@ -47,8 +49,8 @@ describe('EditComColPageComponent', () => { }); communityDataServiceStub = { - update: (com, uuid?) => createSuccessfulRemoteDataObject$(newCommunity) - + update: (com, uuid?) => createSuccessfulRemoteDataObject$(newCommunity), + getLogoEndpoint: () => observableOf(logoEndpoint) }; routerStub = { @@ -56,7 +58,9 @@ describe('EditComColPageComponent', () => { }; routeStub = { - data: observableOf(community) + data: observableOf({ + dso: new RemoteData(false, false, true, null, community) + }) }; } @@ -84,36 +88,97 @@ describe('EditComColPageComponent', () => { describe('onSubmit', () => { let data; - beforeEach(() => { - data = { - dso: Object.assign(new Community(), { - metadata: [{ - key: 'dc.title', - value: 'test' - }] - }), - uploader: { - options: { - url: '' - }, - queue: [], - uploadAll: {} + + describe('with an empty queue in the uploader', () => { + beforeEach(() => { + data = { + dso: Object.assign(new Community(), { + metadata: [{ + key: 'dc.title', + value: 'test' + }] + }), + uploader: { + options: { + url: '' + }, + queue: [], + /* tslint:disable:no-empty */ + uploadAll: () => {} + /* tslint:enable:no-empty */ + } } - } - }); - it('should navigate when successful', () => { - spyOn(router, 'navigate'); - comp.onSubmit(data); - fixture.detectChanges(); - expect(router.navigate).toHaveBeenCalled(); + }); + + it('should navigate when successful', () => { + spyOn(router, 'navigate'); + comp.onSubmit(data); + fixture.detectChanges(); + expect(router.navigate).toHaveBeenCalled(); + }); + + it('should not navigate on failure', () => { + spyOn(router, 'navigate'); + spyOn(dsoDataService, 'update').and.returnValue(createFailedRemoteDataObject$(newCommunity)); + comp.onSubmit(data); + fixture.detectChanges(); + expect(router.navigate).not.toHaveBeenCalled(); + }); }); - it('should not navigate on failure', () => { + describe('with at least one item in the uploader\'s queue', () => { + beforeEach(() => { + data = { + dso: Object.assign(new Community(), { + metadata: [{ + key: 'dc.title', + value: 'test' + }] + }), + uploader: { + options: { + url: '' + }, + queue: [ + {} + ], + /* tslint:disable:no-empty */ + uploadAll: () => {} + /* tslint:enable:no-empty */ + } + } + }); + + it('should not navigate', () => { + spyOn(router, 'navigate'); + comp.onSubmit(data); + fixture.detectChanges(); + expect(router.navigate).not.toHaveBeenCalled(); + }); + + it('should set the uploader\'s url to the logo\'s endpoint', () => { + comp.onSubmit(data); + fixture.detectChanges(); + expect(data.uploader.options.url).toEqual(logoEndpoint); + }); + + it('should call the uploader\'s uploadAll', () => { + spyOn(data.uploader, 'uploadAll'); + comp.onSubmit(data); + fixture.detectChanges(); + expect(data.uploader.uploadAll).toHaveBeenCalled(); + }); + }); + }); + + describe('navigateToHomePage', () => { + beforeEach(() => { spyOn(router, 'navigate'); - spyOn(dsoDataService, 'update').and.returnValue(createFailedRemoteDataObject$(newCommunity)); - comp.onSubmit(data); - fixture.detectChanges(); - expect(router.navigate).not.toHaveBeenCalled(); + comp.navigateToHomePage(); + }); + + it('should navigate', () => { + expect(router.navigate).toHaveBeenCalled(); }); }); });