vendredi 14 avril 2017

How to use setValue and patchValue in testing angular 2 reactive forms

I can't seem to get the correct dependencies in my spec file for testing a new form I've created.

I have 2 passing tests but it all comes to a hault when I use setValue or patchValue to setup my test form data.

import { TestBed, async } from '@angular/core/testing';
import { Component, ViewChild } from '@angular/core';
import { PostJobComponent } from './post-job.component';
import { NgForm, ReactiveFormsModule, FormGroup, FormControl, FormArray } from '@angular/forms';


describe('post-job component test: ', () => {

    beforeEach(() => {
        TestBed.configureTestingModule({
            declarations: [PostJobComponent],
            imports: [ReactiveFormsModule, BrowserAnimationsModule, MaterialModule.forRoot()],
            providers: [] // which provider for setValue/patchValue?
        });

        let fixture = TestBed.createComponent(PostJobComponent);
        let postJobComponent = fixture.debugElement.componentInstance;

    });


    it('sending form values to the service onSubmit method', () => {
        this.jobForm.patchValue({'title': 'a spec title'});
    });

Aucun commentaire:

Enregistrer un commentaire