diff --git a/libs/ng-mocks/src/lib/mock-render/mock-render.ts b/libs/ng-mocks/src/lib/mock-render/mock-render.ts index 5559626f7f..69e524b7d5 100644 --- a/libs/ng-mocks/src/lib/mock-render/mock-render.ts +++ b/libs/ng-mocks/src/lib/mock-render/mock-render.ts @@ -50,7 +50,7 @@ const fixtureFactory = (template: any, meta: Directive, params: any, flags: a template: mockTemplate, }; const fixture: any = generateFixture({ ...meta, params, options }); - if (flags.detectChanges) { + if (flags.detectChanges === undefined || flags.detectChanges) { fixture.detectChanges(); } diff --git a/tests/ng-mocks-render/cdr.spec.ts b/tests/ng-mocks-render/cdr.spec.ts new file mode 100644 index 0000000000..a667284730 --- /dev/null +++ b/tests/ng-mocks-render/cdr.spec.ts @@ -0,0 +1,63 @@ +import { Component, OnInit } from '@angular/core'; +import { MockBuilder, MockRender, ngMocks } from 'ng-mocks'; + +@Component({ + selector: 'target', + template: '{{ value }}', +}) +class TargetComponent implements OnInit { + public value = ''; + + public ngOnInit(): void { + this.value = 'rendered'; + } +} + +describe('ng-mocks-render:cdr', () => { + ngMocks.faster(); + beforeAll(() => MockBuilder(TargetComponent)); + + it('detects instantly w/o params and options', () => { + const fixture = MockRender(TargetComponent); + expect(ngMocks.formatText(fixture)).toEqual('rendered'); + }); + + it('detects instantly w/ params and w/o options', () => { + const fixture = MockRender(TargetComponent, {}); + expect(ngMocks.formatText(fixture)).toEqual('rendered'); + }); + + it('detects instantly w/ params and w/ options', () => { + const fixture = MockRender(TargetComponent, {}, {}); + expect(ngMocks.formatText(fixture)).toEqual('rendered'); + }); + + it('detects instantly w/ params and w/ flag', () => { + const fixture = MockRender( + TargetComponent, + {}, + { + detectChanges: true, + }, + ); + expect(ngMocks.formatText(fixture)).toEqual('rendered'); + }); + + it('does not detect as a flag', () => { + const fixture = MockRender(TargetComponent, undefined, false); + expect(ngMocks.formatText(fixture)).toEqual(''); + + fixture.detectChanges(); + expect(ngMocks.formatText(fixture)).toEqual('rendered'); + }); + + it('does not detect as options', () => { + const fixture = MockRender(TargetComponent, undefined, { + detectChanges: false, + }); + expect(ngMocks.formatText(fixture)).toEqual(''); + + fixture.detectChanges(); + expect(ngMocks.formatText(fixture)).toEqual('rendered'); + }); +});