1- import { CommonModule } from '@angular/common' ;
2- import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core' ;
3- import { TestBed , waitForAsync } from '@angular/core/testing' ;
4- import { Store , StoreModule } from '@ngrx/store' ;
5- import { compare , Operation } from 'fast-json-patch' ;
6- import { getTestScheduler } from 'jasmine-marbles' ;
7- import { Observable , of as observableOf } from 'rxjs' ;
8- import { TranslateLoader , TranslateModule } from '@ngx-translate/core' ;
9- import { TestScheduler } from 'rxjs/testing' ;
1+ import { fakeAsync , TestBed , tick , waitForAsync } from '@angular/core/testing' ;
2+ import { Store } from '@ngrx/store' ;
3+ import { cold } from 'jasmine-marbles' ;
4+ import { of as observableOf } from 'rxjs' ;
105import {
116 EPeopleRegistryCancelEPersonAction ,
127 EPeopleRegistryEditEPersonAction
138} from '../../access-control/epeople-registry/epeople-registry.actions' ;
149import { RequestParam } from '../cache/models/request-param.model' ;
15- import { ChangeAnalyzer } from '../data/change-analyzer' ;
1610import { PatchRequest , PostRequest } from '../data/request.models' ;
1711import { RequestService } from '../data/request.service' ;
1812import { HALEndpointService } from '../shared/hal-endpoint.service' ;
19- import { Item } from '../shared/item.model' ;
20- import { EPersonDataService } from './eperson-data.service' ;
13+ import { editEPersonSelector , EPersonDataService } from './eperson-data.service' ;
2114import { EPerson } from './models/eperson.model' ;
2215import { EPersonMock , EPersonMock2 } from '../../shared/testing/eperson.mock' ;
2316import { HALEndpointServiceStub } from '../../shared/testing/hal-endpoint-service.stub' ;
2417import { createNoContentRemoteDataObject$ , createSuccessfulRemoteDataObject$ } from '../../shared/remote-data.utils' ;
2518import { getMockRemoteDataBuildServiceHrefMap } from '../../shared/mocks/remote-data-build.service.mock' ;
26- import { TranslateLoaderMock } from '../../shared/mocks/translate-loader.mock' ;
2719import { getMockRequestService } from '../../shared/mocks/request.service.mock' ;
2820import { createPaginatedList , createRequestEntry$ } from '../../shared/testing/utils.test' ;
2921import { CoreState } from '../core-state.model' ;
3022import { FindListOptions } from '../data/find-list-options.model' ;
23+ import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service' ;
24+ import { ObjectCacheService } from '../cache/object-cache.service' ;
25+ import { DSOChangeAnalyzer } from '../data/dso-change-analyzer.service' ;
26+ import { NotificationsService } from '../../shared/notifications/notifications.service' ;
27+ import { NotificationsServiceStub } from '../../shared/testing/notifications-service.stub' ;
28+ import { MockStore , provideMockStore } from '@ngrx/store/testing' ;
29+ import { compare , Operation } from 'fast-json-patch' ;
30+ import { Item } from '../shared/item.model' ;
31+ import { ChangeAnalyzer } from '../data/change-analyzer' ;
3132
3233describe ( 'EPersonDataService' , ( ) => {
3334 let service : EPersonDataService ;
34- let store : Store < CoreState > ;
35+ let store : MockStore < CoreState > ;
3536 let requestService : RequestService ;
36- let scheduler : TestScheduler ;
3737
3838 let epeople ;
3939
@@ -43,50 +43,38 @@ describe('EPersonDataService', () => {
4343 let epeople$ ;
4444 let rdbService ;
4545
46- function initTestService ( ) {
47- return new EPersonDataService (
48- requestService ,
49- rdbService ,
50- null ,
51- halService ,
52- new DummyChangeAnalyzer ( ) as any ,
53- null ,
54- store ,
55- ) ;
56- }
46+ const initialState = {
47+ epeopleRegistry : {
48+ editEPerson : null
49+ } ,
50+ } ;
5751
58- function init ( ) {
52+ beforeEach ( waitForAsync ( ( ) => {
5953 restEndpointURL = 'https://rest.api/dspace-spring-rest/api/eperson' ;
6054 epersonsEndpoint = `${ restEndpointURL } /epersons` ;
6155 epeople = [ EPersonMock , EPersonMock2 ] ;
6256 epeople$ = createSuccessfulRemoteDataObject$ ( createPaginatedList ( [ epeople ] ) ) ;
6357 rdbService = getMockRemoteDataBuildServiceHrefMap ( undefined , { 'https://rest.api/dspace-spring-rest/api/eperson/epersons' : epeople$ } ) ;
6458 halService = new HALEndpointServiceStub ( restEndpointURL ) ;
59+ requestService = getMockRequestService ( createRequestEntry$ ( epeople ) ) ;
6560
6661 TestBed . configureTestingModule ( {
67- imports : [
68- CommonModule ,
69- StoreModule . forRoot ( { } ) ,
70- TranslateModule . forRoot ( {
71- loader : {
72- provide : TranslateLoader ,
73- useClass : TranslateLoaderMock
74- }
75- } ) ,
62+ providers : [
63+ EPersonDataService ,
64+ { provide : RequestService , useValue : requestService } ,
65+ { provide : RemoteDataBuildService , useValue : rdbService } ,
66+ { provide : HALEndpointService , useValue : halService } ,
67+ provideMockStore ( { initialState } ) ,
68+ { provide : ObjectCacheService , useValue : { } } ,
69+ { provide : DSOChangeAnalyzer , useClass : DummyChangeAnalyzer } ,
70+ { provide : NotificationsService , useClass : NotificationsServiceStub } ,
7671 ] ,
77- declarations : [ ] ,
78- providers : [ ] ,
79- schemas : [ CUSTOM_ELEMENTS_SCHEMA ]
8072 } ) ;
81- }
8273
83- beforeEach ( ( ) => {
84- init ( ) ;
85- requestService = getMockRequestService ( createRequestEntry$ ( epeople ) ) ;
86- store = new Store < CoreState > ( undefined , undefined , undefined ) ;
87- service = initTestService ( ) ;
88- spyOn ( store , 'dispatch' ) ;
89- } ) ;
74+ service = TestBed . inject ( EPersonDataService ) ;
75+ store = TestBed . inject ( Store ) as MockStore < CoreState > ;
76+ spyOn ( store , 'dispatch' ) . and . callThrough ( ) ;
77+ } ) ) ;
9078
9179 describe ( 'searchByScope' , ( ) => {
9280 beforeEach ( ( ) => {
@@ -239,34 +227,29 @@ describe('EPersonDataService', () => {
239227 } ) ;
240228
241229 describe ( 'clearEPersonRequests' , ( ) => {
242- beforeEach ( waitForAsync ( ( ) => {
243- scheduler = getTestScheduler ( ) ;
244- halService = {
245- getEndpoint ( linkPath : string ) : Observable < string > {
246- return observableOf ( restEndpointURL + '/' + linkPath ) ;
247- }
248- } as HALEndpointService ;
249- initTestService ( ) ;
230+ beforeEach ( ( ) => {
231+ spyOn ( halService , 'getEndpoint' ) . and . callFake ( ( linkPath : string ) => {
232+ return observableOf ( `${ restEndpointURL } /${ linkPath } ` ) ;
233+ } ) ;
234+ } ) ;
235+ it ( 'should remove the eperson hrefs in the request service' , fakeAsync ( ( ) => {
250236 service . clearEPersonRequests ( ) ;
251- } ) ) ;
252- it ( 'should remove the eperson hrefs in the request service' , ( ) => {
237+ tick ( ) ;
238+
253239 expect ( requestService . removeByHrefSubstring ) . toHaveBeenCalledWith ( epersonsEndpoint ) ;
254- } ) ;
240+ } ) ) ;
255241 } ) ;
256242
257243 describe ( 'getActiveEPerson' , ( ) => {
258244 it ( 'should retrieve the ePerson currently getting edited, if any' , ( ) => {
259- service . editEPerson ( EPersonMock ) ;
245+ // Update the state with the ePerson (the provideMockStore doesn't update itself when dispatch is called)
246+ store . overrideSelector ( editEPersonSelector , EPersonMock ) ;
260247
261- service . getActiveEPerson ( ) . subscribe ( ( activeEPerson : EPerson ) => {
262- expect ( activeEPerson ) . toEqual ( EPersonMock ) ;
263- } ) ;
248+ expect ( service . getActiveEPerson ( ) ) . toBeObservable ( cold ( 'a' , { a : EPersonMock } ) ) ;
264249 } ) ;
265250
266251 it ( 'should retrieve the ePerson currently getting edited, null if none being edited' , ( ) => {
267- service . getActiveEPerson ( ) . subscribe ( ( activeEPerson : EPerson ) => {
268- expect ( activeEPerson ) . toEqual ( null ) ;
269- } ) ;
252+ expect ( service . getActiveEPerson ( ) ) . toBeObservable ( cold ( 'a' , { a : null } ) ) ;
270253 } ) ;
271254 } ) ;
272255
0 commit comments