Skip to content

Commit a53db0d

Browse files
authored
fix: trigger change detection cycle on rerender (#158)
1 parent c42467d commit a53db0d

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

projects/testing-library/src/lib/testing-library.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, Type, NgZone, SimpleChange, OnChanges, SimpleChanges } from '@angular/core';
1+
import { ChangeDetectorRef, Component, Type, NgZone, SimpleChange, OnChanges, SimpleChanges } from '@angular/core';
22
import { ComponentFixture, TestBed } from '@angular/core/testing';
33
import { By } from '@angular/platform-browser';
44
import { BrowserAnimationsModule, NoopAnimationsModule } from '@angular/platform-browser/animations';
@@ -117,7 +117,7 @@ export async function render<SutType, WrapperType = SutType>(
117117
fixture.componentInstance.ngOnChanges(changes);
118118
}
119119

120-
detectChanges();
120+
fixture.componentRef.injector.get(ChangeDetectorRef).detectChanges();
121121
};
122122

123123
const inject = TestBed.inject || TestBed.get;

projects/testing-library/tests/rerender.spec.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component, Input, OnChanges, SimpleChanges } from '@angular/core';
2+
import { screen } from '@testing-library/dom';
23
import { render } from '../src/public_api';
34

45
@Component({
@@ -50,3 +51,23 @@ test('will call ngOnChanges on rerender', async () => {
5051
component.getByText(name);
5152
expect(nameChanged).toBeCalledWith(name, false);
5253
})
54+
55+
@Component({
56+
changeDetection: ChangeDetectionStrategy.OnPush,
57+
selector: 'fixture-onpush',
58+
template: `
59+
<div data-testid="number" [class.active]="activeField === 'number'">Number</div>
60+
`,
61+
})
62+
class FixtureWithOnPushComponent {
63+
@Input() activeField: string;
64+
}
65+
66+
test('update properties on rerender', async () => {
67+
const { rerender } = await render(FixtureWithOnPushComponent);
68+
const numberHtmlElementRef = screen.queryByTestId('number');
69+
70+
expect(numberHtmlElementRef).not.toHaveClass('active');
71+
rerender({ activeField: 'number' });
72+
expect(numberHtmlElementRef).toHaveClass('active');
73+
})

0 commit comments

Comments
 (0)