|
| 1 | +import { ɵglobal } from '@angular/core'; |
| 2 | +import { TestScheduler } from 'rxjs/testing'; |
| 3 | +import * as rxjs from 'rxjs'; |
| 4 | +// tslint:disable-next-line:nx-enforce-module-boundaries |
| 5 | +import { jestMatcher } from '@test-helpers'; |
| 6 | +// tslint:disable-next-line:nx-enforce-module-boundaries |
| 7 | +import { onStrategy, RX_NATIVE_STRATEGIES } from '@rx-angular/cdk'; |
| 8 | +import { animationFrameScheduler, observeOn, of } from 'rxjs'; |
| 9 | + |
| 10 | +// tslint:disable: no-duplicate-string |
| 11 | +describe('onStrategy', () => { |
| 12 | + |
| 13 | + let testScheduler: TestScheduler; |
| 14 | + let handles = []; |
| 15 | + let animationRuns = 0; |
| 16 | + function _animate() { |
| 17 | + handles.forEach(handle => handle()); |
| 18 | + } |
| 19 | + let _animationFrameRestore; |
| 20 | + let _cancelAnimationFrameRestore; |
| 21 | + beforeEach(() => { |
| 22 | + testScheduler = new TestScheduler(jestMatcher); |
| 23 | + _animationFrameRestore = ɵglobal.requestAnimationFrame; |
| 24 | + _cancelAnimationFrameRestore = ɵglobal.cancelAnimationFrame; |
| 25 | + ɵglobal.requestAnimationFrame = (cb?) => { |
| 26 | + handles[animationRuns] = cb; |
| 27 | + animationRuns++; |
| 28 | + return animationRuns; |
| 29 | + } |
| 30 | + ɵglobal.cancelAnimationFrame = (id: number) => { |
| 31 | + handles = handles.splice(id, 1); |
| 32 | + } |
| 33 | + }); |
| 34 | + |
| 35 | + |
| 36 | + afterEach(() => { |
| 37 | + ɵglobal.requestAnimationFrame = _animationFrameRestore; |
| 38 | + ɵglobal.cancelAnimationFrame = _cancelAnimationFrameRestore; |
| 39 | + animationRuns = 0; |
| 40 | + handles = []; |
| 41 | + }); |
| 42 | + |
| 43 | + it('should emit 42', () => { |
| 44 | + testScheduler.run(({expectObservable}) => { |
| 45 | + const work = jest.fn(); |
| 46 | + const values = { x: 42 }; |
| 47 | + const stream$ = onStrategy(values.x, RX_NATIVE_STRATEGIES.native, work); |
| 48 | + const expected = '(x|)'; |
| 49 | + expectObservable(stream$).toBe(expected, values) |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should throw error', () => { |
| 54 | + testScheduler.run(({expectObservable}) => { |
| 55 | + const error = new Error('error'); |
| 56 | + const work = () => { |
| 57 | + throw error; |
| 58 | + } |
| 59 | + const value = 42; |
| 60 | + const errorValues = [error, value]; |
| 61 | + const expected = '#'; |
| 62 | + const stream$ = onStrategy(value, RX_NATIVE_STRATEGIES.native, work); |
| 63 | + expectObservable(stream$).toBe(expected, null, errorValues); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should run on animationFrame', () => { |
| 68 | + testScheduler.run(({ expectObservable, animate }) => { |
| 69 | + const work = jest.fn(); |
| 70 | + animate(' --------x'); |
| 71 | + const expected = '--------(x|)'; |
| 72 | + of(null).pipe(observeOn(animationFrameScheduler)).subscribe(() => { |
| 73 | + _animate(); |
| 74 | + }); |
| 75 | + const values = { x: 42 }; |
| 76 | + const stream$ = onStrategy(values.x, RX_NATIVE_STRATEGIES.local, work); |
| 77 | + expectObservable(stream$).toBe(expected, values); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('it should call work once when async', () => { |
| 82 | + const work = jest.fn(); |
| 83 | + onStrategy(42, RX_NATIVE_STRATEGIES.local, work).subscribe(); |
| 84 | + expect(work).not.toHaveBeenCalled(); |
| 85 | + _animate(); |
| 86 | + expect(work).toHaveBeenCalledTimes(1); |
| 87 | + }); |
| 88 | + |
| 89 | + it('it should call work', () => { |
| 90 | + const work = jest.fn(); |
| 91 | + onStrategy(42, RX_NATIVE_STRATEGIES.native, work).subscribe(); |
| 92 | + expect(work).toHaveBeenCalledTimes(1); |
| 93 | + }); |
| 94 | +}); |
0 commit comments