Skip to content

Commit

Permalink
fix: trigger change detection cycle on rerender (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelss95 authored Nov 5, 2020
1 parent c42467d commit a53db0d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
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');
})

0 comments on commit a53db0d

Please sign in to comment.