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
This commit is contained in:
Alexandre Vryghem
2023-07-08 16:35:11 +02:00
parent c2010b5d5e
commit b7dfe0f23d
2 changed files with 50 additions and 67 deletions

View File

@@ -1,39 +1,39 @@
import { CommonModule } from '@angular/common'; import { fakeAsync, TestBed, tick, waitForAsync } from '@angular/core/testing';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; import { Store } from '@ngrx/store';
import { TestBed, waitForAsync } from '@angular/core/testing'; import { cold } from 'jasmine-marbles';
import { Store, StoreModule } from '@ngrx/store'; import { of as observableOf } from 'rxjs';
import { compare, Operation } from 'fast-json-patch';
import { getTestScheduler } from 'jasmine-marbles';
import { Observable, of as observableOf } from 'rxjs';
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { TestScheduler } from 'rxjs/testing';
import { import {
EPeopleRegistryCancelEPersonAction, EPeopleRegistryCancelEPersonAction,
EPeopleRegistryEditEPersonAction EPeopleRegistryEditEPersonAction
} from '../../access-control/epeople-registry/epeople-registry.actions'; } from '../../access-control/epeople-registry/epeople-registry.actions';
import { RequestParam } from '../cache/models/request-param.model'; import { RequestParam } from '../cache/models/request-param.model';
import { ChangeAnalyzer } from '../data/change-analyzer';
import { PatchRequest, PostRequest } from '../data/request.models'; import { PatchRequest, PostRequest } from '../data/request.models';
import { RequestService } from '../data/request.service'; import { RequestService } from '../data/request.service';
import { HALEndpointService } from '../shared/hal-endpoint.service'; import { HALEndpointService } from '../shared/hal-endpoint.service';
import { Item } from '../shared/item.model'; import { editEPersonSelector, EPersonDataService } from './eperson-data.service';
import { EPersonDataService } from './eperson-data.service';
import { EPerson } from './models/eperson.model'; import { EPerson } from './models/eperson.model';
import { EPersonMock, EPersonMock2 } from '../../shared/testing/eperson.mock'; import { EPersonMock, EPersonMock2 } from '../../shared/testing/eperson.mock';
import { HALEndpointServiceStub } from '../../shared/testing/hal-endpoint-service.stub'; import { HALEndpointServiceStub } from '../../shared/testing/hal-endpoint-service.stub';
import { createNoContentRemoteDataObject$, createSuccessfulRemoteDataObject$ } from '../../shared/remote-data.utils'; import { createNoContentRemoteDataObject$, createSuccessfulRemoteDataObject$ } from '../../shared/remote-data.utils';
import { getMockRemoteDataBuildServiceHrefMap } from '../../shared/mocks/remote-data-build.service.mock'; import { getMockRemoteDataBuildServiceHrefMap } from '../../shared/mocks/remote-data-build.service.mock';
import { TranslateLoaderMock } from '../../shared/mocks/translate-loader.mock';
import { getMockRequestService } from '../../shared/mocks/request.service.mock'; import { getMockRequestService } from '../../shared/mocks/request.service.mock';
import { createPaginatedList, createRequestEntry$ } from '../../shared/testing/utils.test'; import { createPaginatedList, createRequestEntry$ } from '../../shared/testing/utils.test';
import { CoreState } from '../core-state.model'; import { CoreState } from '../core-state.model';
import { FindListOptions } from '../data/find-list-options.model'; import { FindListOptions } from '../data/find-list-options.model';
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
import { ObjectCacheService } from '../cache/object-cache.service';
import { DSOChangeAnalyzer } from '../data/dso-change-analyzer.service';
import { NotificationsService } from '../../shared/notifications/notifications.service';
import { NotificationsServiceStub } from '../../shared/testing/notifications-service.stub';
import { MockStore, provideMockStore } from '@ngrx/store/testing';
import { compare, Operation } from 'fast-json-patch';
import { Item } from '../shared/item.model';
import { ChangeAnalyzer } from '../data/change-analyzer';
describe('EPersonDataService', () => { describe('EPersonDataService', () => {
let service: EPersonDataService; let service: EPersonDataService;
let store: Store<CoreState>; let store: MockStore<CoreState>;
let requestService: RequestService; let requestService: RequestService;
let scheduler: TestScheduler;
let epeople; let epeople;
@@ -43,50 +43,38 @@ describe('EPersonDataService', () => {
let epeople$; let epeople$;
let rdbService; let rdbService;
function initTestService() { const initialState = {
return new EPersonDataService( epeopleRegistry: {
requestService, editEPerson: null
rdbService, },
null, };
halService,
new DummyChangeAnalyzer() as any,
null,
store,
);
}
function init() { beforeEach(waitForAsync(() => {
restEndpointURL = 'https://rest.api/dspace-spring-rest/api/eperson'; restEndpointURL = 'https://rest.api/dspace-spring-rest/api/eperson';
epersonsEndpoint = `${restEndpointURL}/epersons`; epersonsEndpoint = `${restEndpointURL}/epersons`;
epeople = [EPersonMock, EPersonMock2]; epeople = [EPersonMock, EPersonMock2];
epeople$ = createSuccessfulRemoteDataObject$(createPaginatedList([epeople])); epeople$ = createSuccessfulRemoteDataObject$(createPaginatedList([epeople]));
rdbService = getMockRemoteDataBuildServiceHrefMap(undefined, { 'https://rest.api/dspace-spring-rest/api/eperson/epersons': epeople$ }); rdbService = getMockRemoteDataBuildServiceHrefMap(undefined, { 'https://rest.api/dspace-spring-rest/api/eperson/epersons': epeople$ });
halService = new HALEndpointServiceStub(restEndpointURL); halService = new HALEndpointServiceStub(restEndpointURL);
requestService = getMockRequestService(createRequestEntry$(epeople));
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [ providers: [
CommonModule, EPersonDataService,
StoreModule.forRoot({}), { provide: RequestService, useValue: requestService },
TranslateModule.forRoot({ { provide: RemoteDataBuildService, useValue: rdbService },
loader: { { provide: HALEndpointService, useValue: halService },
provide: TranslateLoader, provideMockStore({ initialState }),
useClass: TranslateLoaderMock { provide: ObjectCacheService, useValue: {} },
} { provide: DSOChangeAnalyzer, useClass: DummyChangeAnalyzer },
}), { provide: NotificationsService, useClass: NotificationsServiceStub },
], ],
declarations: [],
providers: [],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}); });
}
beforeEach(() => { service = TestBed.inject(EPersonDataService);
init(); store = TestBed.inject(Store) as MockStore<CoreState>;
requestService = getMockRequestService(createRequestEntry$(epeople)); spyOn(store, 'dispatch').and.callThrough();
store = new Store<CoreState>(undefined, undefined, undefined); }));
service = initTestService();
spyOn(store, 'dispatch');
});
describe('searchByScope', () => { describe('searchByScope', () => {
beforeEach(() => { beforeEach(() => {
@@ -239,34 +227,29 @@ describe('EPersonDataService', () => {
}); });
describe('clearEPersonRequests', () => { describe('clearEPersonRequests', () => {
beforeEach(waitForAsync(() => { beforeEach(() => {
scheduler = getTestScheduler(); spyOn(halService, 'getEndpoint').and.callFake((linkPath: string) => {
halService = { return observableOf(`${restEndpointURL}/${linkPath}`);
getEndpoint(linkPath: string): Observable<string> { });
return observableOf(restEndpointURL + '/' + linkPath);
}
} as HALEndpointService;
initTestService();
service.clearEPersonRequests();
}));
it('should remove the eperson hrefs in the request service', () => {
expect(requestService.removeByHrefSubstring).toHaveBeenCalledWith(epersonsEndpoint);
}); });
it('should remove the eperson hrefs in the request service', fakeAsync(() => {
service.clearEPersonRequests();
tick();
expect(requestService.removeByHrefSubstring).toHaveBeenCalledWith(epersonsEndpoint);
}));
}); });
describe('getActiveEPerson', () => { describe('getActiveEPerson', () => {
it('should retrieve the ePerson currently getting edited, if any', () => { it('should retrieve the ePerson currently getting edited, if any', () => {
service.editEPerson(EPersonMock); // Update the state with the ePerson (the provideMockStore doesn't update itself when dispatch is called)
store.overrideSelector(editEPersonSelector, EPersonMock);
service.getActiveEPerson().subscribe((activeEPerson: EPerson) => { expect(service.getActiveEPerson()).toBeObservable(cold('a', { a: EPersonMock }));
expect(activeEPerson).toEqual(EPersonMock);
});
}); });
it('should retrieve the ePerson currently getting edited, null if none being edited', () => { it('should retrieve the ePerson currently getting edited, null if none being edited', () => {
service.getActiveEPerson().subscribe((activeEPerson: EPerson) => { expect(service.getActiveEPerson()).toBeObservable(cold('a', { a: null }));
expect(activeEPerson).toEqual(null);
});
}); });
}); });

View File

@@ -36,7 +36,7 @@ import { RestRequestMethod } from '../data/rest-request-method';
import { dataService } from '../data/base/data-service.decorator'; import { dataService } from '../data/base/data-service.decorator';
const ePeopleRegistryStateSelector = (state: AppState) => state.epeopleRegistry; const ePeopleRegistryStateSelector = (state: AppState) => state.epeopleRegistry;
const editEPersonSelector = createSelector(ePeopleRegistryStateSelector, (ePeopleRegistryState: EPeopleRegistryState) => ePeopleRegistryState.editEPerson); export const editEPersonSelector = createSelector(ePeopleRegistryStateSelector, (ePeopleRegistryState: EPeopleRegistryState) => ePeopleRegistryState.editEPerson);
/** /**
* A service to retrieve {@link EPerson}s from the REST API & EPerson related CRUD actions * A service to retrieve {@link EPerson}s from the REST API & EPerson related CRUD actions