|
| 1 | +import { CommonModule } from '@angular/common'; |
| 2 | +import { Component, NgModule } from '@angular/core'; |
| 3 | +import { MockBuilder, MockRender, ngMocks } from 'ng-mocks'; |
| 4 | +import { BehaviorSubject, Observable } from 'rxjs'; |
| 5 | + |
| 6 | +@Component({ |
| 7 | + selector: 'target', |
| 8 | + template: `<ng-container *ngIf="context.data$ | async as data">{{ |
| 9 | + data.value |
| 10 | + }}</ng-container>`, |
| 11 | +}) |
| 12 | +class TargetComponent { |
| 13 | + public context: |
| 14 | + | { |
| 15 | + data$: Observable<{ value: string }>; |
| 16 | + } |
| 17 | + | any; |
| 18 | +} |
| 19 | + |
| 20 | +@NgModule({ |
| 21 | + declarations: [TargetComponent], |
| 22 | + imports: [CommonModule], |
| 23 | +}) |
| 24 | +class TargetModule {} |
| 25 | + |
| 26 | +// The goal is to provide properties before the real render. |
| 27 | +// @see https://github.com/ike18t/ng-mocks/issues/919 |
| 28 | +describe('issue-919', () => { |
| 29 | + beforeEach(() => MockBuilder(TargetComponent, TargetModule)); |
| 30 | + |
| 31 | + it('allows to change the component before render', () => { |
| 32 | + const fixture = MockRender(TargetComponent, {}, false); |
| 33 | + fixture.point.componentInstance.context = { |
| 34 | + data$: new BehaviorSubject({ value: 'positive' }), |
| 35 | + }; |
| 36 | + fixture.detectChanges(); |
| 37 | + |
| 38 | + expect(ngMocks.formatText(fixture)).toEqual('positive'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('fails on render', () => { |
| 42 | + expect(() => MockRender(TargetComponent)).toThrowError(/data\$/); |
| 43 | + }); |
| 44 | +}); |
0 commit comments