|
| 1 | +import { expect } from 'chai'; |
| 2 | +import 'rxjs/add/operator/mapTo'; |
| 3 | +import 'rxjs/add/operator/windowCount'; |
| 4 | +import { ColdObservable } from 'rxjs/testing/ColdObservable'; |
| 5 | +import { HotObservable } from 'rxjs/testing/HotObservable'; |
| 6 | +import { SubscriptionLog } from 'rxjs/testing/SubscriptionLog'; |
| 7 | +import { TestMessage } from '../../src/index'; |
| 8 | +import { parseObservableMarble } from '../../src/marbles/parseObservableMarble'; |
| 9 | +import { complete, error, next } from '../../src/message/TestMessageValue'; |
| 10 | +import { TestScheduler } from '../../src/scheduler/TestScheduler'; |
| 11 | + |
| 12 | +describe('TestScheduler', () => { |
| 13 | + describe('hotObservable', () => { |
| 14 | + it('should create hot observable via TestMessage', () => { |
| 15 | + const scheduler = new TestScheduler(); |
| 16 | + const messages: Array<TestMessage<string>> = [next(10, 'meh'), complete(20)]; |
| 17 | + |
| 18 | + const value = scheduler.createHotObservable(messages); |
| 19 | + |
| 20 | + expect(value).to.be.instanceOf(HotObservable); |
| 21 | + expect(value.messages).to.deep.equal(messages); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should create hot observable via marble diagram', () => { |
| 25 | + const scheduler = new TestScheduler(); |
| 26 | + const value = scheduler.createHotObservable('---a---|'); |
| 27 | + |
| 28 | + const expected: Array<TestMessage<string>> = [next(3, 'a'), complete(7)]; |
| 29 | + |
| 30 | + expect(value).to.be.instanceOf(HotObservable); |
| 31 | + expect(value.messages).to.deep.equal(expected); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should create hot observale via marble with custom value', () => { |
| 35 | + const scheduler = new TestScheduler(); |
| 36 | + const value = scheduler.createHotObservable('---a---|', { |
| 37 | + a: 'meh' |
| 38 | + }); |
| 39 | + |
| 40 | + const expected: Array<TestMessage<string>> = [next(3, 'meh'), complete(7)]; |
| 41 | + |
| 42 | + expect(value).to.be.instanceOf(HotObservable); |
| 43 | + expect(value.messages).to.deep.equal(expected); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should create hot observable via marble with error', () => { |
| 47 | + const scheduler = new TestScheduler(); |
| 48 | + const value = scheduler.createHotObservable('---a---#'); |
| 49 | + |
| 50 | + const expected: Array<TestMessage<string>> = [next(3, 'a'), error(7, '#')]; |
| 51 | + |
| 52 | + expect(value).to.be.instanceOf(HotObservable); |
| 53 | + expect(value.messages).to.deep.equal(expected); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should create hot observable via marble with custom error', () => { |
| 57 | + const scheduler = new TestScheduler(); |
| 58 | + const value = scheduler.createHotObservable('---a---#', null, 'meh'); |
| 59 | + |
| 60 | + const expected: Array<TestMessage<string>> = [next(3, 'a'), error(7, 'meh')]; |
| 61 | + |
| 62 | + expect(value).to.be.instanceOf(HotObservable); |
| 63 | + expect(value.messages).to.deep.equal(expected); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should honor custom frameTimeFactor', () => { |
| 67 | + const scheduler = new TestScheduler(false, 10); |
| 68 | + |
| 69 | + const value = scheduler.createHotObservable('---a---|'); |
| 70 | + |
| 71 | + const expected: Array<TestMessage<string>> = [next(30, 'a'), complete(70)]; |
| 72 | + |
| 73 | + expect(value).to.be.instanceOf(HotObservable); |
| 74 | + expect(value.messages).to.deep.equal(expected); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('coldObservable', () => { |
| 79 | + it('should create cold observable via TestMessage', () => { |
| 80 | + const scheduler = new TestScheduler(); |
| 81 | + const messages: Array<TestMessage<string>> = [next(10, 'meh'), complete(20)]; |
| 82 | + |
| 83 | + const value = scheduler.createColdObservable(messages); |
| 84 | + |
| 85 | + expect(value).to.be.instanceOf(ColdObservable); |
| 86 | + expect(value.messages).to.deep.equal(messages); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should create hot observable via marble diagram', () => { |
| 90 | + const scheduler = new TestScheduler(); |
| 91 | + const value = scheduler.createColdObservable('---a---|'); |
| 92 | + |
| 93 | + const expected: Array<TestMessage<string>> = [next(3, 'a'), complete(7)]; |
| 94 | + |
| 95 | + expect(value).to.be.instanceOf(ColdObservable); |
| 96 | + expect(value.messages).to.deep.equal(expected); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should create hot observale via marble with custom value', () => { |
| 100 | + const scheduler = new TestScheduler(); |
| 101 | + const value = scheduler.createColdObservable('---a---|', { |
| 102 | + a: 'meh' |
| 103 | + }); |
| 104 | + |
| 105 | + const expected: Array<TestMessage<string>> = [next(3, 'meh'), complete(7)]; |
| 106 | + |
| 107 | + expect(value).to.be.instanceOf(ColdObservable); |
| 108 | + expect(value.messages).to.deep.equal(expected); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should create hot observable via marble with error', () => { |
| 112 | + const scheduler = new TestScheduler(); |
| 113 | + const value = scheduler.createColdObservable('---a---#'); |
| 114 | + |
| 115 | + const expected: Array<TestMessage<string>> = [next(3, 'a'), error(7, '#')]; |
| 116 | + |
| 117 | + expect(value).to.be.instanceOf(ColdObservable); |
| 118 | + expect(value.messages).to.deep.equal(expected); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should create hot observable via marble with custom error', () => { |
| 122 | + const scheduler = new TestScheduler(); |
| 123 | + const value = scheduler.createColdObservable('---a---#', null, 'meh'); |
| 124 | + |
| 125 | + const expected: Array<TestMessage<string>> = [next(3, 'a'), error(7, 'meh')]; |
| 126 | + |
| 127 | + expect(value).to.be.instanceOf(ColdObservable); |
| 128 | + expect(value.messages).to.deep.equal(expected); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should honor custom frameTimeFactor', () => { |
| 132 | + const scheduler = new TestScheduler(false, 10); |
| 133 | + |
| 134 | + const value = scheduler.createColdObservable('---a---|'); |
| 135 | + |
| 136 | + const expected: Array<TestMessage<string>> = [next(30, 'a'), complete(70)]; |
| 137 | + |
| 138 | + expect(value).to.be.instanceOf(ColdObservable); |
| 139 | + expect(value.messages).to.deep.equal(expected); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should throw when marble includes unsupported token', () => { |
| 143 | + const scheduler = new TestScheduler(); |
| 144 | + |
| 145 | + expect(() => scheduler.createColdObservable('----^')).to.throw(); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + describe('getMessages', () => { |
| 150 | + it('should generate messages from observable having hot observable source', () => { |
| 151 | + const expected: Array<TestMessage<string>> = [next(3, 'x'), next(7, 'x'), complete(11)]; |
| 152 | + const scheduler = new TestScheduler(); |
| 153 | + |
| 154 | + const e1 = scheduler.createHotObservable('---1---2---|'); |
| 155 | + const messages = scheduler.getMessages(e1.mapTo('x')); |
| 156 | + |
| 157 | + expect(messages).to.be.empty; |
| 158 | + scheduler.flush(); |
| 159 | + |
| 160 | + expect(messages).to.deep.equal(expected); |
| 161 | + expect(e1.subscriptions).to.deep.equal([new SubscriptionLog(0, 11)]); |
| 162 | + }); |
| 163 | + |
| 164 | + it('should able to autoflush with hot observable source', () => { |
| 165 | + const expected: Array<TestMessage<string>> = [next(3, 'x'), next(7, 'x'), complete(11)]; |
| 166 | + const scheduler = new TestScheduler(true); |
| 167 | + |
| 168 | + const e1 = scheduler.createHotObservable('---1---2---|'); |
| 169 | + const messages = scheduler.getMessages(e1.mapTo('x')); |
| 170 | + |
| 171 | + expect(messages).to.deep.equal(expected); |
| 172 | + expect(e1.subscriptions).to.deep.equal([new SubscriptionLog(0, 11)]); |
| 173 | + }); |
| 174 | + |
| 175 | + it('should throw when autoflush attempted multiple times', () => { |
| 176 | + const scheduler = new TestScheduler(true); |
| 177 | + |
| 178 | + const e1 = scheduler.createHotObservable('---1---2---|'); |
| 179 | + scheduler.getMessages(e1.mapTo('x')); |
| 180 | + |
| 181 | + expect(() => scheduler.getMessages(e1.mapTo('x'))).to.throw(); |
| 182 | + }); |
| 183 | + |
| 184 | + it('should generate messages from observable having hot observable source with error', () => { |
| 185 | + const expected: Array<TestMessage<string>> = [next(3, 'x'), next(7, 'x'), error(11, '#')]; |
| 186 | + const scheduler = new TestScheduler(); |
| 187 | + |
| 188 | + const e1 = scheduler.createHotObservable('---1---2---#'); |
| 189 | + const messages = scheduler.getMessages(e1.mapTo('x')); |
| 190 | + |
| 191 | + expect(messages).to.be.empty; |
| 192 | + scheduler.flush(); |
| 193 | + |
| 194 | + expect(messages).to.deep.equal(expected); |
| 195 | + expect(e1.subscriptions).to.deep.equal([new SubscriptionLog(0, 11)]); |
| 196 | + }); |
| 197 | + |
| 198 | + it('should generate messages from observable having cold observable source', () => { |
| 199 | + const expected: Array<TestMessage<string>> = [next(3, 'x'), next(7, 'x'), complete(11)]; |
| 200 | + const scheduler = new TestScheduler(); |
| 201 | + |
| 202 | + const e1 = scheduler.createColdObservable('---1---2---|'); |
| 203 | + const messages = scheduler.getMessages(e1.mapTo('x')); |
| 204 | + |
| 205 | + expect(messages).to.be.empty; |
| 206 | + scheduler.flush(); |
| 207 | + |
| 208 | + expect(messages).to.deep.equal(expected); |
| 209 | + expect(e1.subscriptions).to.deep.equal([new SubscriptionLog(0, 11)]); |
| 210 | + }); |
| 211 | + |
| 212 | + it('should able to autoflush with cold observable source', () => { |
| 213 | + const expected: Array<TestMessage<string>> = [next(3, 'x'), next(7, 'x'), complete(11)]; |
| 214 | + const scheduler = new TestScheduler(true); |
| 215 | + |
| 216 | + const e1 = scheduler.createColdObservable('---1---2---|'); |
| 217 | + const messages = scheduler.getMessages(e1.mapTo('x')); |
| 218 | + |
| 219 | + expect(messages).to.deep.equal(expected); |
| 220 | + expect(e1.subscriptions).to.deep.equal([new SubscriptionLog(0, 11)]); |
| 221 | + }); |
| 222 | + |
| 223 | + it('should generate messages from observable having cold observable source with error', () => { |
| 224 | + const expected: Array<TestMessage<string>> = [next(3, 'x'), next(7, 'x'), error(11, '#')]; |
| 225 | + const scheduler = new TestScheduler(); |
| 226 | + |
| 227 | + const e1 = scheduler.createColdObservable('---1---2---#'); |
| 228 | + const messages = scheduler.getMessages(e1.mapTo('x')); |
| 229 | + |
| 230 | + expect(messages).to.be.empty; |
| 231 | + scheduler.flush(); |
| 232 | + |
| 233 | + expect(messages).to.deep.equal(expected); |
| 234 | + expect(e1.subscriptions).to.deep.equal([new SubscriptionLog(0, 11)]); |
| 235 | + }); |
| 236 | + |
| 237 | + it('should materialize inner observable with cold observable source', () => { |
| 238 | + const scheduler = new TestScheduler(); |
| 239 | + const expected: Array<TestMessage<Readonly<TestMessage<string | TestMessage<string>[]>[]>>> = [ |
| 240 | + next(0, parseObservableMarble<string>(' ---a---b---(c|)')), |
| 241 | + next(11, parseObservableMarble<string>(' ----d---|')), |
| 242 | + complete(19) |
| 243 | + ]; |
| 244 | + |
| 245 | + const source = scheduler.createColdObservable('---a---b---c---d---|'); |
| 246 | + |
| 247 | + const messages = scheduler.getMessages(source.windowCount(3)); |
| 248 | + expect(messages).to.be.empty; |
| 249 | + |
| 250 | + scheduler.flush(); |
| 251 | + expect(messages).to.deep.equal(expected); |
| 252 | + }); |
| 253 | + |
| 254 | + it('should materialize inner observable with cold observable source with error', () => { |
| 255 | + const scheduler = new TestScheduler(); |
| 256 | + const expected: Array<TestMessage<Readonly<TestMessage<string | TestMessage<string>[]>[]>>> = [ |
| 257 | + next(0, parseObservableMarble<string>(' ---a---b---(c|)')), |
| 258 | + next(11, parseObservableMarble<string>(' ----#')), |
| 259 | + error(15, '#') |
| 260 | + ]; |
| 261 | + |
| 262 | + const source = scheduler.createColdObservable('---a---b---c---#'); |
| 263 | + |
| 264 | + const messages = scheduler.getMessages(source.windowCount(3)); |
| 265 | + expect(messages).to.be.empty; |
| 266 | + |
| 267 | + scheduler.flush(); |
| 268 | + expect(messages).to.deep.equal(expected); |
| 269 | + }); |
| 270 | + |
| 271 | + it('should materialize inner observable with hot observable source', () => { |
| 272 | + const scheduler = new TestScheduler(); |
| 273 | + const expected: Array<TestMessage<Readonly<TestMessage<string | TestMessage<string>[]>[]>>> = [ |
| 274 | + next(0, parseObservableMarble<string>(' ---a---b---(c|)')), |
| 275 | + next(11, parseObservableMarble<string>(' ----d---|')), |
| 276 | + complete(19) |
| 277 | + ]; |
| 278 | + |
| 279 | + const source = scheduler.createHotObservable('---a---b---c---d---|'); |
| 280 | + |
| 281 | + const messages = scheduler.getMessages(source.windowCount(3)); |
| 282 | + expect(messages).to.be.empty; |
| 283 | + |
| 284 | + scheduler.flush(); |
| 285 | + expect(messages).to.deep.equal(expected); |
| 286 | + }); |
| 287 | + |
| 288 | + it('should materialize inner observable with hot observable source with error', () => { |
| 289 | + const scheduler = new TestScheduler(); |
| 290 | + const expected: Array<TestMessage<Readonly<TestMessage<string | TestMessage<string>[]>[]>>> = [ |
| 291 | + next(0, parseObservableMarble<string>(' ---a---b---(c|)')), |
| 292 | + next(11, parseObservableMarble<string>(' ----#')), |
| 293 | + error(15, '#') |
| 294 | + ]; |
| 295 | + |
| 296 | + const source = scheduler.createHotObservable('---a---b---c---#'); |
| 297 | + |
| 298 | + const messages = scheduler.getMessages(source.windowCount(3)); |
| 299 | + expect(messages).to.be.empty; |
| 300 | + |
| 301 | + scheduler.flush(); |
| 302 | + expect(messages).to.deep.equal(expected); |
| 303 | + }); |
| 304 | + |
| 305 | + it('should support subscription with cold observable source', () => { |
| 306 | + const expected: Array<TestMessage<string>> = [next(3, 'x')]; |
| 307 | + const scheduler = new TestScheduler(); |
| 308 | + |
| 309 | + const e1 = scheduler.createColdObservable('---1---2---|'); |
| 310 | + const sub = ' ------^---!'; |
| 311 | + //actual subscription: ---1- |
| 312 | + |
| 313 | + const messages = scheduler.getMessages(e1.mapTo('x'), sub); |
| 314 | + |
| 315 | + expect(messages).to.be.empty; |
| 316 | + scheduler.flush(); |
| 317 | + |
| 318 | + expect(messages).to.deep.equal(expected); |
| 319 | + expect(e1.subscriptions).to.deep.equal([new SubscriptionLog(0, 4)]); |
| 320 | + }); |
| 321 | + |
| 322 | + it('should support subscription with hot observable source', () => { |
| 323 | + const expected: Array<TestMessage<string>> = [next(7, 'x')]; |
| 324 | + const scheduler = new TestScheduler(); |
| 325 | + |
| 326 | + const e1 = scheduler.createHotObservable('---1---2---|'); |
| 327 | + const sub = ' ------^---!'; |
| 328 | + const messages = scheduler.getMessages(e1.mapTo('x'), sub); |
| 329 | + |
| 330 | + expect(messages).to.be.empty; |
| 331 | + scheduler.flush(); |
| 332 | + |
| 333 | + expect(messages).to.deep.equal(expected); |
| 334 | + expect(e1.subscriptions).to.deep.equal([new SubscriptionLog(6, 10)]); |
| 335 | + }); |
| 336 | + }); |
| 337 | +}); |
0 commit comments