|
| 1 | +import {expect} from 'chai'; |
| 2 | +import * as Rx from '../../dist/cjs/Rx'; |
| 3 | +import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports |
| 4 | + |
| 5 | +declare const { asDiagram }; |
| 6 | +declare const hot: typeof marbleTestingSignature.hot; |
| 7 | +declare const cold: typeof marbleTestingSignature.cold; |
| 8 | +declare const expectObservable: typeof marbleTestingSignature.expectObservable; |
| 9 | +declare const expectSubscriptions: typeof marbleTestingSignature.expectSubscriptions; |
| 10 | + |
| 11 | +const Observable = Rx.Observable; |
| 12 | + |
| 13 | +/** @test {shareReplay} */ |
| 14 | +describe('Observable.prototype.shareReplay', () => { |
| 15 | + it('should mirror a simple source Observable', () => { |
| 16 | + const source = cold('--1-2---3-4--5-|'); |
| 17 | + const sourceSubs = '^ !'; |
| 18 | + const published = source.shareReplay(); |
| 19 | + const expected = '--1-2---3-4--5-|'; |
| 20 | + |
| 21 | + expectObservable(published).toBe(expected); |
| 22 | + expectSubscriptions(source.subscriptions).toBe(sourceSubs); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should do nothing if result is not subscribed', () => { |
| 26 | + let subscribed = false; |
| 27 | + const source = new Observable(() => { |
| 28 | + subscribed = true; |
| 29 | + }); |
| 30 | + source.shareReplay(); |
| 31 | + expect(subscribed).to.be.false; |
| 32 | + }); |
| 33 | + |
| 34 | + it('should multicast the same values to multiple observers, bufferSize=1', () => { |
| 35 | + const source = cold('-1-2-3----4-|'); const shared = source.shareReplay(1); |
| 36 | + const sourceSubs = '^ !'; |
| 37 | + const subscriber1 = hot('a| ').mergeMapTo(shared); |
| 38 | + const expected1 = '-1-2-3----4-|'; |
| 39 | + const subscriber2 = hot(' b| ').mergeMapTo(shared); |
| 40 | + const expected2 = ' 23----4-|'; |
| 41 | + const subscriber3 = hot(' c| ').mergeMapTo(shared); |
| 42 | + const expected3 = ' 3-4-|'; |
| 43 | + |
| 44 | + expectObservable(subscriber1).toBe(expected1); |
| 45 | + expectObservable(subscriber2).toBe(expected2); |
| 46 | + expectObservable(subscriber3).toBe(expected3); |
| 47 | + expectSubscriptions(source.subscriptions).toBe(sourceSubs); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should multicast the same values to multiple observers, bufferSize=2', () => { |
| 51 | + const source = cold('-1-2-----3------4-|'); const shared = source.shareReplay(2); |
| 52 | + const sourceSubs = '^ !'; |
| 53 | + const subscriber1 = hot('a| ').mergeMapTo(shared); |
| 54 | + const expected1 = '-1-2-----3------4-|'; |
| 55 | + const subscriber2 = hot(' b| ').mergeMapTo(shared); |
| 56 | + const expected2 = ' (12)-3------4-|'; |
| 57 | + const subscriber3 = hot(' c| ').mergeMapTo(shared); |
| 58 | + const expected3 = ' (23)-4-|'; |
| 59 | + |
| 60 | + expectObservable(subscriber1).toBe(expected1); |
| 61 | + expectObservable(subscriber2).toBe(expected2); |
| 62 | + expectObservable(subscriber3).toBe(expected3); |
| 63 | + expectSubscriptions(source.subscriptions).toBe(sourceSubs); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should multicast an error from the source to multiple observers', () => { |
| 67 | + const source = cold('-1-2-3----4-#'); const shared = source.shareReplay(1); |
| 68 | + const sourceSubs = '^ !'; |
| 69 | + const subscriber1 = hot('a| ').mergeMapTo(shared); |
| 70 | + const expected1 = '-1-2-3----4-#'; |
| 71 | + const subscriber2 = hot(' b| ').mergeMapTo(shared); |
| 72 | + const expected2 = ' 23----4-#'; |
| 73 | + const subscriber3 = hot(' c| ').mergeMapTo(shared); |
| 74 | + const expected3 = ' 3-4-#'; |
| 75 | + |
| 76 | + expectObservable(subscriber1).toBe(expected1); |
| 77 | + expectObservable(subscriber2).toBe(expected2); |
| 78 | + expectObservable(subscriber3).toBe(expected3); |
| 79 | + expectSubscriptions(source.subscriptions).toBe(sourceSubs); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should multicast an empty source', () => { |
| 83 | + const source = cold('|'); |
| 84 | + const sourceSubs = '(^!)'; |
| 85 | + const shared = source.shareReplay(1); |
| 86 | + const expected = '|'; |
| 87 | + |
| 88 | + expectObservable(shared).toBe(expected); |
| 89 | + expectSubscriptions(source.subscriptions).toBe(sourceSubs); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should multicast a never source', () => { |
| 93 | + const source = cold('-'); |
| 94 | + const sourceSubs = '^'; |
| 95 | + |
| 96 | + const shared = source.shareReplay(1); |
| 97 | + const expected = '-'; |
| 98 | + |
| 99 | + expectObservable(shared).toBe(expected); |
| 100 | + expectSubscriptions(source.subscriptions).toBe(sourceSubs); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should multicast a throw source', () => { |
| 104 | + const source = cold('#'); |
| 105 | + const sourceSubs = '(^!)'; |
| 106 | + const shared = source.shareReplay(1); |
| 107 | + const expected = '#'; |
| 108 | + |
| 109 | + expectObservable(shared).toBe(expected); |
| 110 | + expectSubscriptions(source.subscriptions).toBe(sourceSubs); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should replay results to subsequent subscriptions if source completes, bufferSize=2', () => { |
| 114 | + const source = cold('-1-2-----3-| '); |
| 115 | + const shared = source.shareReplay(2); |
| 116 | + const sourceSubs = '^ ! '; |
| 117 | + const subscriber1 = hot('a| ').mergeMapTo(shared); |
| 118 | + const expected1 = '-1-2-----3-| '; |
| 119 | + const subscriber2 = hot(' b| ').mergeMapTo(shared); |
| 120 | + const expected2 = ' (12)-3-| '; |
| 121 | + const subscriber3 = hot(' (c|) ').mergeMapTo(shared); |
| 122 | + const expected3 = ' (23|)'; |
| 123 | + |
| 124 | + expectObservable(subscriber1).toBe(expected1); |
| 125 | + expectObservable(subscriber2).toBe(expected2); |
| 126 | + expectObservable(subscriber3).toBe(expected3); |
| 127 | + expectSubscriptions(source.subscriptions).toBe(sourceSubs); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should completely restart for subsequent subscriptions if source errors, bufferSize=2', () => { |
| 131 | + const source = cold('-1-2-----3-# '); |
| 132 | + const shared = source.shareReplay(2); |
| 133 | + const sourceSubs1 = '^ ! '; |
| 134 | + const subscriber1 = hot('a| ').mergeMapTo(shared); |
| 135 | + const expected1 = '-1-2-----3-# '; |
| 136 | + const subscriber2 = hot(' b| ').mergeMapTo(shared); |
| 137 | + const expected2 = ' (12)-3-# '; |
| 138 | + const subscriber3 = hot(' (c|) ').mergeMapTo(shared); |
| 139 | + const expected3 = ' -1-2-----3-#'; |
| 140 | + const sourceSubs2 = ' ^ !'; |
| 141 | + |
| 142 | + expectObservable(subscriber1).toBe(expected1); |
| 143 | + expectObservable(subscriber2).toBe(expected2); |
| 144 | + expectObservable(subscriber3).toBe(expected3); |
| 145 | + expectSubscriptions(source.subscriptions).toBe([sourceSubs1, sourceSubs2]); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should be retryable, bufferSize=2', () => { |
| 149 | + const subs = []; |
| 150 | + const source = cold('-1-2-----3-# '); |
| 151 | + const shared = source.shareReplay(2).retry(1); |
| 152 | + subs.push( '^ ! '); |
| 153 | + subs.push( ' ^ ! '); |
| 154 | + subs.push( ' ^ !'); |
| 155 | + const subscriber1 = hot('a| ').mergeMapTo(shared); |
| 156 | + const expected1 = '-1-2-----3--1-2-----3-# '; |
| 157 | + const subscriber2 = hot(' b| ').mergeMapTo(shared); |
| 158 | + const expected2 = ' (12)-3--1-2-----3-# '; |
| 159 | + const subscriber3 = hot(' (c|) ').mergeMapTo(shared); |
| 160 | + const expected3 = ' (12)-3--1-2-----3-#'; |
| 161 | + |
| 162 | + expectObservable(subscriber1).toBe(expected1); |
| 163 | + expectObservable(subscriber2).toBe(expected2); |
| 164 | + expectObservable(subscriber3).toBe(expected3); |
| 165 | + expectSubscriptions(source.subscriptions).toBe(subs); |
| 166 | + }); |
| 167 | +}); |
0 commit comments