| 
									
										
										
										
											2016-06-23 09:47:54 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							| 
									
										
										
										
											2020-05-19 12:08:49 -07:00
										 |  |  |  * Copyright Google LLC All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2016-06-23 09:47:54 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-02 12:12:46 -08:00
										 |  |  | import {AsyncTestCompleter, beforeEach, describe, expect, inject, it} from '@angular/core/testing/src/testing_internal'; | 
					
						
							| 
									
										
										
										
											2018-02-04 17:58:02 +01:00
										 |  |  | import {filter} from 'rxjs/operators'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-01 10:28:52 -08:00
										 |  |  | import {EventEmitter} from '../src/event_emitter'; | 
					
						
							| 
									
										
										
										
											2015-05-26 17:00:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-15 16:28:41 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:00:31 -07:00
										 |  |  |   describe('EventEmitter', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |     let emitter: EventEmitter<any>; | 
					
						
							| 
									
										
										
										
											2015-05-26 17:00:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       emitter = new EventEmitter(); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:00:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     it('should call the next callback', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |          emitter.subscribe({ | 
					
						
							|  |  |  |            next: (value: any) => { | 
					
						
							|  |  |  |              expect(value).toEqual(99); | 
					
						
							|  |  |  |              async.done(); | 
					
						
							|  |  |  |            } | 
					
						
							| 
									
										
										
										
											2015-05-26 17:00:31 -07:00
										 |  |  |          }); | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |          emitter.emit(99); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:00:31 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     it('should call the throw callback', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |          emitter.subscribe({ | 
					
						
							|  |  |  |            next: () => {}, | 
					
						
							|  |  |  |            error: (error: any) => { | 
					
						
							|  |  |  |              expect(error).toEqual('Boom'); | 
					
						
							|  |  |  |              async.done(); | 
					
						
							|  |  |  |            } | 
					
						
							| 
									
										
										
										
											2015-05-26 17:00:31 -07:00
										 |  |  |          }); | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |          emitter.error('Boom'); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:00:31 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     it('should work when no throw callback is provided', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |          emitter.subscribe({ | 
					
						
							|  |  |  |            next: () => {}, | 
					
						
							|  |  |  |            error: (_: any) => { | 
					
						
							|  |  |  |              async.done(); | 
					
						
							|  |  |  |            } | 
					
						
							|  |  |  |          }); | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |          emitter.error('Boom'); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:00:31 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     it('should call the return callback', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |          emitter.subscribe({ | 
					
						
							|  |  |  |            next: () => {}, | 
					
						
							|  |  |  |            error: (_: any) => {}, | 
					
						
							|  |  |  |            complete: () => { | 
					
						
							|  |  |  |              async.done(); | 
					
						
							|  |  |  |            } | 
					
						
							|  |  |  |          }); | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |          emitter.complete(); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:00:31 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     it('should subscribe to the wrapper synchronously', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       let called = false; | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |       emitter.subscribe({ | 
					
						
							|  |  |  |         next: (value: any) => { | 
					
						
							|  |  |  |           called = true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |       emitter.emit(99); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:00:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-26 09:34:04 -07:00
										 |  |  |       expect(called).toBe(true); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:00:31 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-02 19:58:03 +02:00
										 |  |  |     it('delivers next and error events synchronously', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |          const log: any[] /** TODO #9100 */ = []; | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |          emitter.subscribe({ | 
					
						
							|  |  |  |            next: (x: any) => { | 
					
						
							|  |  |  |              log.push(x); | 
					
						
							|  |  |  |              expect(log).toEqual([1, 2]); | 
					
						
							|  |  |  |            }, | 
					
						
							|  |  |  |            error: (err: any) => { | 
					
						
							|  |  |  |              log.push(err); | 
					
						
							|  |  |  |              expect(log).toEqual([1, 2, 3, 4]); | 
					
						
							|  |  |  |              async.done(); | 
					
						
							|  |  |  |            } | 
					
						
							|  |  |  |          }); | 
					
						
							| 
									
										
										
										
											2016-08-02 19:58:03 +02:00
										 |  |  |          log.push(1); | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |          emitter.emit(2); | 
					
						
							| 
									
										
										
										
											2016-08-02 19:58:03 +02:00
										 |  |  |          log.push(3); | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |          emitter.error(4); | 
					
						
							| 
									
										
										
										
											2016-08-02 19:58:03 +02:00
										 |  |  |          log.push(5); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('delivers next and complete events synchronously', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const log: any[] /** TODO #9100 */ = []; | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       emitter.subscribe({ | 
					
						
							|  |  |  |         next: (x: any) => { | 
					
						
							|  |  |  |           log.push(x); | 
					
						
							|  |  |  |           expect(log).toEqual([1, 2]); | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         error: null, | 
					
						
							|  |  |  |         complete: () => { | 
					
						
							|  |  |  |           log.push(4); | 
					
						
							|  |  |  |           expect(log).toEqual([1, 2, 3, 4]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-08-02 19:58:03 +02:00
										 |  |  |       log.push(1); | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |       emitter.emit(2); | 
					
						
							| 
									
										
										
										
											2016-08-02 19:58:03 +02:00
										 |  |  |       log.push(3); | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |       emitter.complete(); | 
					
						
							| 
									
										
										
										
											2016-08-02 19:58:03 +02:00
										 |  |  |       log.push(5); | 
					
						
							|  |  |  |       expect(log).toEqual([1, 2, 3, 4, 5]); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-10-19 14:41:15 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     it('delivers events asynchronously when forced to async mode', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |          const e = new EventEmitter(true); | 
					
						
							|  |  |  |          const log: any[] /** TODO #9100 */ = []; | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |          e.subscribe((x: any) => { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |            log.push(x); | 
					
						
							|  |  |  |            expect(log).toEqual([1, 3, 2]); | 
					
						
							|  |  |  |            async.done(); | 
					
						
							|  |  |  |          }); | 
					
						
							|  |  |  |          log.push(1); | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |          e.emit(2); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |          log.push(3); | 
					
						
							|  |  |  |        })); | 
					
						
							| 
									
										
										
										
											2015-10-19 14:41:15 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('reports whether it has subscribers', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const e = new EventEmitter(false); | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |       expect(e.observers.length > 0).toBe(false); | 
					
						
							|  |  |  |       e.subscribe({next: () => {}}); | 
					
						
							|  |  |  |       expect(e.observers.length > 0).toBe(true); | 
					
						
							| 
									
										
										
										
											2015-10-19 14:41:15 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-04 17:58:02 +01:00
										 |  |  |     it('remove a subscriber subscribed directly to EventEmitter', () => { | 
					
						
							|  |  |  |       const sub = emitter.subscribe(); | 
					
						
							|  |  |  |       expect(emitter.observers.length).toBe(1); | 
					
						
							|  |  |  |       sub.unsubscribe(); | 
					
						
							|  |  |  |       expect(emitter.observers.length).toBe(0); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('remove a subscriber subscribed after applying operators with pipe()', () => { | 
					
						
							|  |  |  |       const sub = emitter.pipe(filter(() => true)).subscribe(); | 
					
						
							|  |  |  |       expect(emitter.observers.length).toBe(1); | 
					
						
							|  |  |  |       sub.unsubscribe(); | 
					
						
							|  |  |  |       expect(emitter.observers.length).toBe(0); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('unsubscribing a subscriber invokes the dispose method', () => { | 
					
						
							|  |  |  |       inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							|  |  |  |         const sub = emitter.subscribe(); | 
					
						
							|  |  |  |         sub.add(() => async.done()); | 
					
						
							|  |  |  |         sub.unsubscribe(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('unsubscribing a subscriber after applying operators with pipe() invokes the dispose method', | 
					
						
							|  |  |  |        () => { | 
					
						
							|  |  |  |          inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							|  |  |  |            const sub = emitter.pipe(filter(() => true)).subscribe(); | 
					
						
							|  |  |  |            sub.add(() => async.done()); | 
					
						
							|  |  |  |            sub.unsubscribe(); | 
					
						
							|  |  |  |          }); | 
					
						
							|  |  |  |        }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('error thrown inside an Rx chain propagates to the error handler and disposes the chain', | 
					
						
							|  |  |  |        () => { | 
					
						
							|  |  |  |          let errorPropagated = false; | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |          emitter | 
					
						
							|  |  |  |              .pipe( | 
					
						
							|  |  |  |                  filter(() => { | 
					
						
							|  |  |  |                    throw new Error(); | 
					
						
							|  |  |  |                  }), | 
					
						
							|  |  |  |                  ) | 
					
						
							|  |  |  |              .subscribe( | 
					
						
							|  |  |  |                  () => {}, | 
					
						
							|  |  |  |                  err => errorPropagated = true, | 
					
						
							|  |  |  |              ); | 
					
						
							| 
									
										
										
										
											2018-02-04 17:58:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |          emitter.next(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          expect(errorPropagated).toBe(true); | 
					
						
							|  |  |  |          expect(emitter.observers.length).toBe(0); | 
					
						
							|  |  |  |        }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('error sent by EventEmitter should dispose the Rx chain and remove subscribers', () => { | 
					
						
							|  |  |  |       let errorPropagated = false; | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |       emitter.pipe(filter(() => true)) | 
					
						
							|  |  |  |           .subscribe( | 
					
						
							|  |  |  |               () => {}, | 
					
						
							|  |  |  |               err => errorPropagated = true, | 
					
						
							|  |  |  |           ); | 
					
						
							| 
									
										
										
										
											2018-02-04 17:58:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       emitter.error(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(errorPropagated).toBe(true); | 
					
						
							|  |  |  |       expect(emitter.observers.length).toBe(0); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:00:31 -07:00
										 |  |  |     // TODO: vsavkin: add tests cases
 | 
					
						
							|  |  |  |     // should call dispose on the subscription if generator returns {done:true}
 | 
					
						
							|  |  |  |     // should call dispose on the subscription on throw
 | 
					
						
							|  |  |  |     // should call dispose on the subscription on return
 | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-05-29 17:09:59 -07:00
										 |  |  | } |