Skip to content

Instantly share code, notes, and snippets.

@CHBaker
Created February 8, 2018 16:26
Show Gist options
  • Save CHBaker/a1f827db25aa1a7f41d37d15c71d79cf to your computer and use it in GitHub Desktop.
Save CHBaker/a1f827db25aa1a7f41d37d15c71d79cf to your computer and use it in GitHub Desktop.
Working test with FormBuilder passing in dynamically a ReactiveForm
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ReactiveFormsModule, FormBuilder } from '@angular/forms';
import { CommonModule } from '@angular/common';
import { AppModule } from './app.module';
import { ChildComponent } from './child.component';
describe('StaticComponent', () => {
let component: StaticComponent;
let fixture: ComponentFixture<ChildComponent>;
// create new instance of FormBuilder
const formBuilder: FormBuilder = new FormBuilder();
beforeEach(
async(() => {
TestBed.configureTestingModule({
declarations: [
ChildComponent
],
imports: [
CommonModule,
ReactiveFormsModule,
AppModule
],
providers: [
// reference the new instance of formBuilder from above
{ provide: FormBuilder, useValue: formBuilder }
]
}).compileComponents();
})
);
beforeEach(() => {
fixture = TestBed.createComponent(ChildComponent);
component = fixture.componentInstance;
// pass in the form dynamically
component.nameForm = formBuilder.group({
firstName: null,
lastName: null
});
fixture.detectChanges();
});
it('should be created', () => {
expect(component).toBeTruthy();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment