|
| 1 | +import { RxLet } from '../let.directive'; |
| 2 | +import { TestBed, fakeAsync } from '@angular/core/testing'; |
| 3 | +import { |
| 4 | + ChangeDetectorRef, |
| 5 | + Component, |
| 6 | + TemplateRef, |
| 7 | + ViewContainerRef, |
| 8 | +} from '@angular/core'; |
| 9 | +import { Subscribable, BehaviorSubject, Observable } from 'rxjs'; |
| 10 | +import { MockChangeDetectorRef } from './fixtures'; |
| 11 | +import { RX_RENDER_STRATEGIES_CONFIG } from '@rx-angular/cdk/render-strategies'; |
| 12 | +import { mockConsole } from '@test-helpers/rx-angular'; |
| 13 | + |
| 14 | +@Component({ |
| 15 | + template: ` |
| 16 | + <ng-container *rxLet="value$; let val"> |
| 17 | + <div>{{ val }}</div> |
| 18 | + </ng-container> |
| 19 | + `, |
| 20 | +}) |
| 21 | +class LetDirectiveTestComponent { |
| 22 | + value$: Subscribable<number> = new BehaviorSubject<number>(0); |
| 23 | +} |
| 24 | + |
| 25 | +let fixtureLetDirectiveTestComponent: any; |
| 26 | +let letDirectiveTestComponent: { |
| 27 | + strategy: string; |
| 28 | + value$: Observable<unknown> | unknown | undefined | null; |
| 29 | +}; |
| 30 | +let componentNativeElement: any; |
| 31 | + |
| 32 | +const setupLetDirectiveTestComponent = (): void => { |
| 33 | + TestBed.configureTestingModule({ |
| 34 | + declarations: [LetDirectiveTestComponent], |
| 35 | + imports: [RxLet], |
| 36 | + providers: [ |
| 37 | + { provide: ChangeDetectorRef, useClass: MockChangeDetectorRef }, |
| 38 | + TemplateRef, |
| 39 | + ViewContainerRef, |
| 40 | + { |
| 41 | + provide: RX_RENDER_STRATEGIES_CONFIG, |
| 42 | + useValue: { |
| 43 | + primaryStrategy: 'native', |
| 44 | + }, |
| 45 | + }, |
| 46 | + ], |
| 47 | + teardown: { destroyAfterEach: true }, |
| 48 | + }); |
| 49 | + fixtureLetDirectiveTestComponent = TestBed.createComponent( |
| 50 | + LetDirectiveTestComponent |
| 51 | + ); |
| 52 | + letDirectiveTestComponent = |
| 53 | + fixtureLetDirectiveTestComponent.componentInstance; |
| 54 | + componentNativeElement = fixtureLetDirectiveTestComponent.nativeElement; |
| 55 | +}; |
| 56 | + |
| 57 | +describe('RxLet Directive with Subscribable input', () => { |
| 58 | + beforeAll(() => mockConsole()); |
| 59 | + beforeEach(setupLetDirectiveTestComponent); |
| 60 | + |
| 61 | + it('should be instantiable', () => { |
| 62 | + expect(fixtureLetDirectiveTestComponent).toBeDefined(); |
| 63 | + expect(letDirectiveTestComponent).toBeDefined(); |
| 64 | + expect(componentNativeElement).toBeDefined(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should display value from Subscribable', fakeAsync(() => { |
| 68 | + letDirectiveTestComponent.value$ = 42; |
| 69 | + fixtureLetDirectiveTestComponent.detectChanges(); |
| 70 | + expect(componentNativeElement.textContent.trim()).toBe('42'); |
| 71 | + })); |
| 72 | +}); |
0 commit comments