|
| 1 | +import { TestBed, inject } from '@angular/core/testing'; |
| 2 | + |
| 3 | +import { ObservableContainer } from './observable-container'; |
| 4 | +import { Observable } from 'rxjs/Observable'; |
| 5 | +import { Subject } from 'rxjs/Subject'; |
| 6 | + |
| 7 | +describe('ObservableContainer', () => { |
| 8 | + let subject: Subject<{}>; |
| 9 | + beforeEach(() => { |
| 10 | + subject = new Subject(); |
| 11 | + }); |
| 12 | + |
| 13 | + it(`calls the function when the observable emits`, () => { |
| 14 | + let called = false; |
| 15 | + const container = new ObservableContainer(() => called = true); |
| 16 | + |
| 17 | + container.subscribe({}, () => subject); |
| 18 | + expect(called).toBe(false); |
| 19 | + subject.next(); |
| 20 | + expect(called).toBe(true); |
| 21 | + }); |
| 22 | + |
| 23 | + it(`calls the function on subscribe`, () => { |
| 24 | + let called = false; |
| 25 | + const container = new ObservableContainer(() => called = true); |
| 26 | + |
| 27 | + container.subscribe({}, () => subject, true); |
| 28 | + expect(called).toBe(true); |
| 29 | + }); |
| 30 | + |
| 31 | + it(`calls the function multiple times when the observable emits multiple times`, () => { |
| 32 | + let calls = 0; |
| 33 | + const container = new ObservableContainer(() => calls++); |
| 34 | + |
| 35 | + container.subscribe({}, () => subject); |
| 36 | + expect(calls).toBe(0); |
| 37 | + subject.next(); |
| 38 | + subject.next(); |
| 39 | + expect(calls).toBe(2); |
| 40 | + }); |
| 41 | + |
| 42 | + it(`calls the function once per subscribing object`, () => { |
| 43 | + let calls = 0; |
| 44 | + const objA = {}; |
| 45 | + const objB = {}; |
| 46 | + const container = new ObservableContainer(obj => { |
| 47 | + if (calls === 0) { |
| 48 | + expect(obj).toEqual(objA); |
| 49 | + } else { |
| 50 | + expect(obj).toEqual(objB); |
| 51 | + } |
| 52 | + calls++; |
| 53 | + }); |
| 54 | + |
| 55 | + container.subscribe([objA, {}], () => subject); |
| 56 | + expect(calls).toBe(0); |
| 57 | + subject.next(); |
| 58 | + expect(calls).toBe(2); |
| 59 | + }); |
| 60 | + |
| 61 | + it(`unsubscribes and no longer calls the function when the observable emits`, () => { |
| 62 | + let calls = 0; |
| 63 | + const container = new ObservableContainer(() => calls++); |
| 64 | + |
| 65 | + container.subscribe([{}, {}], () => subject); |
| 66 | + expect(subject.observers.length).toBe(2); |
| 67 | + |
| 68 | + container.unsubscribeAll(); |
| 69 | + expect(subject.observers.length).toBe(0); |
| 70 | + |
| 71 | + subject.next(); |
| 72 | + expect(calls).toBe(0); |
| 73 | + }); |
| 74 | + |
| 75 | + it(`doesn't subscribe or call the function when no objects are provided`, () => { |
| 76 | + let called = false; |
| 77 | + const container = new ObservableContainer(() => called = true); |
| 78 | + |
| 79 | + container.subscribe([], () => subject); |
| 80 | + expect(subject.observers.length).toBe(0); |
| 81 | + expect(called).toBe(false); |
| 82 | + subject.next(); |
| 83 | + expect(called).toBe(false); |
| 84 | + }); |
| 85 | +}); |
0 commit comments