Skip to content

Commit b7dfe0f

Browse files
Fix EPersonDataService no expectation tests
- The store dispatch was not called through so the store was never updated - To initialize the store using provideMockStore I rewrote the test to work through injection
1 parent c2010b5 commit b7dfe0f

2 files changed

Lines changed: 49 additions & 66 deletions

File tree

src/app/core/eperson/eperson-data.service.spec.ts

Lines changed: 48 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
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';
105
import {
116
EPeopleRegistryCancelEPersonAction,
127
EPeopleRegistryEditEPersonAction
138
} from '../../access-control/epeople-registry/epeople-registry.actions';
149
import { RequestParam } from '../cache/models/request-param.model';
15-
import { ChangeAnalyzer } from '../data/change-analyzer';
1610
import { PatchRequest, PostRequest } from '../data/request.models';
1711
import { RequestService } from '../data/request.service';
1812
import { 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';
2114
import { EPerson } from './models/eperson.model';
2215
import { EPersonMock, EPersonMock2 } from '../../shared/testing/eperson.mock';
2316
import { HALEndpointServiceStub } from '../../shared/testing/hal-endpoint-service.stub';
2417
import { createNoContentRemoteDataObject$, createSuccessfulRemoteDataObject$ } from '../../shared/remote-data.utils';
2518
import { getMockRemoteDataBuildServiceHrefMap } from '../../shared/mocks/remote-data-build.service.mock';
26-
import { TranslateLoaderMock } from '../../shared/mocks/translate-loader.mock';
2719
import { getMockRequestService } from '../../shared/mocks/request.service.mock';
2820
import { createPaginatedList, createRequestEntry$ } from '../../shared/testing/utils.test';
2921
import { CoreState } from '../core-state.model';
3022
import { 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

3233
describe('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

src/app/core/eperson/eperson-data.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import { RestRequestMethod } from '../data/rest-request-method';
3636
import { dataService } from '../data/base/data-service.decorator';
3737

3838
const ePeopleRegistryStateSelector = (state: AppState) => state.epeopleRegistry;
39-
const editEPersonSelector = createSelector(ePeopleRegistryStateSelector, (ePeopleRegistryState: EPeopleRegistryState) => ePeopleRegistryState.editEPerson);
39+
export const editEPersonSelector = createSelector(ePeopleRegistryStateSelector, (ePeopleRegistryState: EPeopleRegistryState) => ePeopleRegistryState.editEPerson);
4040

4141
/**
4242
* A service to retrieve {@link EPerson}s from the REST API & EPerson related CRUD actions

0 commit comments

Comments
 (0)