107873: Rework autoRefreshingSearchBy test

This commit is contained in:
Andreas Awouters
2024-01-12 09:51:33 +01:00
parent 2bda29ad1d
commit 4061aaac2e

View File

@@ -9,7 +9,7 @@
import { testFindAllDataImplementation } from '../base/find-all-data.spec'; import { testFindAllDataImplementation } from '../base/find-all-data.spec';
import { ProcessDataService, TIMER_FACTORY } from './process-data.service'; import { ProcessDataService, TIMER_FACTORY } from './process-data.service';
import { testDeleteDataImplementation } from '../base/delete-data.spec'; import { testDeleteDataImplementation } from '../base/delete-data.spec';
import { waitForAsync, TestBed } from '@angular/core/testing'; import { waitForAsync, TestBed, fakeAsync, tick, flush } from '@angular/core/testing';
import { RequestService } from '../request.service'; import { RequestService } from '../request.service';
import { RemoteData } from '../remote-data'; import { RemoteData } from '../remote-data';
import { RequestEntryState } from '../request-entry-state.model'; import { RequestEntryState } from '../request-entry-state.model';
@@ -26,6 +26,7 @@ import { TestScheduler } from 'rxjs/testing';
import { testSearchDataImplementation } from '../base/search-data.spec'; import { testSearchDataImplementation } from '../base/search-data.spec';
import { PaginatedList } from '../paginated-list.model'; import { PaginatedList } from '../paginated-list.model';
import { FindListOptions } from '../find-list-options.model'; import { FindListOptions } from '../find-list-options.model';
import { of } from 'rxjs';
describe('ProcessDataService', () => { describe('ProcessDataService', () => {
let testScheduler; let testScheduler;
@@ -130,9 +131,7 @@ describe('ProcessDataService', () => {
describe('autoRefreshingSearchBy', () => { describe('autoRefreshingSearchBy', () => {
beforeEach(waitForAsync(() => { beforeEach(waitForAsync(() => {
testScheduler = new TestScheduler((actual, expected) => {
expect(actual).toEqual(expected);
});
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [], imports: [],
providers: [ providers: [
@@ -152,8 +151,7 @@ describe('ProcessDataService', () => {
processDataService = TestBed.inject(ProcessDataService); processDataService = TestBed.inject(ProcessDataService);
})); }));
it('should refresh after the specified interval', () => { it('should refresh after the specified interval', fakeAsync(() => {
testScheduler.run(({ cold, expectObservable }) => {
const runningProcess = Object.assign(new Process(), { const runningProcess = Object.assign(new Process(), {
_links: { _links: {
self: { self: {
@@ -162,36 +160,33 @@ describe('ProcessDataService', () => {
} }
}); });
runningProcess.processStatus = ProcessStatus.RUNNING; runningProcess.processStatus = ProcessStatus.RUNNING;
const completedProcess = new Process();
completedProcess.processStatus = ProcessStatus.COMPLETED;
const runningProcessPagination: PaginatedList<Process> = Object.assign(new PaginatedList(), { const runningProcessPagination: PaginatedList<Process> = Object.assign(new PaginatedList(), {
page: [runningProcess], page: [runningProcess],
}); _links: {
const completedProcessPagination: PaginatedList<Process> = Object.assign(new PaginatedList(), { self: {
page: [completedProcess], href: 'https://rest.api/processesList/456'
}
}
}); });
const runningProcessRD = new RemoteData(0, 0, 0, RequestEntryState.Success, null, runningProcessPagination); const runningProcessRD = new RemoteData(0, 0, 0, RequestEntryState.Success, null, runningProcessPagination);
const completedProcessRD = new RemoteData(0, 0, 0, RequestEntryState.Success, null, completedProcessPagination);
spyOn(processDataService, 'invalidateByHref'); spyOn(processDataService, 'invalidateByHref');
spyOn(processDataService, 'searchBy').and.returnValue( spyOn(processDataService, 'searchBy').and.returnValue(
cold('r 150ms c', { of(runningProcessRD)
'r': runningProcessRD,
'c': completedProcessRD
})
); );
let process$ = processDataService.autoRefreshingSearchBy('byProperty', new FindListOptions(), 100); let sub = processDataService.autoRefreshingSearchBy('byProperty', new FindListOptions(), 200).subscribe();
expectObservable(process$).toBe('r 150ms c', { tick(0);
'r': runningProcessRD,
'c': completedProcessRD
});
});
expect(processDataService.searchBy).toHaveBeenCalledTimes(1); expect(processDataService.searchBy).toHaveBeenCalledTimes(1);
}); tick(450);
expect(processDataService.searchBy).toHaveBeenCalledTimes(3);
sub.unsubscribe();
flush();
expect(processDataService.invalidateByHref).toHaveBeenCalledTimes(3);
}));
}); });
}); });