UNPKG

@reactivex/rxjs

Version:

Reactive Extensions for modern JavaScript

443 lines (433 loc) 27.1 kB
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <base data-ice="baseUrl" href="../../../"> <title data-ice="title">spec-js/operators/bufferToggle-spec.js | RxJS API Document</title> <link type="text/css" rel="stylesheet" href="css/style.css"> <link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css"> <script src="script/prettify/prettify.js"></script> <script src="script/manual.js"></script> <script data-ice="userScript" src="user/script/0-Rx.js"></script> <script data-ice="userScript" src="user/script/1-devtools-welcome.js"></script> <script data-ice="userScript" src="user/script/2-custom-manual-styles.js"></script> <script data-ice="userScript" src="user/script/3-decision-tree-widget.min.js"></script> <script data-ice="userScript" src="user/script/4-theme-toggler.js"></script> <link data-ice="userStyle" rel="stylesheet" href="user/css/0-main.css"> </head> <body class="layout-container" data-ice="rootContainer"> <header> <a href="./">Home</a> <a href="./manual/index.html" data-ice="manualHeaderLink">Manual</a> <a href="identifiers.html">Reference</a> <a href="source.html">Source</a> <a href="test.html" data-ice="testLink">Test</a> <a data-ice="repoURL" href="https://github.com/ReactiveX/RxJS" class="repo-url-github">Repository</a> <div class="search-box"> <span> <img src="./image/search.png"> <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span> </span> <ul class="search-result"></ul> </div> </header> <nav class="navigation" data-ice="nav"><div> <ul> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-AsyncSubject">AsyncSubject</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-BehaviorSubject">BehaviorSubject</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Notification">Notification</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Observable">Observable</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-ReplaySubject">ReplaySubject</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Scheduler">Scheduler</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-AnonymousSubject">AnonymousSubject</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Subject">Subject</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-SubjectSubscriber">SubjectSubscriber</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Subscriber">Subscriber</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Subscription">Subscription</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-Rx.Scheduler">Rx.Scheduler</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-Rx.Symbol">Rx.Symbol</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">observable</div><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-ConnectableObservable">ConnectableObservable</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">observable/dom</div><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-AjaxError">AjaxError</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-AjaxResponse">AjaxResponse</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-AjaxTimeoutError">AjaxTimeoutError</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">operator</div><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-GroupedObservable">GroupedObservable</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">scheduler</div><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Action">Action</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">util</div><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-ArgumentOutOfRangeError">ArgumentOutOfRangeError</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-EmptyError">EmptyError</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-ObjectUnsubscribedError">ObjectUnsubscribedError</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-TimeoutError">TimeoutError</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-UnsubscriptionError">UnsubscriptionError</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-root">root</a></span></span></li> </ul> </div> </nav> <div class="content" data-ice="content"><h1 data-ice="title">spec-js/operators/bufferToggle-spec.js</h1> <pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">&quot;use strict&quot;; var chai_1 = require(&apos;chai&apos;); var Rx = require(&apos;../../dist/cjs/Rx&apos;); var Observable = Rx.Observable; /** @test {bufferToggle} */ describe(&apos;Observable.prototype.bufferToggle&apos;, function () { asDiagram(&apos;bufferToggle&apos;)(&apos;should emit buffers using hot openings and hot closings&apos;, function () { var e1 = hot(&apos;---a---b---c---d---e---f---g---|&apos;); var e2 = hot(&apos;--o------------------o---------|&apos;); var e3 = hot(&apos;---------c---------------c-----|&apos;); var expected = &apos;---------x---------------y-----|&apos;; var values = { x: [&apos;a&apos;, &apos;b&apos;], y: [&apos;f&apos;], }; var result = e1.bufferToggle(e2, function (x) { return e3; }); expectObservable(result).toBe(expected, values); }); it(&apos;should emit buffers that are opened by an observable from the first argument &apos; + &apos;and closed by an observable returned by the function in the second argument&apos;, function () { var e1 = hot(&apos;-----a----b----c----d----e----f----g----h----i----|&apos;); var e2 = cold(&apos;-------------x-------------y--------------z-------|&apos;); var e3 = cold(&apos;---------------(j|)&apos;); // ---------------(j|) // ---------------(j|) var expected = &apos;----------------------------q-------------r-------(s|)&apos;; var values = { q: [&apos;c&apos;, &apos;d&apos;, &apos;e&apos;], r: [&apos;f&apos;, &apos;g&apos;, &apos;h&apos;], s: [&apos;i&apos;] }; var innerVals = [&apos;x&apos;, &apos;y&apos;, &apos;z&apos;]; expectObservable(e1.bufferToggle(e2, function (x) { chai_1.expect(x).to.equal(innerVals.shift()); return e3; })).toBe(expected, values); }); it(&apos;should emit buffers using constying cold closings&apos;, function () { var e1 = hot(&apos;--a--^---b---c---d---e---f---g---h------| &apos;); var e2 = cold(&apos;--x-----------y--------z---| &apos;); var subs = &apos;^ ! &apos;; var closings = [ cold(&apos;---------------s--| &apos;), cold(&apos;----(s|) &apos;), cold(&apos;---------------(s|)&apos;)]; var closeSubs = [&apos; ^ ! &apos;, &apos; ^ ! &apos;, &apos; ^ ! &apos;]; var expected = &apos;-----------------ij----------------(k|) &apos;; var values = { i: [&apos;b&apos;, &apos;c&apos;, &apos;d&apos;, &apos;e&apos;], j: [&apos;e&apos;], k: [&apos;g&apos;, &apos;h&apos;] }; var i = 0; var result = e1.bufferToggle(e2, function () { return closings[i++]; }); expectObservable(result).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(subs); expectSubscriptions(closings[0].subscriptions).toBe(closeSubs[0]); expectSubscriptions(closings[1].subscriptions).toBe(closeSubs[1]); expectSubscriptions(closings[2].subscriptions).toBe(closeSubs[2]); }); it(&apos;should emit buffers using constying hot closings&apos;, function () { var e1 = hot(&apos;--a--^---b---c---d---e---f---g---h------| &apos;); var e2 = cold(&apos;--x-----------y--------z---| &apos;); var subs = &apos;^ ! &apos;; var closings = [ { obs: hot(&apos;-1--^----------------s-| &apos;), sub: &apos; ^ ! &apos; }, { obs: hot(&apos;-----3----4-------(s|) &apos;), sub: &apos; ^ ! &apos; }, { obs: hot(&apos;-------3----4-------5----------------s|&apos;), sub: &apos; ^ ! &apos; }]; // eslint-disable-line key-spacing var expected = &apos;-----------------ij----------------(k|)&apos;; var values = { i: [&apos;b&apos;, &apos;c&apos;, &apos;d&apos;, &apos;e&apos;], j: [&apos;e&apos;], k: [&apos;g&apos;, &apos;h&apos;] }; var i = 0; var result = e1.bufferToggle(e2, function () { return closings[i++].obs; }); expectObservable(result).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(subs); for (var j = 0; j &lt; closings.length; j++) { expectSubscriptions(closings[j].obs.subscriptions).toBe(closings[j].sub); } }); it(&apos;should emit buffers using constying empty delayed closings&apos;, function () { var e1 = hot(&apos;--a--^---b---c---d---e---f---g---h------| &apos;); var e2 = cold(&apos;--x-----------y--------z---| &apos;); var subs = &apos;^ ! &apos;; var closings = [ cold(&apos;---------------| &apos;), cold(&apos;----| &apos;), cold(&apos;---------------|&apos;)]; var expected = &apos;-----------------ij----------------(k|)&apos;; var values = { i: [&apos;b&apos;, &apos;c&apos;, &apos;d&apos;, &apos;e&apos;], j: [&apos;e&apos;], k: [&apos;g&apos;, &apos;h&apos;] }; var i = 0; var result = e1.bufferToggle(e2, function () { return closings[i++]; }); expectObservable(result).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(subs); }); it(&apos;should emit buffers using constying cold closings, outer unsubscribed early&apos;, function () { var e1 = hot(&apos;--a--^---b---c---d---e---f---g---h------| &apos;); var subs = &apos;^ ! &apos;; var e2 = cold(&apos;--x-----------y--------z---| &apos;); var closings = [ cold(&apos;---------------s--| &apos;), cold(&apos;----(s|) &apos;), cold(&apos;---------------(s|)&apos;)]; var csub0 = &apos; ^ ! &apos;; var expected = &apos;----------- &apos;; var unsub = &apos; ! &apos;; var values = { i: [&apos;b&apos;, &apos;c&apos;, &apos;d&apos;, &apos;e&apos;] }; var i = 0; var result = e1.bufferToggle(e2, function () { return closings[i++]; }); expectObservable(result, unsub).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(subs); expectSubscriptions(closings[0].subscriptions).toBe(csub0); expectSubscriptions(closings[1].subscriptions).toBe([]); expectSubscriptions(closings[2].subscriptions).toBe([]); }); it(&apos;should not break unsubscription chains when result is unsubscribed explicitly&apos;, function () { var e1 = hot(&apos;--a--^---b---c---d---e---f---g---h------| &apos;); var subs = &apos;^ ! &apos;; var e2 = cold(&apos;--x-----------y--------z---| &apos;); var closings = [ cold(&apos;---------------s--| &apos;), cold(&apos;----(s|) &apos;), cold(&apos;---------------(s|)&apos;)]; var expected = &apos;-----------------i- &apos;; var unsub = &apos; ! &apos;; var values = { i: [&apos;b&apos;, &apos;c&apos;, &apos;d&apos;, &apos;e&apos;] }; var i = 0; var result = e1 .mergeMap(function (x) { return Observable.of(x); }) .bufferToggle(e2, function () { return closings[i++]; }) .mergeMap(function (x) { return Observable.of(x); }); expectObservable(result, unsub).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(subs); }); it(&apos;should propagate error thrown from closingSelector&apos;, function () { var e1 = hot(&apos;--a--^---b---c---d---e---f---g---h------| &apos;); var e2 = cold(&apos;--x-----------y--------z---| &apos;); var subs = &apos;^ ! &apos;; var closings = [ cold(&apos;---------------s--| &apos;), cold(&apos;----(s|) &apos;), cold(&apos;---------------(s|)&apos;)]; var closeSubs0 = &apos; ^ ! &apos;; var expected = &apos;--------------# &apos;; var i = 0; var result = e1.bufferToggle(e2, function () { if (i === 1) { throw &apos;error&apos;; } return closings[i++]; }); expectObservable(result).toBe(expected); expectSubscriptions(e1.subscriptions).toBe(subs); expectSubscriptions(closings[0].subscriptions).toBe(closeSubs0); expectSubscriptions(closings[1].subscriptions).toBe([]); expectSubscriptions(closings[2].subscriptions).toBe([]); }); it(&apos;should propagate error emitted from a closing&apos;, function () { var e1 = hot(&apos;--a--^---b---c---d---e---f---g---h------|&apos;); var e2 = cold(&apos;--x-----------y--------z---| &apos;); var subs = &apos;^ ! &apos;; var closings = [ cold(&apos;---------------s--| &apos;), cold(&apos;# &apos;)]; var closeSubs = [&apos; ^ ! &apos;, &apos; (^!) &apos;]; var expected = &apos;--------------# &apos;; var i = 0; var result = e1.bufferToggle(e2, function () { return closings[i++]; }); expectObservable(result).toBe(expected); expectSubscriptions(e1.subscriptions).toBe(subs); expectSubscriptions(closings[0].subscriptions).toBe(closeSubs[0]); expectSubscriptions(closings[1].subscriptions).toBe(closeSubs[1]); }); it(&apos;should propagate error emitted late from a closing&apos;, function () { var e1 = hot(&apos;--a--^---b---c---d---e---f---g---h------|&apos;); var e2 = cold(&apos;--x-----------y--------z---| &apos;); var subs = &apos;^ ! &apos;; var closings = [ cold(&apos;---------------s--| &apos;), cold(&apos;-----# &apos;)]; var closeSubs = [&apos; ^ ! &apos;, &apos; ^ ! &apos;]; var expected = &apos;-----------------i-# &apos;; var values = { i: [&apos;b&apos;, &apos;c&apos;, &apos;d&apos;, &apos;e&apos;] }; var i = 0; var result = e1.bufferToggle(e2, function () { return closings[i++]; }); expectObservable(result).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(subs); expectSubscriptions(closings[0].subscriptions).toBe(closeSubs[0]); expectSubscriptions(closings[1].subscriptions).toBe(closeSubs[1]); }); it(&apos;should handle errors&apos;, function () { var e1 = hot(&apos;--a--^---b---c---d---e--# &apos;); var e2 = cold(&apos;--x-----------y--------z---|&apos;); var subs = &apos;^ ! &apos;; var closings = [ cold(&apos;---------------s--| &apos;), cold(&apos;-------s| &apos;)]; var closeSubs = [&apos; ^ ! &apos;, &apos; ^ ! &apos;]; var expected = &apos;-----------------i-# &apos;; var values = { i: [&apos;b&apos;, &apos;c&apos;, &apos;d&apos;, &apos;e&apos;] }; var i = 0; var result = e1.bufferToggle(e2, function () { return closings[i++]; }); expectObservable(result).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(subs); expectSubscriptions(closings[0].subscriptions).toBe(closeSubs[0]); expectSubscriptions(closings[1].subscriptions).toBe(closeSubs[1]); }); it(&apos;should handle empty source&apos;, function () { var e1 = cold(&apos;|&apos;); var e2 = cold(&apos;--o-----|&apos;); var e3 = cold(&apos;-----c--|&apos;); var expected = &apos;|&apos;; var values = { x: [] }; var result = e1.bufferToggle(e2, function () { return e3; }); expectObservable(result).toBe(expected, values); }); it(&apos;should handle throw&apos;, function () { var e1 = cold(&apos;#&apos;); var e2 = cold(&apos;--o-----|&apos;); var e3 = cold(&apos;-----c--|&apos;); var expected = &apos;#&apos;; var values = { x: [] }; var result = e1.bufferToggle(e2, function () { return e3; }); expectObservable(result).toBe(expected, values); }); it(&apos;should handle never&apos;, function () { var e1 = hot(&apos;-&apos;); var e2 = cold(&apos;--o-----o------o-----o---o-----|&apos;); var e3 = cold(&apos;--c-|&apos;); var unsub = &apos; !&apos;; var subs = &apos;^ !&apos;; var expected = &apos;----x-----x------x-----x---x-----------------&apos;; var values = { x: [] }; var result = e1.bufferToggle(e2, function () { return e3; }); expectObservable(result, unsub).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(subs); }); it(&apos;should handle a never opening Observable&apos;, function () { var e1 = hot(&apos;--a--^---b---c---d---e---f---g---h------|&apos;); var e2 = cold(&apos;-&apos;); var e3 = cold(&apos;--c-|&apos;); var expected = &apos;-----------------------------------|&apos;; var result = e1.bufferToggle(e2, function () { return e3; }); expectObservable(result).toBe(expected); }); it(&apos;should handle a never closing Observable&apos;, function () { var e1 = hot(&apos;--a--^---b---c---d---e---f---g---h------|&apos;); var e2 = cold(&apos;---o---------------o-----------|&apos;); var e3 = cold(&apos;-&apos;); var expected = &apos;-----------------------------------(xy|)&apos;; var values = { x: [&apos;b&apos;, &apos;c&apos;, &apos;d&apos;, &apos;e&apos;, &apos;f&apos;, &apos;g&apos;, &apos;h&apos;], y: [&apos;f&apos;, &apos;g&apos;, &apos;h&apos;] }; var result = e1.bufferToggle(e2, function () { return e3; }); expectObservable(result).toBe(expected, values); }); it(&apos;should handle opening Observable that just throws&apos;, function () { var e1 = hot(&apos;--a--^---b---c---d---e---f---g---h------|&apos;); var e1subs = &apos;(^!)&apos;; var e2 = cold(&apos;#&apos;); var e2subs = &apos;(^!)&apos;; var e3 = cold(&apos;--c-|&apos;); var expected = &apos;#&apos;; var result = e1.bufferToggle(e2, function () { return e3; }); expectObservable(result).toBe(expected); expectSubscriptions(e1.subscriptions).toBe(e1subs); expectSubscriptions(e2.subscriptions).toBe(e2subs); }); it(&apos;should accept openings resolved promise&apos;, function (done) { var e1 = Observable.concat(Observable.timer(10).mapTo(1), Observable.timer(100).mapTo(2), Observable.timer(150).mapTo(3), Observable.timer(200).mapTo(4)); var expected = [[1]]; e1.bufferToggle(new Promise(function (resolve) { resolve(42); }), function () { return Observable.timer(50); }).subscribe(function (x) { chai_1.expect(x).to.deep.equal(expected.shift()); }, function (x) { done(new Error(&apos;should not be called&apos;)); }, function () { chai_1.expect(expected.length).to.be.equal(0); done(); }); }); it(&apos;should accept openings rejected promise&apos;, function (done) { var e1 = Observable.concat(Observable.of(1), Observable.timer(10).mapTo(2), Observable.timer(10).mapTo(3), Observable.timer(100).mapTo(4)); var expected = 42; e1.bufferToggle(new Promise(function (resolve, reject) { reject(expected); }), function () { return Observable.timer(50); }).subscribe(function (x) { done(new Error(&apos;should not be called&apos;)); }, function (x) { chai_1.expect(x).to.equal(expected); done(); }, function () { done(new Error(&apos;should not be called&apos;)); }); }); it(&apos;should accept closing selector that returns a resolved promise&apos;, function (done) { var e1 = Observable.concat(Observable.of(1), Observable.timer(10).mapTo(2), Observable.timer(10).mapTo(3), Observable.timer(100).mapTo(4)); var expected = [[1]]; e1.bufferToggle(Observable.of(10), function () { return new Promise(function (resolve) { resolve(42); }); }) .subscribe(function (x) { chai_1.expect(x).to.deep.equal(expected.shift()); }, function () { done(new Error(&apos;should not be called&apos;)); }, function () { chai_1.expect(expected.length).to.be.equal(0); done(); }); }); it(&apos;should accept closing selector that returns a rejected promise&apos;, function (done) { var e1 = Observable.concat(Observable.of(1), Observable.timer(10).mapTo(2), Observable.timer(10).mapTo(3), Observable.timer(100).mapTo(4)); var expected = 42; e1.bufferToggle(Observable.of(10), function () { return new Promise(function (resolve, reject) { reject(expected); }); }) .subscribe(function (x) { done(new Error(&apos;should not be called&apos;)); }, function (x) { chai_1.expect(x).to.equal(expected); done(); }, function () { done(new Error(&apos;should not be called&apos;)); }); }); it(&apos;should handle empty closing observable&apos;, function () { var e1 = hot(&apos;--a--^---b---c---d---e---f---g---h------|&apos;); var subs = &apos;^ !&apos;; var e2 = cold(&apos;--x-----------y--------z---| &apos;); var expected = &apos;--l-----------m--------n-----------|&apos;; var result = e1.bufferToggle(e2, function () { return Observable.empty(); }); expectObservable(result).toBe(expected, { l: [], m: [], n: [] }); expectSubscriptions(e1.subscriptions).toBe(subs); }); }); //# sourceMappingURL=bufferToggle-spec.js.map</code></pre> </div> <footer class="footer"> Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(0.4.8)</span></a> </footer> <script src="script/search_index.js"></script> <script src="script/search.js"></script> <script src="script/pretty-print.js"></script> <script src="script/inherited-summary.js"></script> <script src="script/test-summary.js"></script> <script src="script/inner-link.js"></script> <script src="script/patch-for-local.js"></script> </body> </html>