Unit testing 如何测试非分派键盘键入效果
我有一种效果,可以检测用户是否在某个时间内一直在打字。这是一种非分派效应——或者更准确地说,它确实分派动作,但只是作为副作用 在测试它时,我试着迷失方向——我试着用间谍攻击Unit testing 如何测试非分派键盘键入效果,unit-testing,ngrx,ngrx-effects,Unit Testing,Ngrx,Ngrx Effects,我有一种效果,可以检测用户是否在某个时间内一直在打字。这是一种非分派效应——或者更准确地说,它确实分派动作,但只是作为副作用 在测试它时,我试着迷失方向——我试着用间谍攻击store.dispatch(不需要弹珠),但也要用弹珠 export class KeyboardEffect { userTyping$ = createEffect(() => { const keydowns$ = fromEvent(document, 'keydown'); const k
store.dispatch
(不需要弹珠),但也要用弹珠
export class KeyboardEffect {
userTyping$ = createEffect(() => {
const keydowns$ = fromEvent(document, 'keydown');
const keyboardIdle$ = keydowns$.pipe(
debounceTime(10 * 1000), // consider keyboard not used after this time
tap(() => this.store.dispatch(new keyboardActions.inactive()),
);
return keydowns$.pipe(
throttle(() => keyboardIdle$),
tap(() => this.store.dispatch(new keyboardActions.active()),
);
}, {dispatch: false});
}
我的尝试:
it('should test keyboard activity', () => {
scheduler.run(({ hot }) => {
jest.spyOn(store, 'dispatch');
const active = new keyboardActions.active();
const inactive = new keyboardActions.inactive();
const keydowns$ = hot('----a--------|');
const keyboardIdle$ = keydowns$.pipe(
debounceTime(10 * 1000), // consider keyboard inactive after this time
tap(() => this.store.dispatch(inactive),
);
const effect$ = keydowns$.pipe(
throttle(() => keyboardIdle$),
tap(() => this.store.dispatch(active),
);
actions$ = of(inactive);
effect$.subscribe(() => {
expect(store.dispatch).toHaveBeenCalledWith(inactive);
// expectObservable(effects.userTyping$).toBe('-a|', {a: idleAction})
})
})
});
问题:无论中的参数是什么,此测试都会通过,以使其与一起调用-一切似乎都正常
哪种方法最适合这种效果-大理石还是间谍
另外,据我所知,testScheduler.run
会影响调度器在回调中解释时间线的方式,因此我希望这也适用于debounceTime
-在这种情况下,我是否仍应遵循所述的参数化效果技术并提供测试调度器,或者这只是一种避免将效果代码复制到规范中的方法