rewrite community scope test that sometimes failed at random

This commit is contained in:
Art Lowel
2021-03-15 13:59:29 +01:00
parent 2f7060a752
commit 9964f07ff6

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, hot } from 'jasmine-marbles'; import { cold, getTestScheduler } 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';
@@ -19,10 +19,10 @@ import { RequestService } from './request.service';
import { import {
createFailedRemoteDataObject$, createFailedRemoteDataObject$,
createNoContentRemoteDataObject$, createNoContentRemoteDataObject$,
createSuccessfulRemoteDataObject$ createSuccessfulRemoteDataObject$,
createFailedRemoteDataObject
} from '../../shared/remote-data.utils'; } from '../../shared/remote-data.utils';
import { BitstreamDataService } from './bitstream-data.service'; import { BitstreamDataService } from './bitstream-data.service';
import { take } from 'rxjs/operators';
const LINK_NAME = 'test'; const LINK_NAME = 'test';
@@ -59,6 +59,7 @@ describe('ComColDataService', () => {
let halService: any = {}; let halService: any = {};
let bitstreamDataService: BitstreamDataService; let bitstreamDataService: BitstreamDataService;
let rdbService: RemoteDataBuildService; let rdbService: RemoteDataBuildService;
let testScheduler: TestScheduler;
const store = {} as Store<CoreState>; const store = {} as Store<CoreState>;
const notificationsService = {} as NotificationsService; const notificationsService = {} as NotificationsService;
@@ -98,8 +99,8 @@ describe('ComColDataService', () => {
} }
function initMockCommunityDataService(): CommunityDataService { function initMockCommunityDataService(): CommunityDataService {
return jasmine.createSpyObj('responseCache', { return jasmine.createSpyObj('cds', {
getEndpoint: hot('--a-', { a: communitiesEndpoint }), getEndpoint: cold('--a-', { a: communitiesEndpoint }),
getIDHref: communityEndpoint getIDHref: communityEndpoint
}); });
} }
@@ -134,7 +135,14 @@ describe('ComColDataService', () => {
); );
} }
const initTestScheduler = (): TestScheduler => {
return new TestScheduler((actual, expected) => {
expect(actual).toEqual(expected);
});
};
beforeEach(() => { beforeEach(() => {
testScheduler = initTestScheduler();
cds = initMockCommunityDataService(); cds = initMockCommunityDataService();
requestService = getMockRequestService(); requestService = getMockRequestService();
objectCache = initMockObjectCacheService(); objectCache = initMockObjectCacheService();
@@ -165,10 +173,16 @@ describe('ComColDataService', () => {
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', () => {
const result = service.getBrowseEndpoint(options).pipe(take(1)); // tslint:disable-next-line:no-shadowed-variable
const expected = cold('--#-', undefined, new Error(`The Community with scope ${scopeID} couldn't be retrieved`)); testScheduler.run(({ cold, expectObservable }) => {
// spies re-defined here to use the "cold" function from rxjs's TestScheduler
expect(result).toBeObservable(expected); // rather than the one imported from jasmine-marbles.
// Mixing the two seems to lead to unpredictable results
(cds.getEndpoint as jasmine.Spy).and.returnValue(cold('a', { a: communitiesEndpoint }));
(rdbService.buildSingle as jasmine.Spy).and.returnValue(cold('a', { a: createFailedRemoteDataObject() }));
const expectedError = new Error(`The Community with scope ${scopeID} couldn't be retrieved`);
expectObservable(service.getBrowseEndpoint(options)).toBe('#', undefined, expectedError);
});
}); });
}); });