dimanche 6 janvier 2019

How to set NgForm value in Observable and test that it was set?

I'm trying to test NgForm and check if form's value is set when store's state is updated.

@ViewChild('form') form: NgForm;

ngOnInit() {
this.subscription = this.store.select('shoppingList').subscribe(data => {
    this.editedItem = data.editedIngredient;
    this.form.setValue({ 
      name: this.editedItem.name,
      amount: this.editedItem.amount
    })
});

}

But when setting the value I'm getting

There are no form controls registered with this group yet.  
If you're using ngModel, you may want to check next tick (e.g. use setTimeout).

Also tried to create fake form and set it instead of NgForm

TestBed.createComponent(ShoppingEditComponent).debugElement.componentInstance.form = { value: {}, setValue: (newValue) => { this.value = newValue }};

but in the test it's value is never updated, getting empty value object.

What's the best approach to testing such case?

Aucun commentaire:

Enregistrer un commentaire