|
6 | 6 | TestBed,
|
7 | 7 | waitForAsync,
|
8 | 8 | } from '@angular/core/testing';
|
9 |
| -import { BehaviorSubject, EMPTY, NEVER, of, throwError } from 'rxjs'; |
| 9 | +import { BehaviorSubject, delay, EMPTY, NEVER, of, throwError } from 'rxjs'; |
10 | 10 | import { PushPipe } from '../../src/push/push.pipe';
|
11 | 11 | import { MockChangeDetectorRef, MockErrorHandler } from '../fixtures/fixtures';
|
12 | 12 | import { stripSpaces, wrapWithSpace } from '../helpers';
|
@@ -66,11 +66,21 @@ describe('PushPipe', () => {
|
66 | 66 | expect(pushPipe.transform(true)).toBe(true);
|
67 | 67 | });
|
68 | 68 |
|
69 |
| - it('should return initially passed object', () => { |
| 69 | + it('should return initially passed empty object', () => { |
| 70 | + const obj = {}; |
| 71 | + expect(pushPipe.transform(obj)).toBe(obj); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should return initially passed object with non-observable values', () => { |
70 | 75 | const obj = { ngrx: 'component' };
|
71 | 76 | expect(pushPipe.transform(obj)).toBe(obj);
|
72 | 77 | });
|
73 | 78 |
|
| 79 | + it('should return initially passed object with at least one non-observable value', () => { |
| 80 | + const obj = { ngrx: 'component', obs$: of(10) }; |
| 81 | + expect(pushPipe.transform(obj)).toBe(obj); |
| 82 | + }); |
| 83 | + |
74 | 84 | it('should return initially passed array', () => {
|
75 | 85 | const arr = [1, 2, 3];
|
76 | 86 | expect(pushPipe.transform(arr)).toBe(arr);
|
@@ -115,6 +125,19 @@ describe('PushPipe', () => {
|
115 | 125 | });
|
116 | 126 | });
|
117 | 127 |
|
| 128 | + it('should return undefined when any observable from dictionary emits first value asynchronously', () => { |
| 129 | + const result = pushPipe.transform({ |
| 130 | + o1$: of(1).pipe(delay(1)), |
| 131 | + o2$: of(2), |
| 132 | + }); |
| 133 | + expect(result).toBe(undefined); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should return emitted values from observables that are passed as dictionary and emit first values synchronously', () => { |
| 137 | + const result = pushPipe.transform({ o1: of(10), o2: of(20) }); |
| 138 | + expect(result).toEqual({ o1: 10, o2: 20 }); |
| 139 | + }); |
| 140 | + |
118 | 141 | it('should return undefined as value when a new observable NEVER was passed (as no value ever was emitted from new observable)', () => {
|
119 | 142 | expect(pushPipe.transform(of(42))).toBe(42);
|
120 | 143 | expect(pushPipe.transform(NEVER)).toBe(undefined);
|
@@ -192,14 +215,28 @@ describe('PushPipe', () => {
|
192 | 215 | expect(componentNativeElement.textContent).toBe(wrapWithSpace('true'));
|
193 | 216 | });
|
194 | 217 |
|
195 |
| - it('should render initially passed object', () => { |
| 218 | + it('should render initially passed empty object', () => { |
| 219 | + pushPipeTestComponent.value$ = {}; |
| 220 | + fixturePushPipeTestComponent.detectChanges(); |
| 221 | + expect(stripSpaces(componentNativeElement.textContent)).toBe('{}'); |
| 222 | + }); |
| 223 | + |
| 224 | + it('should render initially passed object with non-observable values', () => { |
196 | 225 | pushPipeTestComponent.value$ = { ngrx: 'component' };
|
197 | 226 | fixturePushPipeTestComponent.detectChanges();
|
198 | 227 | expect(stripSpaces(componentNativeElement.textContent)).toBe(
|
199 | 228 | '{"ngrx":"component"}'
|
200 | 229 | );
|
201 | 230 | });
|
202 | 231 |
|
| 232 | + it('should render initially passed object with at least one non-observable value', () => { |
| 233 | + pushPipeTestComponent.value$ = { ngrx: 'component', o: of('ngrx') }; |
| 234 | + fixturePushPipeTestComponent.detectChanges(); |
| 235 | + expect(stripSpaces(componentNativeElement.textContent)).toBe( |
| 236 | + '{"ngrx":"component","o":{}}' |
| 237 | + ); |
| 238 | + }); |
| 239 | + |
203 | 240 | it('should render initially passed array', () => {
|
204 | 241 | pushPipeTestComponent.value$ = [1, 2, 3];
|
205 | 242 | fixturePushPipeTestComponent.detectChanges();
|
@@ -265,6 +302,39 @@ describe('PushPipe', () => {
|
265 | 302 | expect(componentNativeElement.textContent).toBe(wrapWithSpace('42'));
|
266 | 303 | }));
|
267 | 304 |
|
| 305 | + it('should render undefined initially when any observable from dictionary emits first value asynchronously', () => { |
| 306 | + pushPipeTestComponent.value$ = { |
| 307 | + o1: of(100), |
| 308 | + o2: of(200).pipe(delay(1)), |
| 309 | + }; |
| 310 | + fixturePushPipeTestComponent.detectChanges(); |
| 311 | + expect(componentNativeElement.textContent).toBe( |
| 312 | + wrapWithSpace('undefined') |
| 313 | + ); |
| 314 | + }); |
| 315 | + |
| 316 | + it('should render emitted values from observables that are passed as dictionary', () => { |
| 317 | + const o1 = new BehaviorSubject('ng'); |
| 318 | + const o2 = new BehaviorSubject('rx'); |
| 319 | + pushPipeTestComponent.value$ = { o1, o2 }; |
| 320 | + fixturePushPipeTestComponent.detectChanges(); |
| 321 | + expect(stripSpaces(componentNativeElement.textContent)).toBe( |
| 322 | + '{"o1":"ng","o2":"rx"}' |
| 323 | + ); |
| 324 | + |
| 325 | + o1.next('ngrx'); |
| 326 | + fixturePushPipeTestComponent.detectChanges(); |
| 327 | + expect(stripSpaces(componentNativeElement.textContent)).toBe( |
| 328 | + '{"o1":"ngrx","o2":"rx"}' |
| 329 | + ); |
| 330 | + |
| 331 | + o2.next('component'); |
| 332 | + fixturePushPipeTestComponent.detectChanges(); |
| 333 | + expect(stripSpaces(componentNativeElement.textContent)).toBe( |
| 334 | + '{"o1":"ngrx","o2":"component"}' |
| 335 | + ); |
| 336 | + }); |
| 337 | + |
268 | 338 | it('should render undefined as value when a new observable NEVER was passed (as no value ever was emitted from new observable)', () => {
|
269 | 339 | pushPipeTestComponent.value$ = of(42);
|
270 | 340 | fixturePushPipeTestComponent.detectChanges();
|
@@ -310,7 +380,7 @@ describe('PushPipe', () => {
|
310 | 380 | );
|
311 | 381 | });
|
312 | 382 |
|
313 |
| - it('should return non-observable value when it was passed after another non-observable', () => { |
| 383 | + it('should render non-observable value when it was passed after another non-observable', () => { |
314 | 384 | pushPipeTestComponent.value$ = 10;
|
315 | 385 | fixturePushPipeTestComponent.detectChanges();
|
316 | 386 | expect(componentNativeElement.textContent).toBe(wrapWithSpace('10'));
|
|
0 commit comments