-import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
+import { HttpRequest } from '@angular/common/http';
+import {
+ HttpClientTestingModule,
+ HttpTestingController,
+ TestRequest
+} from '@angular/common/http/testing';
import { fakeAsync, TestBed, tick } from '@angular/core/testing';
import { configureTestBed } from '../../../testing/unit-test-helper';
describe('RbdMirroringService', () => {
let service: RbdMirroringService;
let httpTesting: HttpTestingController;
+ let getMirroringSummaryCalls: () => TestRequest[];
+ let flushCalls: (call: TestRequest) => void;
const summary: Record<string, any> = {
status: 0,
beforeEach(() => {
service = TestBed.get(RbdMirroringService);
httpTesting = TestBed.get(HttpTestingController);
-
- const req = httpTesting.expectOne('api/block/mirroring/summary');
- expect(req.request.method).toBe('GET');
- req.flush(summary);
+ getMirroringSummaryCalls = () => {
+ return httpTesting.match((request: HttpRequest<any>) => {
+ return request.url.match(/api\/block\/mirroring\/summary/) && request.method === 'GET';
+ });
+ };
+ flushCalls = (call: TestRequest) => {
+ if (!call.cancelled) {
+ call.flush(summary);
+ }
+ };
});
afterEach(() => {
});
it('should periodically poll summary', fakeAsync(() => {
+ const subs = service.startPolling();
+ tick();
const calledWith: any[] = [];
- service.subscribeSummary((data) => {
+ service.subscribeSummary((data: any) => {
calledWith.push(data);
});
- service.refreshAndSchedule();
- tick(30000);
- // In order to not trigger setTimeout again,
- // which would raise 'Error: 1 timer(s) still in the queue.'
- spyOn(service, 'refreshAndSchedule').and.callFake(() => true);
- tick(30000);
-
- const calls = httpTesting.match((request) => {
- return request.url.match(/api\/block\/mirroring\/summary/) && request.method === 'GET';
- });
+ tick(service.REFRESH_INTERVAL * 2);
+ const calls = getMirroringSummaryCalls();
- expect(calls.length).toEqual(2);
- calls.forEach((call) => call.flush(summary));
+ expect(calls.length).toEqual(3);
+ calls.forEach((call: TestRequest) => flushCalls(call));
+ expect(calledWith).toEqual([null, summary]);
- expect(calledWith).toEqual([summary, summary, summary]);
+ subs.unsubscribe();
}));
it('should get current summary', () => {
+ service.refresh();
+ const calledWith: any[] = [];
+ service.subscribeSummary((data: any) => {
+ calledWith.push(data);
+ });
+ const calls = getMirroringSummaryCalls();
+ calls.forEach((call: TestRequest) => flushCalls(call));
+
expect(service.getCurrentSummary()).toEqual(summary);
});