Skip to content

Commit

Permalink
Merge pull request #2099 from satanTime/issues/919
Browse files Browse the repository at this point in the history
tests(MockRender): covering change detection behavior #919
  • Loading branch information
satanTime authored Mar 19, 2022
2 parents d2db233 + a3a5aef commit 134249b
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tests/issue-919/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { CommonModule } from '@angular/common';
import { Component, NgModule } from '@angular/core';
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';
import { BehaviorSubject, Observable } from 'rxjs';

@Component({
selector: 'target',
template: `<ng-container *ngIf="context.data$ | async as data">{{
data.value
}}</ng-container>`,
})
class TargetComponent {
public context:
| {
data$: Observable<{ value: string }>;
}
| any;
}

@NgModule({
declarations: [TargetComponent],
imports: [CommonModule],
})
class TargetModule {}

// The goal is to provide properties before the real render.
// @see https://github.com/ike18t/ng-mocks/issues/919
describe('issue-919', () => {
beforeEach(() => MockBuilder(TargetComponent, TargetModule));

it('allows to change the component before render', () => {
const fixture = MockRender(TargetComponent, {}, false);
fixture.point.componentInstance.context = {
data$: new BehaviorSubject({ value: 'positive' }),
};
fixture.detectChanges();

expect(ngMocks.formatText(fixture)).toEqual('positive');
});

it('fails on render', () => {
expect(() => MockRender(TargetComponent)).toThrowError(/data\$/);
});
});

0 comments on commit 134249b

Please sign in to comment.