typescript 使用rxjs的大理石测试和jsverify

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了typescript 使用rxjs的大理石测试和jsverify相关的知识,希望对你有一定的参考价值。

import {AjaxError, AjaxRequest, TestScheduler} from 'rxjs';
import * as jsc from 'jsverify';
// tslint:disable-next-line:no-require-imports
require('./testing/jasmineHelpers2'); // https://github.com/jsverify/jsverify#usage-with-jasmine

import {handleError} from './requests';
import {testObservableProperty} from './testing/test-observable-property';

describe('handleError', () => {
  testObservableProperty('works with any of error texts', jsc.nestring, function(propertyScheduler, errorText: string) {
    const xhr = new XMLHttpRequest();
    const request: AjaxRequest = {};
    const sourceAjaxErrorData = new AjaxError(errorText, xhr, request);

    const handled$ = handleError(sourceAjaxErrorData);
    const expectedMap = {
      a: sourceAjaxErrorData
    };

    propertyScheduler.expectObservable(handled$).toBe('(a|)', expectedMap);
  });
});
import {TestScheduler} from 'rxjs';
import {Arbitrary} from 'jsverify';
import {equals} from 'ramda';
import * as jsc from 'jsverify';
// tslint:disable-next-line:no-require-imports
require('./jasmineHelpers2'); // https://github.com/jsverify/jsverify#usage-with-jasmine

type TestPropertyHandler = (propertyScheduler: TestScheduler, ...args: any[]) => void;

export function testObservableProperty(description: string, arbitrary1: Arbitrary<any>, handler: TestPropertyHandler): void;
export function testObservableProperty(description: string, arbitrary1: Arbitrary<any>, arbitrary2: Arbitrary<any>, handler: TestPropertyHandler): void;
export function testObservableProperty(description: string, arbitrary1: Arbitrary<any>, arbitrary2: Arbitrary<any>, arbitrary3: Arbitrary<any>, handler: TestPropertyHandler): void;
export function testObservableProperty(description: string, arbitrary1: Arbitrary<any>, ...other: (Arbitrary<any> | TestPropertyHandler)[]): void {
  const passedHandler = other[other.length - 1] as TestPropertyHandler;
  const otherArbitrary = other.slice(0, -1);
  const allArbitrary = [arbitrary1, ...otherArbitrary];

  const innerHandler = (...vars: any[]) => {
    let areEqual = false;
    const propertyScheduler = new TestScheduler((actual: any, expected: any) => {
      areEqual = equals(actual)(expected);

      if (!areEqual) {
        // to have a verbose information about marble test error
        return expect(actual).toEqual(expected);
      }

      return areEqual;
    });

    passedHandler(propertyScheduler, ...vars);

    propertyScheduler.flush();
    return areEqual;
  };

  jsc.property(description, ...[...allArbitrary, innerHandler]);
}

以上是关于typescript 使用rxjs的大理石测试和jsverify的主要内容,如果未能解决你的问题,请参考以下文章

使用 switchmap 和 forkjoin 链接 observables 不能按预期工作 angular typescript rxjs

带有 RxJS 过滤器输入问题的 Typescript

如何解决 TypeScript 2.4 和 RxJS 5.x 中的“主题不正确地扩展 Observable”错误?

typescript 带有dayjs和rxjs的简单角度计数器/倒计时组件

Typescript模块在同一个文件中以不同方式解析'rxjs'

如何解决 rxjs Typescript 错误(Ionic 3,angularfire2)