|
| 1 | +import { |
| 2 | + ChangeDetectorRef, |
| 3 | + EmbeddedViewRef, |
| 4 | + Injector, |
| 5 | + NgZone, |
| 6 | + OnDestroy, |
| 7 | + Type, |
| 8 | +} from '@angular/core'; |
| 9 | +import { CdAware, createCdAware, getGlobalThis } from '../../src/core'; |
| 10 | +import { |
| 11 | + concat, |
| 12 | + EMPTY, |
| 13 | + NEVER, |
| 14 | + NextObserver, |
| 15 | + Observable, |
| 16 | + of, |
| 17 | + PartialObserver, |
| 18 | + Unsubscribable, |
| 19 | +} from 'rxjs'; |
| 20 | +import { tap } from 'rxjs/operators'; |
| 21 | + |
| 22 | +class CdAwareImplementation<U> implements OnDestroy { |
| 23 | + public renderedValue: any = undefined; |
| 24 | + public error: any = undefined; |
| 25 | + public completed: boolean = false; |
| 26 | + private readonly subscription: Unsubscribable; |
| 27 | + public cdAware: CdAware<U | undefined | null>; |
| 28 | + resetContextObserver: NextObserver<any> = { |
| 29 | + next: _ => (this.renderedValue = undefined), |
| 30 | + error: e => (this.error = e), |
| 31 | + complete: () => (this.completed = true), |
| 32 | + }; |
| 33 | + updateViewContextObserver: PartialObserver<U | undefined | null> = { |
| 34 | + next: (n: U | undefined | null) => (this.renderedValue = n), |
| 35 | + error: e => (this.error = e), |
| 36 | + complete: () => (this.completed = true), |
| 37 | + }; |
| 38 | + configurableBehaviour = <T>( |
| 39 | + o$: Observable<Observable<T>> |
| 40 | + ): Observable<Observable<T>> => o$.pipe(tap()); |
| 41 | + |
| 42 | + constructor() { |
| 43 | + this.cdAware = createCdAware<U>({ |
| 44 | + work: () => {}, |
| 45 | + resetContextObserver: this.resetContextObserver, |
| 46 | + updateViewContextObserver: this.updateViewContextObserver, |
| 47 | + configurableBehaviour: this.configurableBehaviour, |
| 48 | + }); |
| 49 | + this.subscription = this.cdAware.subscribe(); |
| 50 | + } |
| 51 | + |
| 52 | + ngOnDestroy(): void { |
| 53 | + this.subscription.unsubscribe(); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +let cdAwareImplementation: CdAwareImplementation<any>; |
| 58 | +const setupCdAwareImplementation = () => { |
| 59 | + cdAwareImplementation = new CdAwareImplementation(); |
| 60 | + cdAwareImplementation.renderedValue = undefined; |
| 61 | + cdAwareImplementation.error = undefined; |
| 62 | + cdAwareImplementation.completed = false; |
| 63 | +}; |
| 64 | + |
| 65 | +describe('CdAware', () => { |
| 66 | + beforeEach(() => { |
| 67 | + setupCdAwareImplementation(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should be implementable', () => { |
| 71 | + expect(cdAwareImplementation).toBeDefined(); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('next value', () => { |
| 75 | + it('should do nothing if initialized (as no value ever was emitted)', () => { |
| 76 | + expect(cdAwareImplementation.renderedValue).toBe(undefined); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should render undefined as value when initially undefined was passed (as no value ever was emitted)', () => { |
| 80 | + cdAwareImplementation.cdAware.next(undefined); |
| 81 | + expect(cdAwareImplementation.renderedValue).toBe(undefined); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should render null as value when initially null was passed (as no value ever was emitted)', () => { |
| 85 | + cdAwareImplementation.cdAware.next(null); |
| 86 | + expect(cdAwareImplementation.renderedValue).toBe(null); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should render undefined as value when initially of(undefined) was passed (as undefined was emitted)', () => { |
| 90 | + cdAwareImplementation.cdAware.next(of(undefined)); |
| 91 | + expect(cdAwareImplementation.renderedValue).toBe(undefined); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should render null as value when initially of(null) was passed (as null was emitted)', () => { |
| 95 | + cdAwareImplementation.cdAware.next(of(null)); |
| 96 | + expect(cdAwareImplementation.renderedValue).toBe(null); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should render undefined as value when initially EMPTY was passed (as no value ever was emitted)', () => { |
| 100 | + cdAwareImplementation.cdAware.next(EMPTY); |
| 101 | + expect(cdAwareImplementation.renderedValue).toBe(undefined); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should render undefined as value when initially NEVER was passed (as no value ever was emitted)', () => { |
| 105 | + cdAwareImplementation.cdAware.next(NEVER); |
| 106 | + expect(cdAwareImplementation.renderedValue).toBe(undefined); |
| 107 | + }); |
| 108 | + // Also: 'should keep last emitted value in the view until a new observable NEVER was passed (as no value ever was emitted from new observable)' |
| 109 | + it('should render emitted value from passed observable without changing it', () => { |
| 110 | + cdAwareImplementation.cdAware.next(of(42)); |
| 111 | + expect(cdAwareImplementation.renderedValue).toBe(42); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should render undefined as value when a new observable NEVER was passed (as no value ever was emitted from new observable)', () => { |
| 115 | + cdAwareImplementation.cdAware.next(of(42)); |
| 116 | + expect(cdAwareImplementation.renderedValue).toBe(42); |
| 117 | + cdAwareImplementation.cdAware.next(NEVER); |
| 118 | + expect(cdAwareImplementation.renderedValue).toBe(undefined); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + describe('observable context', () => { |
| 123 | + it('next handling running observable', () => { |
| 124 | + cdAwareImplementation.cdAware.next(concat(of(42), NEVER)); |
| 125 | + expect(cdAwareImplementation.renderedValue).toBe(42); |
| 126 | + expect(cdAwareImplementation.error).toBe(undefined); |
| 127 | + expect(cdAwareImplementation.completed).toBe(false); |
| 128 | + }); |
| 129 | + |
| 130 | + it('next handling completed observable', () => { |
| 131 | + cdAwareImplementation.cdAware.next(of(42)); |
| 132 | + expect(cdAwareImplementation.renderedValue).toBe(42); |
| 133 | + expect(cdAwareImplementation.error).toBe(undefined); |
| 134 | + expect(cdAwareImplementation.completed).toBe(true); |
| 135 | + }); |
| 136 | + |
| 137 | + it('error handling', () => { |
| 138 | + expect(cdAwareImplementation.renderedValue).toBe(undefined); |
| 139 | + cdAwareImplementation.cdAware.subscribe({ |
| 140 | + error: (e: Error) => expect(e).toBeDefined(), |
| 141 | + }); |
| 142 | + expect(cdAwareImplementation.renderedValue).toBe(undefined); |
| 143 | + // @TODO use this line |
| 144 | + // expect(cdAwareImplementation.error).toBe(ArgumentNotObservableError); |
| 145 | + expect(cdAwareImplementation.completed).toBe(false); |
| 146 | + }); |
| 147 | + |
| 148 | + it('completion handling', () => { |
| 149 | + cdAwareImplementation.cdAware.next(EMPTY); |
| 150 | + expect(cdAwareImplementation.renderedValue).toBe(undefined); |
| 151 | + expect(cdAwareImplementation.error).toBe(undefined); |
| 152 | + expect(cdAwareImplementation.completed).toBe(true); |
| 153 | + }); |
| 154 | + }); |
| 155 | +}); |
0 commit comments