rewrite all comcoldataservice tests in hopes of catching the one that sometimes fails at random

This commit is contained in:
Art Lowel
2021-03-16 12:55:15 +01:00
parent 9964f07ff6
commit 4a15720b4b

View File

@@ -1,6 +1,6 @@
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { Store } from '@ngrx/store'; import { Store } from '@ngrx/store';
import { cold, getTestScheduler } from 'jasmine-marbles'; import { cold } from 'jasmine-marbles';
import { Observable, of as observableOf } from 'rxjs'; import { Observable, of as observableOf } from 'rxjs';
import { TestScheduler } from 'rxjs/testing'; import { TestScheduler } from 'rxjs/testing';
import { getMockRequestService } from '../../shared/mocks/request.service.mock'; import { getMockRequestService } from '../../shared/mocks/request.service.mock';
@@ -13,14 +13,13 @@ import { HALEndpointService } from '../shared/hal-endpoint.service';
import { ComColDataService } from './comcol-data.service'; import { ComColDataService } from './comcol-data.service';
import { CommunityDataService } from './community-data.service'; import { CommunityDataService } from './community-data.service';
import { DSOChangeAnalyzer } from './dso-change-analyzer.service'; import { DSOChangeAnalyzer } from './dso-change-analyzer.service';
import { FindListOptions, GetRequest } from './request.models'; import { FindListOptions } from './request.models';
import { RequestEntry } from './request.reducer';
import { RequestService } from './request.service'; import { RequestService } from './request.service';
import { import {
createFailedRemoteDataObject$, createFailedRemoteDataObject$,
createNoContentRemoteDataObject$,
createSuccessfulRemoteDataObject$, createSuccessfulRemoteDataObject$,
createFailedRemoteDataObject createFailedRemoteDataObject,
createSuccessfulRemoteDataObject
} from '../../shared/remote-data.utils'; } from '../../shared/remote-data.utils';
import { BitstreamDataService } from './bitstream-data.service'; import { BitstreamDataService } from './bitstream-data.service';
@@ -50,8 +49,8 @@ class TestService extends ComColDataService<any> {
} }
} }
// tslint:disable:no-shadowed-variable
describe('ComColDataService', () => { describe('ComColDataService', () => {
let scheduler: TestScheduler;
let service: TestService; let service: TestService;
let requestService: RequestService; let requestService: RequestService;
let cds: CommunityDataService; let cds: CommunityDataService;
@@ -60,6 +59,7 @@ describe('ComColDataService', () => {
let bitstreamDataService: BitstreamDataService; let bitstreamDataService: BitstreamDataService;
let rdbService: RemoteDataBuildService; let rdbService: RemoteDataBuildService;
let testScheduler: TestScheduler; let testScheduler: TestScheduler;
let topEndpoint: string;
const store = {} as Store<CoreState>; const store = {} as Store<CoreState>;
const notificationsService = {} as NotificationsService; const notificationsService = {} as NotificationsService;
@@ -70,17 +70,9 @@ describe('ComColDataService', () => {
const options = Object.assign(new FindListOptions(), { const options = Object.assign(new FindListOptions(), {
scopeID: scopeID scopeID: scopeID
}); });
const getRequestEntry$ = (successful: boolean) => {
return observableOf({
response: { isSuccessful: successful } as any
} as RequestEntry);
};
const communitiesEndpoint = 'https://rest.api/core/communities'; const communitiesEndpoint = 'https://rest.api/core/communities';
const communityEndpoint = `${communitiesEndpoint}/${scopeID}`; const communityEndpoint = `${communitiesEndpoint}/${scopeID}`;
const scopedEndpoint = `${communityEndpoint}/${LINK_NAME}`; const scopedEndpoint = `${communityEndpoint}/${LINK_NAME}`;
const serviceEndpoint = `https://rest.api/core/${LINK_NAME}`;
const authHeader = 'Bearer eyJhbGciOiJIUzI1NiJ9.eyJlaWQiOiJhNjA4NmIzNC0zOTE4LTQ1YjctOGRkZC05MzI5YTcwMmEyNmEiLCJzZyI6W10sImV4cCI6MTUzNDk0MDcyNX0.RV5GAtiX6cpwBN77P_v16iG9ipeyiO7faNYSNMzq_sQ';
const mockHalService = { const mockHalService = {
getEndpoint: (linkPath) => observableOf(communitiesEndpoint) getEndpoint: (linkPath) => observableOf(communitiesEndpoint)
@@ -142,6 +134,7 @@ describe('ComColDataService', () => {
}; };
beforeEach(() => { beforeEach(() => {
topEndpoint = 'https://rest.api/core/communities/search/top';
testScheduler = initTestScheduler(); testScheduler = initTestScheduler();
cds = initMockCommunityDataService(); cds = initMockCommunityDataService();
requestService = getMockRequestService(); requestService = getMockRequestService();
@@ -153,27 +146,20 @@ describe('ComColDataService', () => {
}); });
describe('getBrowseEndpoint', () => { describe('getBrowseEndpoint', () => {
beforeEach(() => { it(`should call createAndSendGetRequest with the scope Community's self link`, () => {
scheduler = getTestScheduler(); testScheduler.run(({ cold, flush, expectObservable }) => {
}); (cds.getEndpoint as jasmine.Spy).and.returnValue(cold('a', { a: communitiesEndpoint }));
(rdbService.buildSingle as jasmine.Spy).and.returnValue(cold('a', { a: createFailedRemoteDataObject() }));
it('should send a new FindByIDRequest for the scope Community', () => { spyOn(service as any, 'createAndSendGetRequest');
cds = initMockCommunityDataService(); service.getBrowseEndpoint(options);
requestService = getMockRequestService(getRequestEntry$(true)); flush();
objectCache = initMockObjectCacheService(); expectObservable((service as any).createAndSendGetRequest.calls.argsFor(0)[0]).toBe('(a|)', { a: communityEndpoint });
service = initTestService(); expect((service as any).createAndSendGetRequest.calls.argsFor(0)[1]).toBeTrue();
});
const expected = new GetRequest(requestService.generateRequestId(), communityEndpoint);
scheduler.schedule(() => service.getBrowseEndpoint(options).subscribe());
scheduler.flush();
expect(requestService.send).toHaveBeenCalledWith(expected, true);
}); });
describe('if the scope Community can\'t be found', () => { describe('if the scope Community can\'t be found', () => {
it('should throw an error', () => { it('should throw an error', () => {
// tslint:disable-next-line:no-shadowed-variable
testScheduler.run(({ cold, expectObservable }) => { testScheduler.run(({ cold, expectObservable }) => {
// spies re-defined here to use the "cold" function from rxjs's TestScheduler // spies re-defined here to use the "cold" function from rxjs's TestScheduler
// rather than the one imported from jasmine-marbles. // rather than the one imported from jasmine-marbles.
@@ -186,86 +172,68 @@ describe('ComColDataService', () => {
}); });
}); });
describe('cache refresh', () => { });
let communityWithoutParentHref;
let data;
beforeEach(() => { describe('cache refresh', () => {
spyOn(halService, 'getEndpoint').and.returnValue(observableOf('https://rest.api/core/communities/search/top')); let communityWithoutParentHref;
let communityWithParentHref;
beforeEach(() => {
communityWithParentHref = {
_links: {
parentCommunity: {
href: 'topLevel/parentCommunity'
}
}
} as Community;
communityWithoutParentHref = {
_links: {}
} as Community;
});
describe('cache refreshed top level community', () => {
it(`should refresh the top level community cache when the dso has a parent link that can't be resolved`, () => {
testScheduler.run(({ flush, cold }) => {
spyOn(halService, 'getEndpoint').and.returnValue(cold('a', { a: topEndpoint }));
spyOn(service, 'findByHref').and.returnValue(cold('a', { a: createSuccessfulRemoteDataObject({}) }));
service.refreshCache(communityWithParentHref);
flush();
expect(requestService.setStaleByHrefSubstring).toHaveBeenCalledWith(topEndpoint);
});
}); });
it(`shouldn't do anything when the dso doesn't have a parent link`, () => {
describe('cache refreshed top level community', () => { testScheduler.run(({ flush, cold }) => {
beforeEach(() => { spyOn(halService, 'getEndpoint').and.returnValue(cold('a', { a: topEndpoint }));
(rdbService.buildSingle as jasmine.Spy).and.returnValue(createNoContentRemoteDataObject$()); spyOn(service, 'findByHref').and.returnValue(cold('a', { a: createSuccessfulRemoteDataObject({}) }));
data = { service.refreshCache(communityWithoutParentHref);
dso: Object.assign(new Community(), { flush();
metadata: [{
key: 'dc.title',
value: 'top level community'
}]
}),
_links: {
parentCommunity: {
href: 'topLevel/parentCommunity'
}
}
};
communityWithoutParentHref = {
dso: Object.assign(new Community(), {
metadata: [{
key: 'dc.title',
value: 'top level community'
}]
}),
_links: {}
};
});
it('top level community cache refreshed', () => {
scheduler.schedule(() => (service as any).refreshCache(data));
scheduler.flush();
expect(requestService.setStaleByHrefSubstring).toHaveBeenCalledWith('https://rest.api/core/communities/search/top');
});
it('top level community without parent link, cache not refreshed', () => {
scheduler.schedule(() => (service as any).refreshCache(communityWithoutParentHref));
scheduler.flush();
expect(requestService.setStaleByHrefSubstring).not.toHaveBeenCalled(); expect(requestService.setStaleByHrefSubstring).not.toHaveBeenCalled();
}); });
}); });
describe('cache refreshed child community', () => {
beforeEach(() => {
const parentCommunity = Object.assign(new Community(), {
uuid: 'a20da287-e174-466a-9926-f66as300d399',
id: 'a20da287-e174-466a-9926-f66as300d399',
metadata: [{
key: 'dc.title',
value: 'parent community'
}],
_links: {}
});
(rdbService.buildSingle as jasmine.Spy).and.returnValue(createSuccessfulRemoteDataObject$(parentCommunity));
data = {
dso: Object.assign(new Community(), {
metadata: [{
key: 'dc.title',
value: 'child community'
}]
}),
_links: {
parentCommunity: {
href: 'child/parentCommunity'
}
}
};
});
it('child level community cache refreshed', () => {
scheduler.schedule(() => (service as any).refreshCache(data));
scheduler.flush();
expect(requestService.setStaleByHrefSubstring).toHaveBeenCalledWith('a20da287-e174-466a-9926-f66as300d399');
});
});
}); });
describe('cache refreshed child community', () => {
let parentCommunity: Community;
beforeEach(() => {
parentCommunity = Object.assign(new Community(), {
uuid: 'a20da287-e174-466a-9926-f66as300d399',
id: 'a20da287-e174-466a-9926-f66as300d399',
metadata: [{
key: 'dc.title',
value: 'parent community'
}],
_links: {}
});
});
it('should refresh a specific cached community when the parent link can be resolved', () => {
testScheduler.run(({ flush, cold }) => {
spyOn(halService, 'getEndpoint').and.returnValue(cold('a', { a: topEndpoint }));
spyOn(service, 'findByHref').and.returnValue(cold('a', { a: createSuccessfulRemoteDataObject(parentCommunity) }));
service.refreshCache(communityWithParentHref);
flush();
expect(requestService.setStaleByHrefSubstring).toHaveBeenCalledWith('a20da287-e174-466a-9926-f66as300d399');
});
});
});
}); });
}); });