fixed tests after angular 10 upgrade

This commit is contained in:
Giuseppe Digilio
2020-12-03 19:45:05 +01:00
parent 9ed1c74f29
commit 10d0c2e518
212 changed files with 1461 additions and 1333 deletions

View File

@@ -1,13 +1,12 @@
import * as ngrx from '@ngrx/store';
import { ActionsSubject, Store } from '@ngrx/store';
import { cold, getTestScheduler, hot } from 'jasmine-marbles';
import { BehaviorSubject, EMPTY, of as observableOf } from 'rxjs';
import { Store, StoreModule } from '@ngrx/store';
import { cold, getTestScheduler } from 'jasmine-marbles';
import { EMPTY, of as observableOf } from 'rxjs';
import { TestScheduler } from 'rxjs/testing';
import { getMockObjectCacheService } from '../../shared/mocks/object-cache.service.mock';
import { defaultUUID, getMockUUIDService } from '../../shared/mocks/uuid.service.mock';
import { ObjectCacheService } from '../cache/object-cache.service';
import { CoreState } from '../core.reducers';
import { coreReducers, CoreState } from '../core.reducers';
import { UUIDService } from '../shared/uuid.service';
import { RequestConfigureAction, RequestExecuteAction } from './request.actions';
import {
@@ -22,7 +21,9 @@ import {
} from './request.models';
import { RequestEntry } from './request.reducer';
import { RequestService } from './request.service';
import { parseJsonSchemaToCommandDescription } from '@angular/cli/utilities/json-schema';
import { TestBed, waitForAsync } from '@angular/core/testing';
import { storeModuleConfig } from '../../app.reducer';
import { MockStore, provideMockStore } from '@ngrx/store/testing';
describe('RequestService', () => {
let scheduler: TestScheduler;
@@ -31,6 +32,7 @@ describe('RequestService', () => {
let objectCache: ObjectCacheService;
let uuidService: UUIDService;
let store: Store<CoreState>;
let mockStore: MockStore<CoreState>;
const testUUID = '5f2a0d2a-effa-4d54-bd54-5663b960f9eb';
const testHref = 'https://rest.api/endpoint/selfLink';
@@ -41,7 +43,29 @@ describe('RequestService', () => {
const testOptionsRequest = new OptionsRequest(testUUID, testHref);
const testHeadRequest = new HeadRequest(testUUID, testHref);
const testPatchRequest = new PatchRequest(testUUID, testHref);
let selectSpy;
const initialState: any = {
core: {
'cache/object': { },
'cache/syncbuffer': { },
'cache/object-updates': { },
'data/request': { },
'index': { },
}
};
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [
StoreModule.forRoot(coreReducers, storeModuleConfig)
],
providers: [
provideMockStore({ initialState }),
{ provide: RequestService, useValue: service }
]
}).compileComponents();
}));
beforeEach(() => {
scheduler = getTestScheduler();
@@ -51,14 +75,9 @@ describe('RequestService', () => {
uuidService = getMockUUIDService();
store = new Store<CoreState>(new BehaviorSubject({}), new ActionsSubject(), null);
selectSpy = spyOnProperty(ngrx, 'select');
selectSpy.and.callFake(() => {
return () => {
return () => cold('a', { a: undefined });
};
});
store = TestBed.inject(Store);
mockStore = store as MockStore<CoreState>;
mockStore.setState(initialState);
service = new RequestService(
objectCache,
uuidService,
@@ -109,7 +128,7 @@ describe('RequestService', () => {
beforeEach(() => {
spyOn(service, 'getByHref').and.returnValue(observableOf({
completed: false
} as RequestEntry))
} as RequestEntry));
});
it('should return true', () => {
@@ -140,23 +159,21 @@ describe('RequestService', () => {
describe('getByUUID', () => {
describe('if the request with the specified UUID exists in the store', () => {
beforeEach(() => {
let callCounter = 0;
const responses = [
cold('a', { // A direct hit in the request cache
a: {
completed: true
const state = Object.assign({}, initialState, {
core: Object.assign({}, initialState.core, {
'data/request': {
'5f2a0d2a-effa-4d54-bd54-5663b960f9eb': {
completed: true
}
},
'index': {
'get-request/configured-to-cache-uuid': {
'5f2a0d2a-effa-4d54-bd54-5663b960f9eb': '5f2a0d2a-effa-4d54-bd54-5663b960f9eb'
}
}
}),
cold('b', { b: undefined }), // No hit in the index
cold('c', { c: undefined }) // So no mapped hit in the request cache
];
selectSpy.and.callFake(() => {
return () => {
const response = responses[callCounter];
callCounter++;
return () => response;
};
})
});
mockStore.setState(state);
});
it('should return an Observable of the RequestEntry', () => {
@@ -173,57 +190,53 @@ describe('RequestService', () => {
describe(`if the request with the specified UUID doesn't exist in the store `, () => {
beforeEach(() => {
let callCounter = 0;
const responses = [
cold('a', { a: undefined }), // No direct hit in the request cache
cold('b', { b: undefined }), // No hit in the index
cold('c', { c: undefined }), // So no mapped hit in the request cache
];
selectSpy.and.callFake(() => {
return () => {
const response = responses[callCounter];
callCounter++;
return () => response;
};
});
// No direct hit in the request cache
// No hit in the index
// So no mapped hit in the request cache
mockStore.setState(initialState);
});
it('should return an Observable of undefined', () => {
const result = service.getByUUID(testUUID);
scheduler.expectObservable(result).toBe('a', { a: undefined });
const expected = cold('b', {
b: undefined
});
expect(result).toBeObservable(expected);
});
});
describe(`if the request with the specified UUID wasn't sent, because it was already cached`, () => {
beforeEach(() => {
let callCounter = 0;
const responses = [
cold('a', { a: undefined }), // No direct hit in the request cache with that UUID
cold('b', { b: 'otherRequestUUID' }), // A hit in the index, which returns the uuid of the cached request
cold('c', { // the call to retrieve the cached request using the UUID from the index
c: {
completed: true
// No direct hit in the request cache with that UUID
// A hit in the index, which returns the uuid of the cached request
// the call to retrieve the cached request using the UUID from the index
const state = Object.assign({}, initialState, {
core: Object.assign({}, initialState.core, {
'data/request': {
'otherRequestUUID': {
completed: true
}
},
'index': {
'get-request/configured-to-cache-uuid': {
'5f2a0d2a-effa-4d54-bd54-5663b960f9eb': 'otherRequestUUID'
}
}
})
];
selectSpy.and.callFake(() => {
return () => {
const response = responses[callCounter];
callCounter++;
return () => response;
};
});
mockStore.setState(state);
});
it(`it should return the cached request`, () => {
const result = service.getByUUID(testUUID);
scheduler.expectObservable(result).toBe('c', {
c: {
const expected = cold('b', {
b: {
completed: true
}
});
expect(result).toBeObservable(expected);
});
});
@@ -232,16 +245,24 @@ describe('RequestService', () => {
describe('getByHref', () => {
describe('when the request with the specified href exists in the store', () => {
beforeEach(() => {
selectSpy.and.callFake(() => {
return () => {
return () => hot('a', { a: testUUID });
};
const state = Object.assign({}, initialState, {
core: Object.assign({}, initialState.core, {
'data/request': {
'5f2a0d2a-effa-4d54-bd54-5663b960f9eb': {
completed: true
}
},
'index': {
'get-request/configured-to-cache-uuid': {
'5f2a0d2a-effa-4d54-bd54-5663b960f9eb': '5f2a0d2a-effa-4d54-bd54-5663b960f9eb'
},
'get-request/href-to-uuid': {
'https://rest.api/endpoint/selfLink': '5f2a0d2a-effa-4d54-bd54-5663b960f9eb'
}
}
})
});
spyOn(service, 'getByUUID').and.returnValue(cold('b', {
b: {
completed: true
}
}));
mockStore.setState(state);
});
it('should return an Observable of the RequestEntry', () => {
@@ -258,14 +279,10 @@ describe('RequestService', () => {
describe('when the request with the specified href doesn\'t exist in the store', () => {
beforeEach(() => {
selectSpy.and.callFake(() => {
return () => {
return () => hot('a', { a: undefined });
};
});
spyOn(service, 'getByUUID').and.returnValue(cold('b', {
b: undefined
}));
// No direct hit in the request cache
// No hit in the index
// So no mapped hit in the request cache
mockStore.setState(initialState);
});
it('should return an Observable of undefined', () => {
@@ -455,11 +472,7 @@ describe('RequestService', () => {
describe('when the request is added to the store', () => {
it('should stop tracking the request', () => {
selectSpy.and.callFake(() => {
return () => {
return () => observableOf({ request });
};
});
spyOn(serviceAsAny, 'getByHref').and.returnValue(observableOf({ request }));
serviceAsAny.trackRequestsOnTheirWayToTheStore(request);
expect(serviceAsAny.requestsOnTheirWayToTheStore.includes(request.href)).toBeFalsy();
});
@@ -475,7 +488,7 @@ describe('RequestService', () => {
});
it('return an observable emitting false', () => {
expect(valid).toBe(false);
})
});
});
describe('when the given entry has a value, but the request is not completed', () => {
@@ -487,7 +500,7 @@ describe('RequestService', () => {
});
it('return an observable emitting false', () => {
expect(valid).toBe(false);
})
});
});
describe('when the given entry has a value, but the response is not successful', () => {
@@ -499,7 +512,7 @@ describe('RequestService', () => {
});
it('return an observable emitting false', () => {
expect(valid).toBe(false);
})
});
});
describe('when the given UUID has a value, its response was successful, but the response is outdated', () => {
@@ -526,7 +539,7 @@ describe('RequestService', () => {
it('return an observable emitting false', () => {
expect(valid).toBe(false);
})
});
});
describe('when the given UUID has a value, a cached entry is found, its response was successful, and the response is not outdated', () => {
@@ -553,8 +566,8 @@ describe('RequestService', () => {
it('return an observable emitting true', () => {
expect(valid).toBe(true);
})
})
});
});
});
describe('hasByHref', () => {