From 48c2390515e170e9aa91b468cf724e610a09e7b9 Mon Sep 17 00:00:00 2001 From: Kristof De Langhe Date: Wed, 4 Apr 2018 17:17:57 +0200 Subject: [PATCH] 50632: Added tests for registry service and updated component tests --- .../bitstream-formats.component.spec.ts | 15 +- .../metadata-registry.component.spec.ts | 21 +- .../metadata-schema.component.spec.ts | 27 ++- .../core/registry/registry.service.spec.ts | 200 ++++++++++++++++++ src/tsconfig.test.json | 2 +- 5 files changed, 247 insertions(+), 18 deletions(-) create mode 100644 src/app/core/registry/registry.service.spec.ts diff --git a/src/app/+admin/admin-registries/bitstream-formats/bitstream-formats.component.spec.ts b/src/app/+admin/admin-registries/bitstream-formats/bitstream-formats.component.spec.ts index f83f96127f..18a28c16fb 100644 --- a/src/app/+admin/admin-registries/bitstream-formats/bitstream-formats.component.spec.ts +++ b/src/app/+admin/admin-registries/bitstream-formats/bitstream-formats.component.spec.ts @@ -8,8 +8,14 @@ import { CommonModule } from '@angular/common'; import { RouterTestingModule } from '@angular/router/testing'; import { TranslateModule } from '@ngx-translate/core'; import { By } from '@angular/platform-browser'; +import { SharedModule } from '../../../shared/shared.module'; +import { PaginationComponent } from '../../../shared/pagination/pagination.component'; +import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; +import { EnumKeysPipe } from '../../../shared/utils/enum-keys-pipe'; +import { HostWindowService } from '../../../shared/host-window.service'; +import { HostWindowServiceStub } from '../../../shared/testing/host-window-service-stub'; -describe('BitstreamFormatsComponent', () => { +fdescribe('BitstreamFormatsComponent', () => { let comp: BitstreamFormatsComponent; let fixture: ComponentFixture; let registryService: RegistryService; @@ -54,10 +60,11 @@ describe('BitstreamFormatsComponent', () => { beforeEach(async(() => { TestBed.configureTestingModule({ - imports: [CommonModule, RouterTestingModule.withRoutes([]), TranslateModule.forRoot()], - declarations: [BitstreamFormatsComponent], + imports: [CommonModule, RouterTestingModule.withRoutes([]), TranslateModule.forRoot(), NgbModule.forRoot()], + declarations: [BitstreamFormatsComponent, PaginationComponent, EnumKeysPipe], providers: [ - { provide: RegistryService, useValue: registryServiceStub } + { provide: RegistryService, useValue: registryServiceStub }, + { provide: HostWindowService, useValue: new HostWindowServiceStub(0) } ] }).compileComponents(); })); diff --git a/src/app/+admin/admin-registries/metadata-registry/metadata-registry.component.spec.ts b/src/app/+admin/admin-registries/metadata-registry/metadata-registry.component.spec.ts index 154a96dc81..f05110b45a 100644 --- a/src/app/+admin/admin-registries/metadata-registry/metadata-registry.component.spec.ts +++ b/src/app/+admin/admin-registries/metadata-registry/metadata-registry.component.spec.ts @@ -8,18 +8,26 @@ import { By } from '@angular/platform-browser'; import { CommonModule } from '@angular/common'; import { RouterTestingModule } from '@angular/router/testing'; import { RegistryService } from '../../../core/registry/registry.service'; +import { SharedModule } from '../../../shared/shared.module'; +import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; +import { EnumKeysPipe } from '../../../shared/utils/enum-keys-pipe'; +import { PaginationComponent } from '../../../shared/pagination/pagination.component'; +import { HostWindowServiceStub } from '../../../shared/testing/host-window-service-stub'; +import { HostWindowService } from '../../../shared/host-window.service'; -describe('MetadataRegistryComponent', () => { +fdescribe('MetadataRegistryComponent', () => { let comp: MetadataRegistryComponent; let fixture: ComponentFixture; let registryService: RegistryService; const mockSchemasList = [ { + id: 1, self: 'https://dspace7.4science.it/dspace-spring-rest/api/core/metadataschemas/1', prefix: 'dc', namespace: 'http://dublincore.org/documents/dcmi-terms/' }, { + id: 2, self: 'https://dspace7.4science.it/dspace-spring-rest/api/core/metadataschemas/2', prefix: 'mock', namespace: 'http://dspace.org/mockschema' @@ -32,10 +40,11 @@ describe('MetadataRegistryComponent', () => { beforeEach(async(() => { TestBed.configureTestingModule({ - imports: [CommonModule, RouterTestingModule.withRoutes([]), TranslateModule.forRoot()], - declarations: [MetadataRegistryComponent], + imports: [CommonModule, RouterTestingModule.withRoutes([]), TranslateModule.forRoot(), NgbModule.forRoot()], + declarations: [MetadataRegistryComponent, PaginationComponent, EnumKeysPipe], providers: [ - { provide: RegistryService, useValue: registryServiceStub } + { provide: RegistryService, useValue: registryServiceStub }, + { provide: HostWindowService, useValue: new HostWindowServiceStub(0) } ] }).compileComponents(); })); @@ -53,10 +62,10 @@ describe('MetadataRegistryComponent', () => { }); it('should contain the correct schemas', () => { - const dcName: HTMLElement = fixture.debugElement.query(By.css('#metadata-schemas tr:nth-child(1) td:nth-child(2)')).nativeElement; + const dcName: HTMLElement = fixture.debugElement.query(By.css('#metadata-schemas tr:nth-child(1) td:nth-child(3)')).nativeElement; expect(dcName.textContent).toBe('dc'); - const mockName: HTMLElement = fixture.debugElement.query(By.css('#metadata-schemas tr:nth-child(2) td:nth-child(2)')).nativeElement; + const mockName: HTMLElement = fixture.debugElement.query(By.css('#metadata-schemas tr:nth-child(2) td:nth-child(3)')).nativeElement; expect(mockName.textContent).toBe('mock'); }); diff --git a/src/app/+admin/admin-registries/metadata-schema/metadata-schema.component.spec.ts b/src/app/+admin/admin-registries/metadata-schema/metadata-schema.component.spec.ts index e639e2bf2a..bd606bf1fe 100644 --- a/src/app/+admin/admin-registries/metadata-schema/metadata-schema.component.spec.ts +++ b/src/app/+admin/admin-registries/metadata-schema/metadata-schema.component.spec.ts @@ -6,22 +6,33 @@ import { PaginatedList } from '../../../core/data/paginated-list'; import { MetadataSchema } from '../../../core/metadata/metadataschema.model'; import { TranslateLoader, TranslateModule } from '@ngx-translate/core'; import { CommonModule } from '@angular/common'; -import { ActivatedRoute } from '@angular/router'; +import { ActivatedRoute, Router } from '@angular/router'; import { By } from '@angular/platform-browser'; import { MockTranslateLoader } from '../../../shared/testing/mock-translate-loader'; import { RegistryService } from '../../../core/registry/registry.service'; +import { SharedModule } from '../../../shared/shared.module'; +import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; +import { EnumKeysPipe } from '../../../shared/utils/enum-keys-pipe'; +import { PaginationComponent } from '../../../shared/pagination/pagination.component'; +import { HostWindowServiceStub } from '../../../shared/testing/host-window-service-stub'; +import { HostWindowService } from '../../../shared/host-window.service'; +import { RouterStub } from '../../../shared/testing/router-stub'; +import { RouterTestingModule } from '@angular/router/testing'; +import { ActivatedRouteStub } from '../../../shared/testing/active-router-stub'; -describe('MetadataSchemaComponent', () => { +fdescribe('MetadataSchemaComponent', () => { let comp: MetadataSchemaComponent; let fixture: ComponentFixture; let registryService: RegistryService; const mockSchemasList = [ { + id: 1, self: 'https://dspace7.4science.it/dspace-spring-rest/api/core/metadataschemas/1', prefix: 'dc', namespace: 'http://dublincore.org/documents/dcmi-terms/' }, { + id: 2, self: 'https://dspace7.4science.it/dspace-spring-rest/api/core/metadataschemas/2', prefix: 'mock', namespace: 'http://dspace.org/mockschema' @@ -64,19 +75,21 @@ describe('MetadataSchemaComponent', () => { getMetadataSchemaByName: (schemaName: string) => Observable.of(new RemoteData(false, false, true, undefined, mockSchemasList.filter((value) => value.prefix === schemaName)[0])) }; const schemaNameParam = 'mock'; - const activatedRouteStub = { + const activatedRouteStub = Object.assign(new ActivatedRouteStub(), { params: Observable.of({ schemaName: schemaNameParam }) - }; + }); beforeEach(async(() => { TestBed.configureTestingModule({ - imports: [CommonModule, TranslateModule.forRoot()], - declarations: [MetadataSchemaComponent], + imports: [CommonModule, RouterTestingModule.withRoutes([]), TranslateModule.forRoot(), NgbModule.forRoot()], + declarations: [MetadataSchemaComponent, PaginationComponent, EnumKeysPipe], providers: [ { provide: RegistryService, useValue: registryServiceStub }, - { provide: ActivatedRoute, useValue: activatedRouteStub } + { provide: ActivatedRoute, useValue: activatedRouteStub }, + { provide: HostWindowService, useValue: new HostWindowServiceStub(0) }, + { provide: Router, useValue: new RouterStub() } ] }).compileComponents(); })); diff --git a/src/app/core/registry/registry.service.spec.ts b/src/app/core/registry/registry.service.spec.ts new file mode 100644 index 0000000000..0255576137 --- /dev/null +++ b/src/app/core/registry/registry.service.spec.ts @@ -0,0 +1,200 @@ +import { async, TestBed } from '@angular/core/testing'; +import { RegistryService } from './registry.service'; +import { CommonModule } from '@angular/common'; +import { ResponseCacheService } from '../cache/response-cache.service'; +import { RequestService } from '../data/request.service'; +import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service'; +import { HALEndpointService } from '../shared/hal-endpoint.service'; +import { PaginationComponentOptions } from '../../shared/pagination/pagination-component-options.model'; +import { Observable } from 'rxjs/Observable'; +import { ResponseCacheEntry } from '../cache/response-cache.reducer'; +import { RequestEntry } from '../data/request.reducer'; +import { RemoteData } from '../data/remote-data'; +import { PaginatedList } from '../data/paginated-list'; +import { PageInfo } from '../shared/page-info.model'; + +fdescribe('RegistryService', () => { + let registryService: RegistryService; + const pagination: PaginationComponentOptions = Object.assign(new PaginationComponentOptions(), { + id: 'registry-service-spec-pagination', + pageSize: 20 + }); + + const mockSchemasList = [ + { + id: 1, + self: 'https://dspace7.4science.it/dspace-spring-rest/api/core/metadataschemas/1', + prefix: 'dc', + namespace: 'http://dublincore.org/documents/dcmi-terms/' + }, + { + id: 2, + self: 'https://dspace7.4science.it/dspace-spring-rest/api/core/metadataschemas/2', + prefix: 'mock', + namespace: 'http://dspace.org/mockschema' + } + ]; + const mockFieldsList = [ + { + self: 'https://dspace7.4science.it/dspace-spring-rest/api/core/metadatafields/8', + element: 'contributor', + qualifier: 'advisor', + scopenote: null, + schema: mockSchemasList[0] + }, + { + self: 'https://dspace7.4science.it/dspace-spring-rest/api/core/metadatafields/9', + element: 'contributor', + qualifier: 'author', + scopenote: null, + schema: mockSchemasList[0] + }, + { + self: 'https://dspace7.4science.it/dspace-spring-rest/api/core/metadatafields/10', + element: 'contributor', + qualifier: 'editor', + scopenote: 'test scope note', + schema: mockSchemasList[1] + }, + { + self: 'https://dspace7.4science.it/dspace-spring-rest/api/core/metadatafields/11', + element: 'contributor', + qualifier: 'illustrator', + scopenote: null, + schema: mockSchemasList[1] + } + ]; + const mockFormatsList = [ + { + shortDescription: 'Unknown', + description: 'Unknown data format', + mimetype: 'application/octet-stream', + supportLevel: 0, + internal: false, + extensions: null + }, + { + shortDescription: 'License', + description: 'Item-specific license agreed upon to submission', + mimetype: 'text/plain; charset=utf-8', + supportLevel: 1, + internal: true, + extensions: null + }, + { + shortDescription: 'CC License', + description: 'Item-specific Creative Commons license agreed upon to submission', + mimetype: 'text/html; charset=utf-8', + supportLevel: 2, + internal: true, + extensions: null + }, + { + shortDescription: 'Adobe PDF', + description: 'Adobe Portable Document Format', + mimetype: 'application/pdf', + supportLevel: 0, + internal: false, + extensions: null + } + ]; + + const pageInfo = new PageInfo(); + pageInfo.elementsPerPage = 10; + pageInfo.currentPage = 1; + + const halServiceStub = { + getEndpoint: () => Observable.of('path') + }; + + const rdbMetadataschemasStub = { + toRemoteDataObservable: () => { + const payload = new PaginatedList(pageInfo, mockSchemasList); + const remoteData = new RemoteData(false, false, true, undefined, payload); + return Observable.of(remoteData); + } + }; + const rdbMetadatafieldsStub = { + toRemoteDataObservable: () => { + const payload = new PaginatedList(pageInfo, mockFieldsList); + const remoteData = new RemoteData(false, false, true, undefined, payload); + return Observable.of(remoteData); + } + }; + const rdbBitstreamformatsStub = { + toRemoteDataObservable: () => { + const payload = new PaginatedList(pageInfo, mockFormatsList); + const remoteData = new RemoteData(false, false, true, undefined, payload); + return Observable.of(remoteData); + } + }; + + describe('when requesting metadataschemas', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [ CommonModule ], + providers: [ + { provide: ResponseCacheService, useValue: {} }, + { provide: RequestService, useValue: {} }, + { provide: RemoteDataBuildService, useValue: rdbMetadataschemasStub }, + { provide: HALEndpointService, useValue: halServiceStub }, + RegistryService + ] + }); + + registryService = TestBed.get(RegistryService); + }); + + it('should recieve the correct data', () => { + registryService.getMetadataSchemas(pagination).subscribe((value) => { + expect(value.payload.page[0].prefix).toEqual('dc'); + }); + }); + }); + + describe('when requesting metadatafields', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [ CommonModule ], + providers: [ + { provide: ResponseCacheService, useValue: {} }, + { provide: RequestService, useValue: {} }, + { provide: RemoteDataBuildService, useValue: rdbMetadatafieldsStub }, + { provide: HALEndpointService, useValue: halServiceStub }, + RegistryService + ] + }); + + registryService = TestBed.get(RegistryService); + }); + + it('should recieve the correct data', () => { + registryService.getMetadataFieldsBySchema(mockSchemasList[0], pagination).subscribe((value) => { + expect(value.payload.page[0].element).toEqual('contributor'); + }); + }); + }); + + describe('when requesting bitstreamformats', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [ CommonModule ], + providers: [ + { provide: ResponseCacheService, useValue: {} }, + { provide: RequestService, useValue: {} }, + { provide: RemoteDataBuildService, useValue: rdbBitstreamformatsStub }, + { provide: HALEndpointService, useValue: halServiceStub }, + RegistryService + ] + }); + + registryService = TestBed.get(RegistryService); + }); + + it('should recieve the correct data', () => { + registryService.getBitstreamFormats(pagination).subscribe((value) => { + expect(value.payload.page[0].shortDescription).toEqual('Unknown'); + }); + }); + }); +}); diff --git a/src/tsconfig.test.json b/src/tsconfig.test.json index 079eabc561..712ad1ab1c 100644 --- a/src/tsconfig.test.json +++ b/src/tsconfig.test.json @@ -1,4 +1,4 @@ -yarn add{ +{ "extends": "../tsconfig.json", "compilerOptions": { "sourceMap": true