mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-18 07:23:03 +00:00
65272: Added test cases
This commit is contained in:
@@ -4,23 +4,38 @@ import { SharedModule } from '../../../shared/shared.module';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { RouterTestingModule } from '@angular/router/testing';
|
||||
import { CollectionDataService } from '../../../core/data/collection-data.service';
|
||||
import { ActivatedRoute } from '@angular/router';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
import { of as observableOf } from 'rxjs/internal/observable/of';
|
||||
import { NO_ERRORS_SCHEMA } from '@angular/core';
|
||||
import { CollectionMetadataComponent } from './collection-metadata.component';
|
||||
import { Item } from '../../../core/shared/item.model';
|
||||
import { createSuccessfulRemoteDataObject$ } from '../../../shared/testing/utils';
|
||||
import { createSuccessfulRemoteDataObject, createSuccessfulRemoteDataObject$ } from '../../../shared/testing/utils';
|
||||
import { ItemTemplateDataService } from '../../../core/data/item-template-data.service';
|
||||
import { NotificationsService } from '../../../shared/notifications/notifications.service';
|
||||
import { Collection } from '../../../core/shared/collection.model';
|
||||
|
||||
describe('CollectionMetadataComponent', () => {
|
||||
let comp: CollectionMetadataComponent;
|
||||
let fixture: ComponentFixture<CollectionMetadataComponent>;
|
||||
let router: Router;
|
||||
let itemTemplateService: ItemTemplateDataService;
|
||||
|
||||
const template = new Item();
|
||||
const collection = Object.assign(new Collection(), {
|
||||
uuid: 'collection-id',
|
||||
id: 'collection-id',
|
||||
name: 'Fake Collection'
|
||||
});
|
||||
|
||||
const itemTemplateService = Object.assign({
|
||||
findByCollectionID: () => createSuccessfulRemoteDataObject$(template)
|
||||
const itemTemplateServiceStub = Object.assign({
|
||||
findByCollectionID: () => createSuccessfulRemoteDataObject$(template),
|
||||
create: () => createSuccessfulRemoteDataObject$(template),
|
||||
deleteByCollectionID: () => observableOf(true)
|
||||
});
|
||||
|
||||
const notificationsService = jasmine.createSpyObj('notificationsService', {
|
||||
success: {},
|
||||
error: {}
|
||||
});
|
||||
|
||||
beforeEach(async(() => {
|
||||
@@ -29,9 +44,9 @@ describe('CollectionMetadataComponent', () => {
|
||||
declarations: [CollectionMetadataComponent],
|
||||
providers: [
|
||||
{ provide: CollectionDataService, useValue: {} },
|
||||
{ provide: ItemTemplateDataService, useValue: itemTemplateService },
|
||||
{ provide: ActivatedRoute, useValue: { parent: { data: observableOf({ dso: { payload: {} } }) } } },
|
||||
{ provide: NotificationsService, useValue: {} }
|
||||
{ provide: ItemTemplateDataService, useValue: itemTemplateServiceStub },
|
||||
{ provide: ActivatedRoute, useValue: { parent: { data: observableOf({ dso: createSuccessfulRemoteDataObject(collection) }) } } },
|
||||
{ provide: NotificationsService, useValue: notificationsService }
|
||||
],
|
||||
schemas: [NO_ERRORS_SCHEMA]
|
||||
}).compileComponents();
|
||||
@@ -40,12 +55,46 @@ describe('CollectionMetadataComponent', () => {
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(CollectionMetadataComponent);
|
||||
comp = fixture.componentInstance;
|
||||
router = (comp as any).router;
|
||||
itemTemplateService = (comp as any).itemTemplateService;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
describe('frontendURL', () => {
|
||||
it('should have the right frontendURL set', () => {
|
||||
expect((comp as any).frontendURL).toEqual('/collections/');
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
describe('addItemTemplate', () => {
|
||||
it('should navigate to the collection\'s itemtemplate page', () => {
|
||||
spyOn(router, 'navigate');
|
||||
comp.addItemTemplate();
|
||||
expect(router.navigate).toHaveBeenCalledWith(['collections', collection.uuid, 'itemtemplate']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('deleteItemTemplate', () => {
|
||||
describe('when delete returns a success', () => {
|
||||
beforeEach(() => {
|
||||
spyOn(itemTemplateService, 'deleteByCollectionID').and.returnValue(observableOf(true));
|
||||
comp.deleteItemTemplate();
|
||||
});
|
||||
|
||||
it('should display a success notification', () => {
|
||||
expect(notificationsService.success).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('when delete returns a failure', () => {
|
||||
beforeEach(() => {
|
||||
spyOn(itemTemplateService, 'deleteByCollectionID').and.returnValue(observableOf(false));
|
||||
comp.deleteItemTemplate();
|
||||
});
|
||||
|
||||
it('should display an error notification', () => {
|
||||
expect(notificationsService.error).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@@ -0,0 +1,51 @@
|
||||
import { EditItemTemplatePageComponent } from './edit-item-template-page.component';
|
||||
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
import { TranslateModule } from '@ngx-translate/core';
|
||||
import { SharedModule } from '../../shared/shared.module';
|
||||
import { RouterTestingModule } from '@angular/router/testing';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { ItemTemplateDataService } from '../../core/data/item-template-data.service';
|
||||
import { ActivatedRoute } from '@angular/router';
|
||||
import { of as observableOf } from 'rxjs/internal/observable/of';
|
||||
import { createSuccessfulRemoteDataObject } from '../../shared/testing/utils';
|
||||
import { Collection } from '../../core/shared/collection.model';
|
||||
import { NO_ERRORS_SCHEMA } from '@angular/core';
|
||||
import { getCollectionEditPath } from '../collection-page-routing.module';
|
||||
|
||||
describe('EditItemTemplatePageComponent', () => {
|
||||
let comp: EditItemTemplatePageComponent;
|
||||
let fixture: ComponentFixture<EditItemTemplatePageComponent>;
|
||||
let itemTemplateService: ItemTemplateDataService;
|
||||
let collection: Collection;
|
||||
|
||||
beforeEach(async(() => {
|
||||
collection = Object.assign(new Collection(), {
|
||||
uuid: 'collection-id',
|
||||
id: 'collection-id',
|
||||
name: 'Fake Collection'
|
||||
});
|
||||
TestBed.configureTestingModule({
|
||||
imports: [TranslateModule.forRoot(), SharedModule, CommonModule, RouterTestingModule],
|
||||
declarations: [EditItemTemplatePageComponent],
|
||||
providers: [
|
||||
{ provide: ItemTemplateDataService, useValue: {} },
|
||||
{ provide: ActivatedRoute, useValue: { data: observableOf({ dso: createSuccessfulRemoteDataObject(collection) }) } }
|
||||
],
|
||||
schemas: [NO_ERRORS_SCHEMA]
|
||||
}).compileComponents();
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(EditItemTemplatePageComponent);
|
||||
comp = fixture.componentInstance;
|
||||
itemTemplateService = (comp as any).itemTemplateService;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
describe('getCollectionEditUrl', () => {
|
||||
it('should return the collection\'s edit url', () => {
|
||||
const url = comp.getCollectionEditUrl(collection);
|
||||
expect(url).toEqual(getCollectionEditPath(collection.uuid));
|
||||
});
|
||||
});
|
||||
});
|
@@ -34,7 +34,11 @@ export class EditItemTemplatePageComponent implements OnInit {
|
||||
* @param collection
|
||||
*/
|
||||
getCollectionEditUrl(collection: Collection): string {
|
||||
return getCollectionEditPath(collection.uuid);
|
||||
if (collection) {
|
||||
return getCollectionEditPath(collection.uuid);
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,28 @@
|
||||
import { of as observableOf } from 'rxjs/internal/observable/of';
|
||||
import { first } from 'rxjs/operators';
|
||||
import { ItemTemplatePageResolver } from './item-template-page.resolver';
|
||||
|
||||
describe('ItemTemplatePageResolver', () => {
|
||||
describe('resolve', () => {
|
||||
let resolver: ItemTemplatePageResolver;
|
||||
let itemTemplateService: any;
|
||||
const uuid = '1234-65487-12354-1235';
|
||||
|
||||
beforeEach(() => {
|
||||
itemTemplateService = {
|
||||
findByCollectionID: (id: string) => observableOf({ payload: { id }, hasSucceeded: true })
|
||||
};
|
||||
resolver = new ItemTemplatePageResolver(itemTemplateService);
|
||||
});
|
||||
|
||||
it('should resolve an item template with the correct id', () => {
|
||||
resolver.resolve({ params: { id: uuid } } as any, undefined)
|
||||
.pipe(first())
|
||||
.subscribe(
|
||||
(resolved) => {
|
||||
expect(resolved.payload.id).toEqual(uuid);
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
136
src/app/core/data/item-template-data.service.spec.ts
Normal file
136
src/app/core/data/item-template-data.service.spec.ts
Normal file
@@ -0,0 +1,136 @@
|
||||
import { ItemTemplateDataService } from './item-template-data.service';
|
||||
import { RestRequest } from './request.models';
|
||||
import { RequestEntry } from './request.reducer';
|
||||
import { RestResponse } from '../cache/response.models';
|
||||
import { RequestService } from './request.service';
|
||||
import { of as observableOf } from 'rxjs';
|
||||
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
|
||||
import { NormalizedObjectBuildService } from '../cache/builders/normalized-object-build.service';
|
||||
import { Store } from '@ngrx/store';
|
||||
import { CoreState } from '../core.reducers';
|
||||
import { ObjectCacheService } from '../cache/object-cache.service';
|
||||
import { BrowseService } from '../browse/browse.service';
|
||||
import { Observable } from 'rxjs/internal/Observable';
|
||||
import { cold } from 'jasmine-marbles';
|
||||
import { HALEndpointService } from '../shared/hal-endpoint.service';
|
||||
import { NotificationsService } from '../../shared/notifications/notifications.service';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { CollectionDataService } from './collection-data.service';
|
||||
import { RestRequestMethod } from './rest-request-method';
|
||||
import { Item } from '../shared/item.model';
|
||||
|
||||
describe('ItemTemplateDataService', () => {
|
||||
let service: ItemTemplateDataService;
|
||||
let itemService: any;
|
||||
|
||||
const item = new Item();
|
||||
const collectionEndpoint = 'https://rest.api/core/collections/4af28e99-6a9c-4036-a199-e1b587046d39';
|
||||
const itemEndpoint = `${collectionEndpoint}/itemtemplate`;
|
||||
const scopeID = '4af28e99-6a9c-4036-a199-e1b587046d39';
|
||||
const requestService = {
|
||||
generateRequestId(): string {
|
||||
return scopeID;
|
||||
},
|
||||
configure(request: RestRequest) {
|
||||
// Do nothing
|
||||
},
|
||||
getByHref(requestHref: string) {
|
||||
const responseCacheEntry = new RequestEntry();
|
||||
responseCacheEntry.response = new RestResponse(true, 200, 'OK');
|
||||
return observableOf(responseCacheEntry);
|
||||
},
|
||||
commit(method?: RestRequestMethod) {
|
||||
// Do nothing
|
||||
}
|
||||
} as RequestService;
|
||||
const rdbService = {} as RemoteDataBuildService;
|
||||
const dataBuildService = {} as NormalizedObjectBuildService;
|
||||
const store = {} as Store<CoreState>;
|
||||
const bs = {} as BrowseService;
|
||||
const objectCache = {
|
||||
getObjectBySelfLink(self) {
|
||||
return observableOf({})
|
||||
},
|
||||
addPatch(self, operations) {
|
||||
// Do nothing
|
||||
}
|
||||
} as ObjectCacheService;
|
||||
const halEndpointService = {
|
||||
getEndpoint(linkPath: string): Observable<string> {
|
||||
return cold('a', {a: itemEndpoint});
|
||||
}
|
||||
} as HALEndpointService;
|
||||
const notificationsService = {} as NotificationsService;
|
||||
const http = {} as HttpClient;
|
||||
const comparator = {
|
||||
diff(first, second) {
|
||||
return [{}];
|
||||
}
|
||||
} as any;
|
||||
const collectionService = {
|
||||
getIDHrefObs(id): Observable<string> {
|
||||
return observableOf(collectionEndpoint);
|
||||
}
|
||||
} as CollectionDataService;
|
||||
|
||||
function initTestService() {
|
||||
service = new ItemTemplateDataService(
|
||||
requestService,
|
||||
rdbService,
|
||||
dataBuildService,
|
||||
store,
|
||||
bs,
|
||||
objectCache,
|
||||
halEndpointService,
|
||||
notificationsService,
|
||||
http,
|
||||
comparator,
|
||||
collectionService
|
||||
);
|
||||
itemService = (service as any).dataService;
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
initTestService();
|
||||
});
|
||||
|
||||
describe('commitUpdates', () => {
|
||||
it('should call commitUpdates on the item service implementation', () => {
|
||||
spyOn(itemService, 'commitUpdates');
|
||||
service.commitUpdates();
|
||||
expect(itemService.commitUpdates).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('update', () => {
|
||||
it('should call update on the item service implementation', () => {
|
||||
spyOn(itemService, 'update');
|
||||
service.update(item);
|
||||
expect(itemService.update).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('findByCollectionID', () => {
|
||||
it('should call findByCollectionID on the item service implementation', () => {
|
||||
spyOn(itemService, 'findByCollectionID');
|
||||
service.findByCollectionID(scopeID);
|
||||
expect(itemService.findByCollectionID).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('create', () => {
|
||||
it('should call create on the item service implementation', () => {
|
||||
spyOn(itemService, 'create');
|
||||
service.create(item, scopeID);
|
||||
expect(itemService.create).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('deleteByCollectionID', () => {
|
||||
it('should call deleteByCollectionID on the item service implementation', () => {
|
||||
spyOn(itemService, 'deleteByCollectionID');
|
||||
service.deleteByCollectionID(item, scopeID);
|
||||
expect(itemService.deleteByCollectionID).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user