All files / src/testing-helpers/subscribeAndExpect subscribeAndExpect.hlpr.ts

100% Statements 47/47
100% Branches 18/18
100% Functions 15/15
100% Lines 43/43
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96          1x 1x 1x   1x   4x           3x 2x       1x     4x 4x     1x 1x 1x   1x   6x 6x       4x 2x   4x     1x           5x 1x       6x 6x     1x 1x 1x   1x   5x 5x     5x     1x     4x 3x       5x 5x     1x 9x 8x 6x 3x 6x   2x    
import { Observable } from 'rxjs/Observable';
 
/*
  Testing helper functions for observables
*/
export function subscribeAndExpectAllValues(observable: Observable<any>, expected: any[] ) {
  const failed = subscribeAndTestAllValues(observable, expected);
  expect(failed).toBeFalsy(failed);
}
export function subscribeAndTestAllValues(observable: Observable<any>, expected: any[] ): string {
  let fail;
  const sub = observable
    .toArray()  // toArray returns all results,
                // and ensures the subscription (and expectation)
                // happens even when there are no observables output (result is empty array)
    .subscribe(
      result => {
        if (!arraysEqual(result, expected)) {
          fail = 'Subscription results do not match expected array';
        }
      },
      (error) => {
        fail = 'Subscription raised an error';
      }
    );
  sub.unsubscribe();
  return fail;
}
 
export function subscribeAndExpectValue(observable: Observable<any>, expected: any) {
  const failed = subscribeAndTestValue(observable, expected);
  expect(failed).toBeFalsy(failed);
}
export function subscribeAndTestValue(observable: Observable<any>, expected: any): string {
  let fail;
  let wasSubscribed = false;
  const sub = observable
    .take(1)
    .subscribe(
      result => {
        if (result !== expected) {
          fail = 'Subscription result does not match expected value';
        }
        wasSubscribed = true;
      },
      (error) => {
        fail = 'Subscription raised an error';
      },
      (/*completed*/) => {
        // When testing a single value,
        // need to check that the subscription was activated,
        // otherwise the expected value is never tested
        if (!wasSubscribed) {
          fail = 'Subscription produced no results';
        }
      }
    );
  sub.unsubscribe();
  return fail;
}
 
export function subscribeAndExpectNoDataEmitted(observable: Observable<any>) {
  const failed = subscribeAndTestNoDataEmitted(observable);
  expect(failed).toBeFalsy(failed);
}
export function subscribeAndTestNoDataEmitted(observable: Observable<any>): string {
  let fail;
  let wasSubscribed = false;
  const sub = observable
    .subscribe(
      result => {
        wasSubscribed = true;
      },
      (error) => {
        fail = 'Subscription raised an error';
      },
      (/*completed*/) => {
        if (wasSubscribed) {
          fail = 'Subscription produced values when none were expected';
        }
      }
    );
  sub.unsubscribe();
  return fail;
}
 
export function arraysEqual(a, b) {
  if (a === b) {return true; };
  if (a == null || b == null) {return false; };
  if (a.length !== b.length) {return false; };
  for (let i = 0; i < a.length; ++i) {
    if (a[i] !== b[i]) {return false; };
  }
  return true;
}