Trying to write a unit test where in my component, i update a value in a FormGroup in a FormArray. However, when running the test i am presented with the error:
Cannot Read Prop value of undefined
Running the app, the code compiles fine and the value is updated when user triggered on GUI. Error appears to link to this line:
_.find(this.myForm.value.settings, {task: 'Set Alarm}).visible = true;
Component:
nominatedArray = [];
myForm: FormGroup;
constructor(private fb: FormBuilder) {
this.myForm = this.fb.group({
title: ['title'],
settings: fb.array([
fb.group({
completed: fb.control(false),
task: fb.control('Set Alarm'),
visible: fb.control(false)
}),
fb.group({
completed: fb.control(false),
task: fb.control('Brush teeth'),
visible: fb.control(false)
}),
fb.group({
completed: fb.control(false),
task: fb.control('Shower'),
visible: fb.control(false)
})
])
})
}
public updateVis() {
// TEST IS FAILING AT 'VALUE' LINE BELOW
_.find(this.myForm.value.settings, {task: 'Set Alarm}).visible = true;
}
Test.spec:
it('should update value, () => {
component.myForm = this.fb.group({
title: ['title'],
settings: fb.array([
fb.group({
completed: fb.control(false),
task: fb.control('Set Alarm'),
visible: fb.control(false)
}),
fb.group({
completed: fb.control(false),
task: fb.control('Brush teeth'),
visible: fb.control(false)
}),
fb.group({
completed: fb.control(false),
task: fb.control('Shower'),
visible: fb.control(false)
})
])
})
component.updateVis();
expect(component.myForm.value.settings[0].visible).toBeFalsy();
});