Lack of data access when trying to test base form with angular 4 and ionic 3

I have a simple ionic form:

<ion-header>
    <ion-navbar>
        <ion-title>Foo</ion-title>
    </ion-navbar>
</ion-header>

<ion-content padding>
    <form [formGroup]="fooForm">
        <ion-item>
            <ion-label floating>Name</ion-label>
            <ion-input formControlName="name" type="text"></ion-input>
        </ion-item>
    </form>
    <button ion-button full color="primary" (click)="save()">Save</button>
</ion-content>

      

and the page component:

import { Component } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';

@Component({
    selector: 'fo-page-foo',
    templateUrl: './foo.html'
})
export class FooPage {
    fooForm: FormGroup;

    constructor(public formBuilder: FormBuilder) {
    }

    ngOnInit() {
        this.fooForm = this.formBuilder.group({
            name: ['']
        });

    }

    public save() {
        console.log(this.fooForm.value);
    }
}

      

and the test:

import { FooPage } from './foo';
import { TestBed, ComponentFixture } from '@angular/core/testing';
import { CUSTOM_ELEMENTS_SCHEMA, DebugElement } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from "@angular/forms";
import { Form } from 'ionic-angular';

describe('Foo Page:', () => {

    let comp: FooPage;
    let fixture: ComponentFixture<FooPage>;
    let de: DebugElement;

    beforeEach(() => {
        TestBed.configureTestingModule({
            schemas: [CUSTOM_ELEMENTS_SCHEMA],
            declarations: [FooPage],
            providers: [Form],
            imports: [FormsModule, ReactiveFormsModule],
        });
        fixture = TestBed.createComponent(FooPage);
        fixture.detectChanges();
        comp = fixture.componentInstance;
        de = fixture.debugElement;
        comp.ngOnInit();
    });

    describe('.constructor()', () => {
        it('Should be defined', () => {
            expect(comp).toBeDefined();
        });
    });
});

      

The project and testing structure is based on https://github.com/marcoturi/ionic-boilerplate . Test failure due to an error: No value accessor for form control

.

I've tried different approaches, but it seems that the combination of Ionic features and recent Angular 4 makes it require some additional configuration to be added, and some of the existing solutions are no longer valid due to changes in Angular.

+3


source to share


1 answer


I ran into this issue two days ago and thanks to isolating it for posting on SO was able to access the solution base for a slightly similar question

The main task is to add IonicModule

in imports

, but this triggers many other ionic imports:

import {
    App, Platform, Config, Keyboard,
    Form, IonicModule, DomController
} from 'ionic-angular';
import { 
    mockConfig, mockDomController, MockPlatform, mockPlatform 
} from 'ionic-angular/util/mock-providers';

      



so the updated one beforeEach

should start with

    TestBed.configureTestingModule({
        schemas: [CUSTOM_ELEMENTS_SCHEMA],
        declarations: [FooPage],
        providers: [
            App,
            Form,
            Keyboard,
            { provide: Platform, useClass: MockPlatform },
            { provide: DomController, useValue: mockDomController(mockPlatform()) },
            { provide: Config, useValue: mockConfig() },
        ],
        imports: [
            FormsModule,
            IonicModule,
            ReactiveFormsModule
        ],
    });

      

The PR with the above example is already merged with https://github.com/marcoturi/ionic-boilerplate .

+1


source







All Articles