Skip to content

Commit

Permalink
test(distinctUntilChanged): add subscription assertions for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
staltz authored and kwonoj committed Nov 25, 2015
1 parent 358a687 commit 227c168
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions spec/operators/distinctUntilChanged-spec.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,69 @@
/* globals describe, it, expect, expectObservable, hot */
/* globals describe, it, expect, expectObservable, expectSubscriptions, cold, hot */
var Rx = require('../../dist/cjs/Rx');
var Observable = Rx.Observable;

describe('Observable.prototype.distinctUntilChanged()', function () {
it('should distinguish between values', function () {
var e1 = hot('--a--a--a--b--b--a--|');
var e1subs = '^ !';
var expected = '--a--------b-----a--|';

expectObservable(e1.distinctUntilChanged()).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should distinguish between values and does not completes', function () {
var e1 = hot('--a--a--a--b--b--a-');
var e1subs = '^ ';
var expected = '--a--------b-----a-';

expectObservable(e1.distinctUntilChanged()).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should not completes if source never completes', function () {
var e1 = Observable.never();
var e1 = cold('-');
var e1subs = '^';
var expected = '-';

expectObservable(e1.distinctUntilChanged()).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should not completes if source does not completes', function () {
var e1 = hot('-');
var e1 = hot('-');
var e1subs = '^';
var expected = '-';

expectObservable(e1.distinctUntilChanged()).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should complete if source is empty', function () {
var e1 = Observable.empty();
var e1 = cold('|');
var e1subs = '(^!)';
var expected = '|';

expectObservable(e1.distinctUntilChanged()).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should complete if source does not emit', function () {
var e1 = hot('------|');
var e1subs = '^ !';
var expected = '------|';

expectObservable(e1.distinctUntilChanged()).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should emit if source emits single element only', function () {
var e1 = hot('--a--|');
var e1subs = '^ !';
var expected = '--a--|';

expectObservable(e1.distinctUntilChanged()).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should emit if source is scalar', function () {
Expand All @@ -61,59 +75,74 @@ describe('Observable.prototype.distinctUntilChanged()', function () {

it('should raises error if source raises error', function () {
var e1 = hot('--a--a--#');
var e1subs = '^ !';
var expected = '--a-----#';

expectObservable(e1.distinctUntilChanged()).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should raises error if source throws', function () {
var e1 = Observable.throw('error');
var e1 = cold('#');
var e1subs = '(^!)';
var expected = '#';

expectObservable(e1.distinctUntilChanged()).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should not omit if source elements are all different', function () {
var e1 = hot('--a--b--c--d--e--f--|');
var e1subs = '^ !';
var expected = '--a--b--c--d--e--f--|';

expectObservable(e1.distinctUntilChanged()).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should emit once if source elements are all same', function () {
var e1 = hot('--a--a--a--a--a--a--|');
var e1subs = '^ !';
var expected = '--a-----------------|';

expectObservable(e1.distinctUntilChanged()).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should emit once if comparer returns true always regardless of source emits', function () {
var e1 = hot('--a--b--c--d--e--f--|');
var e1subs = '^ !';
var expected = '--a-----------------|';

expectObservable(e1.distinctUntilChanged(function () { return true; })).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should emit all if comparer returns false always regardless of source emits', function () {
var e1 = hot('--a--a--a--a--a--a--|');
var e1subs = '^ !';
var expected = '--a--a--a--a--a--a--|';

expectObservable(e1.distinctUntilChanged(function () { return false; })).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should distinguish values by selector', function () {
var e1 = hot('--a--b--c--d--e--f--|', {a: 1, b: 2, c: 3, d: 4, e: 5, f: 6});
var e1subs = '^ !';
var expected = '--a-----c-----e-----|';
var selector = function (x, y) {
return y % 2 === 0;
};

expectObservable(e1.distinctUntilChanged(selector)).toBe(expected, {a: 1, c: 3, e: 5});
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should raises error when comparer throws', function () {
var e1 = hot('--a--b--c--d--e--f--|');
var expected = '--a--b--c--#';
var e1subs = '^ ! ';
var expected = '--a--b--c--# ';
var selector = function (x, y) {
if (y === 'd') {
throw 'error';
Expand All @@ -122,5 +151,6 @@ describe('Observable.prototype.distinctUntilChanged()', function () {
};

expectObservable(e1.distinctUntilChanged(selector)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
});

0 comments on commit 227c168

Please sign in to comment.