From 227c168075d7ea50be8c3d0325abaa5b4cd9e825 Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Wed, 25 Nov 2015 10:22:54 -0200 Subject: [PATCH] test(distinctUntilChanged): add subscription assertions for tests --- spec/operators/distinctUntilChanged-spec.js | 42 ++++++++++++++++++--- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/spec/operators/distinctUntilChanged-spec.js b/spec/operators/distinctUntilChanged-spec.js index 09a3e99c25..bd26753a41 100644 --- a/spec/operators/distinctUntilChanged-spec.js +++ b/spec/operators/distinctUntilChanged-spec.js @@ -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 () { @@ -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'; @@ -122,5 +151,6 @@ describe('Observable.prototype.distinctUntilChanged()', function () { }; expectObservable(e1.distinctUntilChanged(selector)).toBe(expected); + expectSubscriptions(e1.subscriptions).toBe(e1subs); }); }); \ No newline at end of file