正常に動作する ng-idle でアイドル機能を実装しました。Karma/jasmine でテストを実装しようとしていますが、idle-service を使用できません:
更新2
import { Injectable, OnInit, OnDestroy} from '@angular/core';
import { RoutingService } from '../../services/routing/routing.service';
import { Idle, DEFAULT_INTERRUPTSOURCES } from '@ng-idle/core';
import { Keepalive } from '@ng-idle/keepalive';
@Injectable()
export class TimeoutService {
idleState = 'Not started.';
timedOut = false;
lastPing?: Date = null;
title = 'angular-idle-timeout';
idleTime = 0;
timeoutTime = 0;
constructor(
public idle: Idle,
private keepalive: Keepalive,
public routingService: RoutingService) {
}
initIdle(): void {
this.idle.setIdle(5);
this.idle.setTimeout(5);
this.idle.setInterrupts(DEFAULT_INTERRUPTSOURCES);
this.idle.onIdleEnd.subscribe(() => {
this.idleState = 'No longer idle.';
this.reset();
});
this.idle.onTimeout.subscribe(() => {
this.idleState = 'Timed out!';
this.timedOut = true;
this.idle.stop();
this.timedOut = false;
this.routingService.navigateToIndex();
});
this.idle.onIdleStart.subscribe(() => {
this.idleState = 'Inaktivität nach 10 Sekunden festgestellt!';
});
this.idle.onTimeoutWarning.subscribe((countdown) => {
this.idleState = 'Buchvorgang wird abgebrochen in ' + countdown + ' Sekunden!';
});
this.keepalive.interval(15);
this.keepalive.onPing.subscribe(() => this.lastPing = new Date());
this.reset();
}
setIdleTime(idleTime): void {
this.idleTime = idleTime;
}
setTimeoutTime(timeoutTime): void {
this.timeoutTime = timeoutTime;
}
getIdleTime(): number {
return this.idleTime;
}
reset() {
this.idle.watch();
this.idleState = 'Started.';
this.timedOut = false;
}
}
注入可能なサービスの私の仕様
import { Router } from '@angular/router';
import {async} from '@angular/core/testing';
import { inject, TestBed } from '@angular/core/testing';
import { RoutingService } from '../../services/routing/routing.service';
import { RouterTestingModule } from '@angular/router/testing';
import { TimeoutService } from './timeout.service';
import { Idle, IdleExpiry, DEFAULT_INTERRUPTSOURCES } from '@ng-idle/core';
import { CoreTestingModule } from '../../core-testing.module';
import { Keepalive } from '@ng-idle/keepalive';
import { HttpClient, HttpHandler} from '@angular/common/http';
import { HttpClientTestingModule } from '@angular/common/http/testing';
//import { MockExpiry } from './MockExpiry';
fdescribe('TimeoutService', () => {
/** let router: Router; */
let service: TimeoutService;
let router: Router;
const mockIdleService = jasmine.createSpyObj('idle', ['initIdle', 'setIdleTime',
'setTimeoutTime', 'reset',
'setIdle','setTimeout','setInterrupts',
'onIdleEnd','onTimeout','onIdleStart',
'onTimeoutWarning','keepalive','idling']);
const spies = {
navigate: null
};
jasmine.clock().install();
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule, RouterTestingModule],
providers: [HttpClient, RoutingService,
Keepalive, TimeoutService, Idle, IdleExpiry, { provide: IdleExpiry, useValue: mockIdleService }]
});
service = TestBed.get(TimeoutService);
router = TestBed.get(Router);
spies.navigate = spyOn(router, 'navigate').and.stub();
}));
fit('should navigate to index after 15 Seconds', () => {
console.log('IDLE TIME VOR DEM INIT-IDLE' + service.idleTime);
service.initIdle();
service.setIdleTime(1);
service.setTimeoutTime(1);
expect(service.idleTime).toBe( 1 );
expect(service.timeoutTime).toBe( 1 );
expect(spies.navigate).toHaveBeenCalledWith(['/index']);
});
});
テスト中のエラーは次のとおりです。
TypeError: this.expiry.now は TimeoutService.initIdle の TimeoutService.reset の Idle.watch の関数ではありません