65240: comcol form logo tests

This commit is contained in:
Kristof De Langhe
2019-10-02 13:04:15 +02:00
parent f3a032470d
commit 8a475f2523
3 changed files with 344 additions and 111 deletions

View File

@@ -15,6 +15,11 @@ import { NotificationsService } from '../../notifications/notifications.service'
import { NotificationsServiceStub } from '../../testing/notifications-service-stub'; import { NotificationsServiceStub } from '../../testing/notifications-service-stub';
import { AuthService } from '../../../core/auth/auth.service'; import { AuthService } from '../../../core/auth/auth.service';
import { AuthServiceMock } from '../../mocks/mock-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', () => { describe('ComColFormComponent', () => {
let comp: ComColFormComponent<DSpaceObject>; let comp: ComColFormComponent<DSpaceObject>;
@@ -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 */ /* tslint:disable:no-empty */
const locationStub = jasmine.createSpyObj('location', ['back']); const locationStub = jasmine.createSpyObj('location', ['back']);
/* tslint:enable:no-empty */ /* tslint:enable:no-empty */
@@ -64,24 +76,20 @@ describe('ComColFormComponent', () => {
providers: [ providers: [
{ provide: Location, useValue: locationStub }, { provide: Location, useValue: locationStub },
{ provide: DynamicFormService, useValue: formServiceStub }, { provide: DynamicFormService, useValue: formServiceStub },
{ provide: NotificationsService, useValue: new NotificationsServiceStub() }, { provide: NotificationsService, useValue: notificationsService },
{ provide: AuthService, useValue: new AuthServiceMock() } { provide: AuthService, useValue: new AuthServiceMock() }
], ],
schemas: [NO_ERRORS_SCHEMA] schemas: [NO_ERRORS_SCHEMA]
}).compileComponents(); }).compileComponents();
})); }));
describe('when the dso doesn\'t contain an ID (newly created)', () => {
beforeEach(() => { beforeEach(() => {
fixture = TestBed.createComponent(ComColFormComponent); initComponent(new Community());
comp = fixture.componentInstance;
comp.formModel = [];
comp.dso = new Community();
(comp as any).type = Community.type;
comp.uploaderComponent = Object.assign({
uploader: {}
}); });
fixture.detectChanges();
location = (comp as any).location; it('should initialize the uploadFilesOptions with a placeholder url', () => {
expect(comp.uploadFilesOptions.url.length).toBeGreaterThan(0);
}); });
describe('onSubmit', () => { describe('onSubmit', () => {
@@ -129,4 +137,113 @@ describe('ComColFormComponent', () => {
expect(locationStub.back).toHaveBeenCalled(); 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 = dso;
(comp as any).type = Community.type;
comp.uploaderComponent = Object.assign({
uploader: {}
});
(comp as any).dsoService = dsoService;
fixture.detectChanges();
location = (comp as any).location;
}
}); });

View File

@@ -11,7 +11,6 @@ import { RouterTestingModule } from '@angular/router/testing';
import { NO_ERRORS_SCHEMA } from '@angular/core'; import { NO_ERRORS_SCHEMA } from '@angular/core';
import { DSpaceObject } from '../../../core/shared/dspace-object.model'; import { DSpaceObject } from '../../../core/shared/dspace-object.model';
import { CreateComColPageComponent } from './create-comcol-page.component'; import { CreateComColPageComponent } from './create-comcol-page.component';
import { DataService } from '../../../core/data/data.service';
import { import {
createFailedRemoteDataObject$, createFailedRemoteDataObject$,
createSuccessfulRemoteDataObject$ createSuccessfulRemoteDataObject$
@@ -32,6 +31,8 @@ describe('CreateComColPageComponent', () => {
let routeServiceStub; let routeServiceStub;
let routerStub; let routerStub;
const logoEndpoint = 'rest/api/logo/endpoint';
function initializeVars() { function initializeVars() {
community = Object.assign(new Community(), { community = Object.assign(new Community(), {
uuid: 'a20da287-e174-466a-9926-f66b9300d347', uuid: 'a20da287-e174-466a-9926-f66b9300d347',
@@ -57,8 +58,8 @@ describe('CreateComColPageComponent', () => {
value: community.name value: community.name
}] }]
})), })),
create: (com, uuid?) => createSuccessfulRemoteDataObject$(newCommunity) create: (com, uuid?) => createSuccessfulRemoteDataObject$(newCommunity),
getLogoEndpoint: () => observableOf(logoEndpoint)
}; };
routeServiceStub = { routeServiceStub = {
@@ -96,6 +97,8 @@ describe('CreateComColPageComponent', () => {
describe('onSubmit', () => { describe('onSubmit', () => {
let data; let data;
describe('with an empty queue in the uploader', () => {
beforeEach(() => { beforeEach(() => {
data = { data = {
dso: Object.assign(new Community(), { dso: Object.assign(new Community(), {
@@ -109,10 +112,13 @@ describe('CreateComColPageComponent', () => {
url: '' url: ''
}, },
queue: [], queue: [],
uploadAll: {} /* tslint:disable:no-empty */
uploadAll: () => {}
/* tslint:enable:no-empty */
} }
}; };
}); });
it('should navigate when successful', () => { it('should navigate when successful', () => {
spyOn(router, 'navigate'); spyOn(router, 'navigate');
comp.onSubmit(data); comp.onSubmit(data);
@@ -128,4 +134,49 @@ describe('CreateComColPageComponent', () => {
expect(router.navigate).not.toHaveBeenCalled(); 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();
});
});
});
}); });

View File

@@ -10,12 +10,12 @@ import { RouterTestingModule } from '@angular/router/testing';
import { NO_ERRORS_SCHEMA } from '@angular/core'; import { NO_ERRORS_SCHEMA } from '@angular/core';
import { DSpaceObject } from '../../../core/shared/dspace-object.model'; import { DSpaceObject } from '../../../core/shared/dspace-object.model';
import { EditComColPageComponent } from './edit-comcol-page.component'; import { EditComColPageComponent } from './edit-comcol-page.component';
import { DataService } from '../../../core/data/data.service';
import { import {
createFailedRemoteDataObject$, createFailedRemoteDataObject$,
createSuccessfulRemoteDataObject$ createSuccessfulRemoteDataObject$
} from '../../testing/utils'; } from '../../testing/utils';
import { ComColDataService } from '../../../core/data/comcol-data.service'; import { ComColDataService } from '../../../core/data/comcol-data.service';
import { RemoteData } from '../../../core/data/remote-data';
describe('EditComColPageComponent', () => { describe('EditComColPageComponent', () => {
let comp: EditComColPageComponent<DSpaceObject>; let comp: EditComColPageComponent<DSpaceObject>;
@@ -29,6 +29,8 @@ describe('EditComColPageComponent', () => {
let routerStub; let routerStub;
let routeStub; let routeStub;
const logoEndpoint = 'rest/api/logo/endpoint';
function initializeVars() { function initializeVars() {
community = Object.assign(new Community(), { community = Object.assign(new Community(), {
uuid: 'a20da287-e174-466a-9926-f66b9300d347', uuid: 'a20da287-e174-466a-9926-f66b9300d347',
@@ -47,8 +49,8 @@ describe('EditComColPageComponent', () => {
}); });
communityDataServiceStub = { communityDataServiceStub = {
update: (com, uuid?) => createSuccessfulRemoteDataObject$(newCommunity) update: (com, uuid?) => createSuccessfulRemoteDataObject$(newCommunity),
getLogoEndpoint: () => observableOf(logoEndpoint)
}; };
routerStub = { routerStub = {
@@ -56,7 +58,9 @@ describe('EditComColPageComponent', () => {
}; };
routeStub = { routeStub = {
data: observableOf(community) data: observableOf({
dso: new RemoteData(false, false, true, null, community)
})
}; };
} }
@@ -84,6 +88,8 @@ describe('EditComColPageComponent', () => {
describe('onSubmit', () => { describe('onSubmit', () => {
let data; let data;
describe('with an empty queue in the uploader', () => {
beforeEach(() => { beforeEach(() => {
data = { data = {
dso: Object.assign(new Community(), { dso: Object.assign(new Community(), {
@@ -97,10 +103,13 @@ describe('EditComColPageComponent', () => {
url: '' url: ''
}, },
queue: [], queue: [],
uploadAll: {} /* tslint:disable:no-empty */
uploadAll: () => {}
/* tslint:enable:no-empty */
} }
} }
}); });
it('should navigate when successful', () => { it('should navigate when successful', () => {
spyOn(router, 'navigate'); spyOn(router, 'navigate');
comp.onSubmit(data); comp.onSubmit(data);
@@ -116,4 +125,60 @@ describe('EditComColPageComponent', () => {
expect(router.navigate).not.toHaveBeenCalled(); 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();
});
});
});
describe('navigateToHomePage', () => {
beforeEach(() => {
spyOn(router, 'navigate');
comp.navigateToHomePage();
});
it('should navigate', () => {
expect(router.navigate).toHaveBeenCalled();
});
});
}); });