Skip to content

Commit

Permalink
feat: add a MockedComponent type
Browse files Browse the repository at this point in the history
The MockedComponent type allows you to extend an existing class with the __simulateChange and __simulateTouch methods.
  • Loading branch information
vespertilian authored and ike18t committed Aug 28, 2018
1 parent fba56d9 commit fe547af
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/mock-component/mock-component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Component } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';
import { MockComponent, MockComponents } from './mock-component';
import { MockComponent, MockComponents, MockedComponent } from './mock-component';
import { CustomFormControlComponent } from './test-components/custom-form-control.component';
import { EmptyComponent } from './test-components/empty-component.component';
import { SimpleComponent } from './test-components/simple-component.component';
Expand Down Expand Up @@ -126,14 +126,16 @@ describe('MockComponent', () => {
describe('ReactiveForms - ControlValueAccessor', () => {
it('should allow you simulate the component being touched', () => {
fixture.detectChanges();
const customFormControl = fixture.debugElement.query(By.css('custom-form-control')).componentInstance;
const customFormControl: MockedComponent<CustomFormControlComponent> =
fixture.debugElement.query(By.css('custom-form-control')).componentInstance;
customFormControl.__simulateTouch();
expect(component.formControl.touched).toBe(true);
});

it('should allow you simulate a value being set', () => {
fixture.detectChanges();
const customFormControl = fixture.debugElement.query(By.css('custom-form-control')).componentInstance;
const customFormControl: MockedComponent<CustomFormControlComponent>
= fixture.debugElement.query(By.css('custom-form-control')).componentInstance;
customFormControl.__simulateChange('foo');
expect(component.formControl.value).toBe('foo');
});
Expand Down
5 changes: 5 additions & 0 deletions lib/mock-component/mock-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import { directiveResolver } from '../common/reflect';

const cache = new Map<Type<Component>, Type<Component>>();

export type MockedComponent<T> = T & {
__simulateChange(value: any): void;
__simulateTouch(): void;
};

export function MockComponents<TComponent>(...components: Array<Type<TComponent>>): Array<Type<TComponent>> {
return components.map(MockComponent);
}
Expand Down

0 comments on commit fe547af

Please sign in to comment.