|
| 1 | +/* globals describe, expect, it, hot, cold, expectObservable */ |
| 2 | + |
| 3 | +var Rx = require('../../dist/cjs/Rx'); |
| 4 | +var Observable = Rx.Observable; |
| 5 | + |
| 6 | +describe('Observable.prototype.shareReplay()', function () { |
| 7 | + it('should share a single subscription', function () { |
| 8 | + var subscriptionCount = 0; |
| 9 | + var obs = new Observable(function (observer) { |
| 10 | + subscriptionCount++; |
| 11 | + }); |
| 12 | + |
| 13 | + var source = obs.shareReplay(1); |
| 14 | + |
| 15 | + expect(subscriptionCount).toBe(0); |
| 16 | + |
| 17 | + source.subscribe(); |
| 18 | + source.subscribe(); |
| 19 | + |
| 20 | + expect(subscriptionCount).toBe(1); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should replay as many events as specified by the bufferSize', function (done) { |
| 24 | + var results1 = []; |
| 25 | + var results2 = []; |
| 26 | + var subscriptions = 0; |
| 27 | + |
| 28 | + var source = new Observable(function (observer) { |
| 29 | + subscriptions++; |
| 30 | + observer.next(1); |
| 31 | + observer.next(2); |
| 32 | + observer.next(3); |
| 33 | + observer.next(4); |
| 34 | + }); |
| 35 | + |
| 36 | + var hot = source.shareReplay(2); |
| 37 | + |
| 38 | + expect(results1).toEqual([]); |
| 39 | + expect(results2).toEqual([]); |
| 40 | + |
| 41 | + hot.subscribe(function (x) { |
| 42 | + results1.push(x); |
| 43 | + }); |
| 44 | + |
| 45 | + expect(results1).toEqual([1, 2, 3, 4]); |
| 46 | + expect(results2).toEqual([]); |
| 47 | + |
| 48 | + hot.subscribe(function (x) { |
| 49 | + results2.push(x); |
| 50 | + }); |
| 51 | + |
| 52 | + expect(results1).toEqual([1, 2, 3, 4]); |
| 53 | + expect(results2).toEqual([3, 4]); |
| 54 | + expect(subscriptions).toBe(1); |
| 55 | + done(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should not change the output of the observable when successful', function () { |
| 59 | + var e1 = hot('---a--^--b-c--d--e--|'); |
| 60 | + var expected = '---b-c--d--e--|'; |
| 61 | + |
| 62 | + expectObservable(e1.shareReplay(1)).toBe(expected); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should not change the output of the observable when error', function () { |
| 66 | + var e1 = hot('---a--^--b-c--d--e--#'); |
| 67 | + var expected = '---b-c--d--e--#'; |
| 68 | + |
| 69 | + expectObservable(e1.shareReplay(1)).toBe(expected); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should not change the output of the observable when never', function () { |
| 73 | + var e1 = Observable.never(); |
| 74 | + var expected = '-'; |
| 75 | + |
| 76 | + expectObservable(e1.shareReplay(1)).toBe(expected); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should not change the output of the observable when empty', function () { |
| 80 | + var e1 = Observable.empty(); |
| 81 | + var expected = '|'; |
| 82 | + |
| 83 | + expectObservable(e1.shareReplay(1)).toBe(expected); |
| 84 | + }); |
| 85 | +}); |
0 commit comments