From a2fb8a316b3c47b85a995db892839c54907738c0 Mon Sep 17 00:00:00 2001 From: Kristof De Langhe <kristof.delanghe@atmire.com> Date: Mon, 30 Sep 2019 10:35:52 +0200 Subject: [PATCH] 62589: Added tests for more coverage --- .../collection-item-mapper.component.spec.ts | 43 ++++++++++++++- .../item-collection-mapper.component.spec.ts | 45 +++++++++++++++- .../core/data/collection-data.service.spec.ts | 54 +++++++++++++++++++ src/app/core/data/item-data.service.spec.ts | 49 +++++++++++++++-- .../collection-select.component.spec.ts | 14 +++++ .../item-select/item-select.component.spec.ts | 14 +++++ 6 files changed, 213 insertions(+), 6 deletions(-) create mode 100644 src/app/core/data/collection-data.service.spec.ts diff --git a/src/app/+collection-page/collection-item-mapper/collection-item-mapper.component.spec.ts b/src/app/+collection-page/collection-item-mapper/collection-item-mapper.component.spec.ts index dcbce697c6..8332d20cea 100644 --- a/src/app/+collection-page/collection-item-mapper/collection-item-mapper.component.spec.ts +++ b/src/app/+collection-page/collection-item-mapper/collection-item-mapper.component.spec.ts @@ -67,8 +67,12 @@ describe('CollectionItemMapperComponent', () => { sort: new SortOptions('dc.title', SortDirection.ASC), scope: mockCollection.id })); + const url = 'http://test.url'; + const urlWithParam = url + '?param=value'; const routerStub = Object.assign(new RouterStub(), { - url: 'http://test.url' + url: urlWithParam, + navigateByUrl: {}, + navigate: {} }); const searchConfigServiceStub = { paginatedSearchOptions: mockSearchOptions @@ -168,4 +172,41 @@ describe('CollectionItemMapperComponent', () => { }); }); + describe('tabChange', () => { + beforeEach(() => { + spyOn(routerStub, 'navigateByUrl'); + comp.tabChange({}); + }); + + it('should navigate to the same page to remove parameters', () => { + expect(router.navigateByUrl).toHaveBeenCalledWith(url); + }); + }); + + describe('buildQuery', () => { + const query = 'query'; + const expected = `-location.coll:\"${mockCollection.id}\" AND ${query}`; + + let result; + + beforeEach(() => { + result = comp.buildQuery(mockCollection.id, query); + }); + + it('should build a solr query to exclude the provided collection', () => { + expect(result).toEqual(expected); + }) + }); + + describe('onCancel', () => { + beforeEach(() => { + spyOn(routerStub, 'navigate'); + comp.onCancel(); + }); + + it('should navigate to the collection page', () => { + expect(router.navigate).toHaveBeenCalledWith(['/collections/', mockCollection.id]); + }); + }); + }); diff --git a/src/app/+item-page/edit-item-page/item-collection-mapper/item-collection-mapper.component.spec.ts b/src/app/+item-page/edit-item-page/item-collection-mapper/item-collection-mapper.component.spec.ts index 2f04126711..018ed3f2ac 100644 --- a/src/app/+item-page/edit-item-page/item-collection-mapper/item-collection-mapper.component.spec.ts +++ b/src/app/+item-page/edit-item-page/item-collection-mapper/item-collection-mapper.component.spec.ts @@ -36,6 +36,7 @@ import { PaginationComponent } from '../../../shared/pagination/pagination.compo import { EnumKeysPipe } from '../../../shared/utils/enum-keys-pipe'; import { VarDirective } from '../../../shared/utils/var.directive'; import { SearchFormComponent } from '../../../shared/search-form/search-form.component'; +import { Collection } from '../../../core/shared/collection.model'; describe('ItemCollectionMapperComponent', () => { let comp: ItemCollectionMapperComponent; @@ -48,6 +49,7 @@ describe('ItemCollectionMapperComponent', () => { let notificationsService: NotificationsService; let itemDataService: ItemDataService; + const mockCollection = Object.assign(new Collection(), { id: 'collection1' }); const mockItem: Item = Object.assign(new Item(), { id: '932c7d50-d85a-44cb-b9dc-b427b12877bd', name: 'test-item' @@ -61,8 +63,12 @@ describe('ItemCollectionMapperComponent', () => { }), sort: new SortOptions('dc.title', SortDirection.ASC) })); + const url = 'http://test.url'; + const urlWithParam = url + '?param=value'; const routerStub = Object.assign(new RouterStub(), { - url: 'http://test.url' + url: urlWithParam, + navigateByUrl: {}, + navigate: {} }); const searchConfigServiceStub = { paginatedSearchOptions: mockSearchOptions @@ -159,4 +165,41 @@ describe('ItemCollectionMapperComponent', () => { }); }); + describe('tabChange', () => { + beforeEach(() => { + spyOn(routerStub, 'navigateByUrl'); + comp.tabChange({}); + }); + + it('should navigate to the same page to remove parameters', () => { + expect(router.navigateByUrl).toHaveBeenCalledWith(url); + }); + }); + + describe('buildQuery', () => { + const query = 'query'; + const expected = `${query} AND -search.resourceid:${mockCollection.id}`; + + let result; + + beforeEach(() => { + result = comp.buildQuery([mockCollection], query); + }); + + it('should build a solr query to exclude the provided collection', () => { + expect(result).toEqual(expected); + }) + }); + + describe('onCancel', () => { + beforeEach(() => { + spyOn(routerStub, 'navigate'); + comp.onCancel(); + }); + + it('should navigate to the item page', () => { + expect(router.navigate).toHaveBeenCalledWith(['/items/', mockItem.id]); + }); + }); + }); diff --git a/src/app/core/data/collection-data.service.spec.ts b/src/app/core/data/collection-data.service.spec.ts new file mode 100644 index 0000000000..b0b3889c9c --- /dev/null +++ b/src/app/core/data/collection-data.service.spec.ts @@ -0,0 +1,54 @@ +import { CollectionDataService } from './collection-data.service'; +import { HALEndpointServiceStub } from '../../shared/testing/hal-endpoint-service-stub'; +import { getMockRequestService } from '../../shared/mocks/mock-request.service'; +import { HALEndpointService } from '../shared/hal-endpoint.service'; +import { RequestService } from './request.service'; +import { ObjectCacheService } from '../cache/object-cache.service'; +import { GetRequest } from './request.models'; +import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service'; + +describe('CollectionDataService', () => { + let service: CollectionDataService; + let objectCache: ObjectCacheService; + let requestService: RequestService; + let halService: HALEndpointService; + let rdbService: RemoteDataBuildService; + + const url = 'fake-collections-url'; + + beforeEach(() => { + objectCache = jasmine.createSpyObj('objectCache', { + remove: jasmine.createSpy('remove') + }); + requestService = getMockRequestService(); + halService = Object.assign(new HALEndpointServiceStub(url)); + rdbService = jasmine.createSpyObj('rdbService', { + buildList: jasmine.createSpy('buildList') + }); + + service = new CollectionDataService(requestService, rdbService, null, null, null, objectCache, halService, null, null, null); + }); + + describe('getMappedItems', () => { + let result; + + beforeEach(() => { + result = service.getMappedItems('collection-id'); + }); + + it('should configure a GET request', () => { + expect(requestService.configure).toHaveBeenCalledWith(jasmine.any(GetRequest), undefined); + }); + }); + + describe('clearMappedItemsRequests', () => { + beforeEach(() => { + service.clearMappedItemsRequests('collection-id'); + }); + + it('should remote request cache', () => { + expect(requestService.removeByHrefSubstring).toHaveBeenCalled(); + }); + }); + +}); diff --git a/src/app/core/data/item-data.service.spec.ts b/src/app/core/data/item-data.service.spec.ts index 3553a63af4..36b8e6b3c5 100644 --- a/src/app/core/data/item-data.service.spec.ts +++ b/src/app/core/data/item-data.service.spec.ts @@ -7,7 +7,14 @@ import { CoreState } from '../core.reducers'; import { ItemDataService } from './item-data.service'; import { RequestService } from './request.service'; import { HALEndpointService } from '../shared/hal-endpoint.service'; -import { FindAllOptions, RestRequest } from './request.models'; +import { + DeleteRequest, + FindAllOptions, + GetRequest, + MappedCollectionsRequest, + PostRequest, + RestRequest +} from './request.models'; import { ObjectCacheService } from '../cache/object-cache.service'; import { Observable } from 'rxjs'; import { RestResponse } from '../cache/response.models'; @@ -16,12 +23,13 @@ import { NormalizedObjectBuildService } from '../cache/builders/normalized-objec import { HttpClient } from '@angular/common/http'; import { RequestEntry } from './request.reducer'; import { of as observableOf } from 'rxjs'; +import { getMockRequestService } from '../../shared/mocks/mock-request.service'; describe('ItemDataService', () => { let scheduler: TestScheduler; let service: ItemDataService; let bs: BrowseService; - const requestService = { + const requestService = Object.assign(getMockRequestService(), { generateRequestId(): string { return scopeID; }, @@ -32,9 +40,14 @@ describe('ItemDataService', () => { const responseCacheEntry = new RequestEntry(); responseCacheEntry.response = new RestResponse(true, 200, 'OK'); return observableOf(responseCacheEntry); + }, + removeByHrefSubstring(href: string) { + // Do nothing } - } as RequestService; - const rdbService = {} as RemoteDataBuildService; + }) as RequestService; + const rdbService = jasmine.createSpyObj('rdbService', { + toRemoteDataObservable: observableOf({}) + }); const store = {} as Store<CoreState>; const objectCache = {} as ObjectCacheService; @@ -162,4 +175,32 @@ describe('ItemDataService', () => { }); }); + describe('removeMappingFromCollection', () => { + let result; + + beforeEach(() => { + service = initTestService(); + spyOn(requestService, 'configure'); + result = service.removeMappingFromCollection('item-id', 'collection-id'); + }); + + it('should configure a DELETE request', () => { + result.subscribe(() => expect(requestService.configure).toHaveBeenCalledWith(jasmine.any(DeleteRequest), undefined)); + }); + }); + + describe('mapToCollection', () => { + let result; + + beforeEach(() => { + service = initTestService(); + spyOn(requestService, 'configure'); + result = service.mapToCollection('item-id', 'collection-href'); + }); + + it('should configure a POST request', () => { + result.subscribe(() => expect(requestService.configure).toHaveBeenCalledWith(jasmine.any(PostRequest), undefined)); + }); + }); + }); diff --git a/src/app/shared/object-select/collection-select/collection-select.component.spec.ts b/src/app/shared/object-select/collection-select/collection-select.component.spec.ts index bc83c3d52a..c9f79f6af5 100644 --- a/src/app/shared/object-select/collection-select/collection-select.component.spec.ts +++ b/src/app/shared/object-select/collection-select/collection-select.component.spec.ts @@ -101,4 +101,18 @@ describe('ItemSelectComponent', () => { expect(comp.confirm.emit).toHaveBeenCalled(); }); }); + + describe('when cancel is clicked', () => { + let cancelButton: HTMLButtonElement; + + beforeEach(() => { + cancelButton = fixture.debugElement.query(By.css('button.collection-cancel')).nativeElement; + spyOn(comp.cancel, 'emit').and.callThrough(); + }); + + it('should emit a cancel event',() => { + cancelButton.click(); + expect(comp.cancel.emit).toHaveBeenCalled(); + }); + }); }); diff --git a/src/app/shared/object-select/item-select/item-select.component.spec.ts b/src/app/shared/object-select/item-select/item-select.component.spec.ts index be7c315c45..33fa4dcd7e 100644 --- a/src/app/shared/object-select/item-select/item-select.component.spec.ts +++ b/src/app/shared/object-select/item-select/item-select.component.spec.ts @@ -123,4 +123,18 @@ describe('ItemSelectComponent', () => { expect(comp.confirm.emit).toHaveBeenCalled(); }); }); + + describe('when cancel is clicked', () => { + let cancelButton: HTMLButtonElement; + + beforeEach(() => { + cancelButton = fixture.debugElement.query(By.css('button.item-cancel')).nativeElement; + spyOn(comp.cancel, 'emit').and.callThrough(); + }); + + it('should emit a cancel event',() => { + cancelButton.click(); + expect(comp.cancel.emit).toHaveBeenCalled(); + }); + }); }); -- GitLab