0
getInactiveTimerTabSynchronizer() {
    return fromEvent(window, 'storage').pipe(
      filter((x: StorageEvent) => {
        return x.key === this.inactivityTabSynchronizerStorageKey;
      }),
      debounceTime(350));
  }

上面的代码有如下单元测试用例:

import { Observable, of as observableOf } from 'rxjs';


it(
    'getInactiveTimerTabSynchronizer() function should return an observable that looks for storage events ' +
      'and filters it to only "INACTIVE_TIMER_RESET" keyed events',
    fakeAsync(
      inject([AuthService], (service: AuthService) => {
        spyOn(Observable, 'fromEvent').and.returnValue(
          observableOf({ key: 'INACTIVE_TIMER_RESET' } as StorageEvent, { key: 'SOME_OTHER_KEY' } as StorageEvent)
        );

        let counter = 0;
        service.getInactiveTimerTabSynchronizer().subscribe(x => {
          counter++;
          expect(counter).toBe(1);
        });
        tick(350);
        tick(350);
      })
    )
  );

当我有 angular 6 和 rxjs 5.5 时它工作正常,但是在我升级到 angular 7 和 rxjs 6.5 后它会抛出错误:

Argument of type '"fromEvent"' is not assignable to parameter of type '"prototype" | "create" | "if" | "throw"'

对修复或解决方法有任何帮助吗?

4

1 回答 1

0

现在必须是spyOn(rxjs, 'fromEvent')

 import * as rxjs from 'rxjs'
 ...
 spyOn(rxjs, 'fromEvent').and.returnValue(
      observableOf({ key: 'INACTIVE_TIMER_RESET' } as StorageEvent, { key: 'SOME_OTHER_KEY' } as StorageEvent)
    );

因为使用 fromEvent 的旧方法是

Rx.Observable.fromEvent(input, 'keyup')
于 2020-06-16T09:42:01.703 回答