Skip to content

Commit

Permalink
feat: add detectChanges function (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
GregOnNet authored and timdeschryver committed Sep 3, 2019
1 parent 3e7f485 commit 8a6fe17
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
9 changes: 8 additions & 1 deletion projects/testing-library/src/lib/models.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Type } from '@angular/core';
import { ComponentFixture } from '@angular/core/testing';
import { FireObject, Queries, queries, BoundFunction } from '@testing-library/dom';
import { BoundFunction, FireObject, Queries, queries } from '@testing-library/dom';
import { UserEvents } from './user-events';

export type RenderResultQueries<Q extends Queries = typeof queries> = { [P in keyof Q]: BoundFunction<Q[P]> };
Expand All @@ -21,6 +21,13 @@ export interface RenderResult extends RenderResultQueries, FireObject, UserEvent
* element: The to be printed HTML element, if not provided it will log the whole component's DOM
*/
debug: (element?: HTMLElement) => void;
/**
* @description
* Trigger a change detection cycle for the component.
*
* For more info see https://angular.io/api/core/testing/ComponentFixture#detectChanges
*/
detectChanges: () => void;
/**
* @description
* The Angular `ComponentFixture` of the component.
Expand Down
13 changes: 7 additions & 6 deletions projects/testing-library/src/lib/testing-library.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Component, OnInit, ElementRef, Type, DebugElement } from '@angular/core';
import { Component, DebugElement, ElementRef, OnInit, Type } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { NoopAnimationsModule, BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { TestBed, ComponentFixture } from '@angular/core/testing';
import { getQueriesForElement, prettyDOM, fireEvent, FireObject, FireFunction } from '@testing-library/dom';
import { RenderResult, RenderOptions } from './models';
import { createType, createSelectOptions } from './user-events';
import { BrowserAnimationsModule, NoopAnimationsModule } from '@angular/platform-browser/animations';
import { fireEvent, FireFunction, FireObject, getQueriesForElement, prettyDOM } from '@testing-library/dom';
import { RenderOptions, RenderResult } from './models';
import { createSelectOptions, createType } from './user-events';

@Component({ selector: 'wrapper-component', template: '' })
class WrapperComponent implements OnInit {
Expand Down Expand Up @@ -84,6 +84,7 @@ export async function render<T>(
fixture,
container: fixture.nativeElement,
debug: (element = fixture.nativeElement) => console.log(prettyDOM(element)),
detectChanges: () => fixture.detectChanges(),
...getQueriesForElement(fixture.nativeElement, queries),
...eventsWithDetectChanges,
type: createType(eventsWithDetectChanges),
Expand Down
43 changes: 43 additions & 0 deletions projects/testing-library/tests/detect-changes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Component, OnInit } from '@angular/core';
import { fakeAsync, tick } from '@angular/core/testing';
import { FormControl, ReactiveFormsModule } from '@angular/forms';
import { delay } from 'rxjs/operators';
import { render } from '../src/public_api';

@Component({
selector: 'fixture',
template: `
<input type="text" data-testid="input" [formControl]="inputControl" />
<button data-testid="button">{{ caption }}</button>
`,
})
class FixtureComponent implements OnInit {
inputControl = new FormControl();
caption = 'Button';

ngOnInit() {
this.inputControl.valueChanges.pipe(delay(400)).subscribe(() => (this.caption = 'Button updated after 400ms'));
}
}

describe('detectChanges', () => {
test('does not recognize change if execution is delayed', async () => {
const { getByTestId, type } = await render(FixtureComponent, { imports: [ReactiveFormsModule] });

type(getByTestId('input'), 'What a great day!');
expect(getByTestId('button').innerHTML).toBe('Button');
});

test('exposes detectChanges triggering a change detection cycle', fakeAsync(async () => {
const { getByTestId, type, detectChanges } = await render(FixtureComponent, {
imports: [ReactiveFormsModule],
});

type(getByTestId('input'), 'What a great day!');

tick(500);
detectChanges();

expect(getByTestId('button').innerHTML).toBe('Button updated after 400ms');
}));
});

0 comments on commit 8a6fe17

Please sign in to comment.