barbiturat
4/14/2017 - 3:10 PM

Use rxjs's marble-testing together with jsverify

Use rxjs's marble-testing together with 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]);
}