Skip to content

Commit

Permalink
test(debounce): Add more subscription-related tests for debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
staltz authored and kwonoj committed Nov 13, 2015
1 parent e43be90 commit f392a27
Showing 1 changed file with 129 additions and 5 deletions.
134 changes: 129 additions & 5 deletions spec/operators/debounce-spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals describe, it, expect, expectObservable, hot, cold, rxTestScheduler */
/* globals describe, it, expect, expectObservable, expectSubscriptions, hot, cold, rxTestScheduler */
var Rx = require('../../dist/cjs/Rx');
var Observable = Rx.Observable;

Expand All @@ -11,146 +11,224 @@ describe('Observable.prototype.debounce()', function () {

it('should delay all element by selector observable', function () {
var e1 = hot('--a--b--c--d---------|');
var e1subs = '^ !';
var expected = '----a--b--c--d-------|';

expectObservable(e1.debounce(getTimerSelector(20))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should debounce by selector observable', function () {
var e1 = hot('--a--bc--d----|');
var e1subs = '^ !';
var expected = '----a---c--d--|';

expectObservable(e1.debounce(getTimerSelector(20))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

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

expectObservable(e1.debounce(getTimerSelector(20))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

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

expectObservable(e1.debounce(getTimerSelector(20))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should raise error when source does not emit and raises error', function () {
var e1 = hot('-----#');
var e1subs = '^ !';
var expected = '-----#';

expectObservable(e1.debounce(getTimerSelector(20))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

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

expectObservable(e1.debounce(getTimerSelector(20))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should debounce and does not complete when source does not completes', function () {
var e1 = hot('--a--bc--d---');
var expected = '----a---c--d--';
var e1subs = '^ ';
var expected = '----a---c--d-';

expectObservable(e1.debounce(getTimerSelector(20))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

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

expectObservable(e1.debounce(getTimerSelector(20))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

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

expectObservable(e1.debounce(getTimerSelector(20))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should delay all element until source raises error', function () {
var e1 = hot('--a--b--c--d---------#');
var e1subs = '^ !';
var expected = '----a--b--c--d-------#';

expectObservable(e1.debounce(getTimerSelector(20))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should debounce all elements while source emits by selector observable', function () {
var e1 = hot('---a---b---c---d---e|');
var e1subs = '^ !';
var expected = '--------------------(e|)';

expectObservable(e1.debounce(getTimerSelector(40))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should debounce all element while source emits by selector observable until raises error', function () {
var e1 = hot('--a--b--c--d-#');
var e1subs = '^ !';
var expected = '-------------#';

expectObservable(e1.debounce(getTimerSelector(50))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should delay element by same selector observable emits multiple', function () {
var e1 = hot('----a--b--c----d----e-------|');
var e1subs = '^ !';
var expected = '------a--b--c----d----e-----|';
var selector = cold('--x-y-');
var selectorSubs =
[' ^ ! ',
' ^ ! ',
' ^ ! ',
' ^ ! ',
' ^ ! '];

expectObservable(e1.debounce(function () { return selector; })).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectSubscriptions(selector.subscriptions).toBe(selectorSubs);
});

it('should debounce by selector observable emits multiple', function () {
var e1 = hot('----a--b--c----d----e-------|');
var e1subs = '^ !';
var expected = '------a-----c---------e-----|';
var selector = [cold('--x-y-'),
cold( '----x-y-'),
cold( '--x-y-'),
cold( '------x-y-'),
cold( '--x-y-')];
var selectorSubs =
[' ^ ! ',
' ^ ! ',
' ^ ! ',
' ^ ! ',
' ^ ! '];

expectObservable(e1.debounce(function () { return selector.shift(); })).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
for (var i = 0; i < selectorSubs.length; i++) {
expectSubscriptions(selector[i].subscriptions).toBe(selectorSubs[i]);
}
});

it('should debounce by selector observable until source completes', function () {
var e1 = hot('----a--b--c----d----e|');
var e1subs = '^ !';
var expected = '------a-----c--------(e|)';
var selector = [cold('--x-y-'),
cold( '----x-y-'),
cold( '--x-y-'),
cold( '------x-y-'),
cold( '--x-y-')];
var selectorSubs =
[' ^ ! ',
' ^ ! ',
' ^ ! ',
' ^ ! ',
' ^!'];

expectObservable(e1.debounce(function () { return selector.shift(); })).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
for (var i = 0; i < selectorSubs.length; i++) {
expectSubscriptions(selector[i].subscriptions).toBe(selectorSubs[i]);
}
});

it('should raise error when selector observable raises error', function () {
var e1 = hot('--------a--------b--------c---------|');
var e1subs = '^ !';
var expected = '---------a---------b---------#';
var selector = [cold( '-x-y-'),
cold( '--x-y-'),
cold( '---#')];
var selectorSubs =
[' ^! ',
' ^ ! ',
' ^ !'];

expectObservable(e1.debounce(function () { return selector.shift(); })).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
for (var i = 0; i < selectorSubs.length; i++) {
expectSubscriptions(selector[i].subscriptions).toBe(selectorSubs[i]);
}
});

it('should raise error when source raises error with selector observable', function () {
var e1 = hot('--------a--------b--------c---------d#');
var e1subs = '^ !';
var expected = '---------a---------b---------c-------#';
var selector = [cold( '-x-y-'),
cold( '--x-y-'),
cold( '---x-y-'),
cold( '----x-y-')];
var selectorSubs =
[' ^! ',
' ^ ! ',
' ^ ! ',
' ^!'];

expectObservable(e1.debounce(function () { return selector.shift(); })).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
for (var i = 0; i < selectorSubs.length; i++) {
expectSubscriptions(selector[i].subscriptions).toBe(selectorSubs[i]);
}
});

it('should raise error when selector function throws', function () {
var e1 = hot('--------a--------b--------c---------|');
var e1subs = '^ !';
var expected = '---------a---------b------#';
var selector = [cold( '-x-y-'),
cold( '--x-y-')];
var selectorSubs =
[' ^! ',
' ^ ! '];

function selectorFunction(x) {
if (x !== 'c') {
Expand All @@ -161,28 +239,74 @@ describe('Observable.prototype.debounce()', function () {
}

expectObservable(e1.debounce(selectorFunction)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
for (var i = 0; i < selectorSubs.length; i++) {
expectSubscriptions(selector[i].subscriptions).toBe(selectorSubs[i]);
}
});

it('should mirror the source when given an empty selector Observable', function () {
var e1 = hot('--------a-x-yz---bxy---z--c--x--y--z|');
var e1subs = '^ !';
var expected = '--------a-x-yz---bxy---z--c--x--y--z|';

function selectorFunction(x) { return Observable.empty(); }

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

it('should ignore all values except last, when given a never selector Observable', function () {
var e1 = hot('--------a-x-yz---bxy---z--c--x--y--z|');
var e1subs = '^ !';
var expected = '------------------------------------(z|)';

function selectorFunction(x) { return Observable.never(); }

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

it('should delay element by selector observable completes when it does not emits', function () {
var e1 = hot('--------a--------b--------c---------|');
var e1subs = '^ !';
var expected = '---------a---------b---------c------|';
var selector = [cold( '-|'),
cold( '--|'),
cold( '---|')];
var selectorSubs =
[' ^! ',
' ^ ! ',
' ^ ! '];

expectObservable(e1.debounce(function () { return selector.shift(); })).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
for (var i = 0; i < selectorSubs.length; i++) {
expectSubscriptions(selector[i].subscriptions).toBe(selectorSubs[i]);
}
});

it('should debounce by selector observable completes when it does not emits', function () {
var e1 = hot('----a--b-c---------de-------------|');
var e1subs = '^ !';
var expected = '-----a------c------------e--------|';
var selector = [cold('-|'),
cold( '--|'),
cold( '---|'),
cold( '----|'),
cold( '-----|')];
var selectorSubs =
[' ^! ',
' ^ ! ',
' ^ ! ',
' ^! ',
' ^ ! '];

expectObservable(e1.debounce(function () { return selector.shift(); })).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
for (var i = 0; i < selectorSubs.length; i++) {
expectSubscriptions(selector[i].subscriptions).toBe(selectorSubs[i]);
}
});

it('should delay by promise resolves', function (done) {
Expand Down

0 comments on commit f392a27

Please sign in to comment.