From 7fd4b41460df3c12cc3b0ae79cbb07bae3aaf5b4 Mon Sep 17 00:00:00 2001 From: Kristof De Langhe Date: Fri, 4 Oct 2019 17:50:53 +0200 Subject: [PATCH] 65272: Added test cases --- .../collection-metadata.component.spec.ts | 65 +++++++-- .../edit-item-template-page.component.spec.ts | 51 +++++++ .../edit-item-template-page.component.ts | 6 +- .../item-template-page.resolver.spec.ts | 28 ++++ .../data/item-template-data.service.spec.ts | 136 ++++++++++++++++++ 5 files changed, 277 insertions(+), 9 deletions(-) create mode 100644 src/app/+collection-page/edit-item-template-page/edit-item-template-page.component.spec.ts create mode 100644 src/app/+collection-page/edit-item-template-page/item-template-page.resolver.spec.ts create mode 100644 src/app/core/data/item-template-data.service.spec.ts diff --git a/src/app/+collection-page/edit-collection-page/collection-metadata/collection-metadata.component.spec.ts b/src/app/+collection-page/edit-collection-page/collection-metadata/collection-metadata.component.spec.ts index 0db0b51490..2bd932b7d2 100644 --- a/src/app/+collection-page/edit-collection-page/collection-metadata/collection-metadata.component.spec.ts +++ b/src/app/+collection-page/edit-collection-page/collection-metadata/collection-metadata.component.spec.ts @@ -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; + 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(); + }); + }); }); }); diff --git a/src/app/+collection-page/edit-item-template-page/edit-item-template-page.component.spec.ts b/src/app/+collection-page/edit-item-template-page/edit-item-template-page.component.spec.ts new file mode 100644 index 0000000000..90857712b8 --- /dev/null +++ b/src/app/+collection-page/edit-item-template-page/edit-item-template-page.component.spec.ts @@ -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; + 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)); + }); + }); +}); diff --git a/src/app/+collection-page/edit-item-template-page/edit-item-template-page.component.ts b/src/app/+collection-page/edit-item-template-page/edit-item-template-page.component.ts index 56e21ecb37..0170ab620b 100644 --- a/src/app/+collection-page/edit-item-template-page/edit-item-template-page.component.ts +++ b/src/app/+collection-page/edit-item-template-page/edit-item-template-page.component.ts @@ -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 ''; + } } } diff --git a/src/app/+collection-page/edit-item-template-page/item-template-page.resolver.spec.ts b/src/app/+collection-page/edit-item-template-page/item-template-page.resolver.spec.ts new file mode 100644 index 0000000000..885adadb3f --- /dev/null +++ b/src/app/+collection-page/edit-item-template-page/item-template-page.resolver.spec.ts @@ -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); + } + ); + }); + }); +}); diff --git a/src/app/core/data/item-template-data.service.spec.ts b/src/app/core/data/item-template-data.service.spec.ts new file mode 100644 index 0000000000..38d72a6265 --- /dev/null +++ b/src/app/core/data/item-template-data.service.spec.ts @@ -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; + const bs = {} as BrowseService; + const objectCache = { + getObjectBySelfLink(self) { + return observableOf({}) + }, + addPatch(self, operations) { + // Do nothing + } + } as ObjectCacheService; + const halEndpointService = { + getEndpoint(linkPath: string): Observable { + 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 { + 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(); + }); + }); +});