Skip to content

fix: trigger change detection cycle on rerender #158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions projects/testing-library/src/lib/testing-library.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, Type, NgZone, SimpleChange, OnChanges, SimpleChanges } from '@angular/core';
import { ChangeDetectorRef, Component, Type, NgZone, SimpleChange, OnChanges, SimpleChanges } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { BrowserAnimationsModule, NoopAnimationsModule } from '@angular/platform-browser/animations';
Expand Down Expand Up @@ -117,7 +117,7 @@ export async function render<SutType, WrapperType = SutType>(
fixture.componentInstance.ngOnChanges(changes);
}

detectChanges();
fixture.componentRef.injector.get(ChangeDetectorRef).detectChanges();
};

const inject = TestBed.inject || TestBed.get;
Expand Down
23 changes: 22 additions & 1 deletion projects/testing-library/tests/rerender.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';
import { ChangeDetectionStrategy, Component, Input, OnChanges, SimpleChanges } from '@angular/core';
import { screen } from '@testing-library/dom';
import { render } from '../src/public_api';

@Component({
Expand Down Expand Up @@ -50,3 +51,23 @@ test('will call ngOnChanges on rerender', async () => {
component.getByText(name);
expect(nameChanged).toBeCalledWith(name, false);
})

@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
selector: 'fixture-onpush',
template: `
<div data-testid="number" [class.active]="activeField === 'number'">Number</div>
`,
})
class FixtureWithOnPushComponent {
@Input() activeField: string;
}

test('update properties on rerender', async () => {
const { rerender } = await render(FixtureWithOnPushComponent);
const numberHtmlElementRef = screen.queryByTestId('number');

expect(numberHtmlElementRef).not.toHaveClass('active');
rerender({ activeField: 'number' });
expect(numberHtmlElementRef).toHaveClass('active');
})