-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(throttleTime): add tests for throttleTime operator
Partially resolves issue #666.
- Loading branch information
Showing
2 changed files
with
114 additions
and
25 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* globals describe, it, expect, expectObservable, expectSubscription, hot, cold */ | ||
var Rx = require('../../dist/cjs/Rx'); | ||
var Observable = Rx.Observable; | ||
var Scheduler = Rx.Scheduler; | ||
|
||
describe('Observable.prototype.throttleTime()', function () { | ||
it('should throttle events by 50 time units', function (done) { | ||
Observable.of(1, 2, 3).throttleTime(50) | ||
.subscribe(function (x) { | ||
expect(x).toBe(1); | ||
}, null, done); | ||
}); | ||
|
||
it('should throttle events multiple times', function (done) { | ||
var expected = ['1-0', '2-0']; | ||
Observable.concat( | ||
Observable.timer(0, 10).take(3).map(function (x) { return '1-' + x; }), | ||
Observable.timer(80, 10).take(5).map(function (x) { return '2-' + x; }) | ||
) | ||
.throttleTime(50) | ||
.subscribe(function (x) { | ||
expect(x).toBe(expected.shift()); | ||
}, null, done); | ||
}); | ||
|
||
it('should simply mirror the source if values are not emitted often enough', function () { | ||
var e1 = hot('-a--------b-----c----|'); | ||
var subs = '^ !'; | ||
var expected = '-a--------b-----c----|'; | ||
|
||
expectObservable(e1.throttleTime(50, rxTestScheduler)).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(subs); | ||
}); | ||
|
||
it('should immediately emit the first value in each time window', function () { | ||
var e1 = hot('-a-x-y----b---x-cx---|'); | ||
var subs = '^ !'; | ||
var expected = '-a--------b-----c----|'; | ||
|
||
expectObservable(e1.throttleTime(50, rxTestScheduler)).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(subs); | ||
}); | ||
|
||
it('should handle a busy producer emitting a regular repeating sequence', function () { | ||
var e1 = hot('abcdefabcdefabcdefabcdefa|'); | ||
var subs = '^ !'; | ||
var expected = 'a-----a-----a-----a-----a|'; | ||
|
||
expectObservable(e1.throttleTime(50, rxTestScheduler)).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(subs); | ||
}); | ||
|
||
it('should complete when source does not emit', function () { | ||
var e1 = hot('-----|'); | ||
var subs = '^ !'; | ||
var expected = '-----|'; | ||
|
||
expectObservable(e1.throttleTime(50, rxTestScheduler)).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(subs); | ||
}); | ||
|
||
it('should raise error when source does not emit and raises error', function () { | ||
var e1 = hot('-----#'); | ||
var expected = '-----#'; | ||
|
||
expectObservable(e1.throttleTime(10, rxTestScheduler)).toBe(expected); | ||
}); | ||
|
||
it('should handle an empty source', function () { | ||
var e1 = cold('|'); | ||
var subs = '(^!)'; | ||
var expected = '|'; | ||
|
||
expectObservable(e1.throttleTime(30, rxTestScheduler)).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(subs); | ||
}); | ||
|
||
it('should handle a never source', function () { | ||
var e1 = cold('-'); | ||
var subs = '^'; | ||
var expected = '-'; | ||
|
||
expectObservable(e1.throttleTime(30, rxTestScheduler)).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(subs); | ||
}); | ||
|
||
it('should handle a throw source', function () { | ||
var e1 = cold('#'); | ||
var subs = '(^!)'; | ||
var expected = '#'; | ||
|
||
expectObservable(e1.throttleTime(30, rxTestScheduler)).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(subs); | ||
}); | ||
|
||
it('should throttle and does not complete when source does not completes', function () { | ||
var e1 = hot('-a--(bc)-------d----------------'); | ||
var unsub = ' !'; | ||
var subs = '^ !'; | ||
var expected = '-a-------------d----------------'; | ||
|
||
expectObservable(e1.throttleTime(50, rxTestScheduler), unsub).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(subs); | ||
}); | ||
|
||
it('should throttle values until source raises error', function () { | ||
var e1 = hot('-a--(bc)-------d---------------#'); | ||
var subs = '^ !'; | ||
var expected = '-a-------------d---------------#'; | ||
|
||
expectObservable(e1.throttleTime(50, rxTestScheduler)).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(subs); | ||
}); | ||
}); |