mardi 7 mars 2017

Set @Input() array inside test (undefined)

I have a component that gets an array of passengers from it's parent. Can't simulate that in test so I need to set that array manually.

Something like component.passengers = [...]

Inside component:

@Input() passengers: Passenger[];

  ngOnInit() {
    this.passengers.forEach((passenger) => {
      const firstNameControl: FormControl = new FormControl('', Validators.required);
      const lastNameControl: FormControl = new FormControl('', Validators.required);
      this.passengersEditForm.addControl('nameFirst' + this.passengers.indexOf(passenger), firstNameControl);
      this.passengersEditForm.addControl('nameLast' + this.passengers.indexOf(passenger), lastNameControl);
    });
  }

Tried to do something like:

  beforeEach(() => {
    fixture = TestBed.createComponent(PassengersComponent);
    component = fixture.componentInstance;
    component.passengers = [something here of Passenger type]
    fixture.detectChanges();
  });

but I get two errors: 1. Cannot set property 'passengers' of undefined 2. Error in :0:0 caused by: Cannot read property 'forEach' of undefined

It looks like passing mocked array in beforeEach fails. How to do that?

Aucun commentaire:

Enregistrer un commentaire