HARI PRASAD
HARI PRASAD

Reputation: 123

Issue with @input element failing default test case

relatively new to testing framework, facing below issue where I'm running 'npm run test' on a newly created spec.ts file with no extra test cases added

I'm facing issue that the map function used upon @input element is undefined

spec.ts

beforeEach(() => {
    fixture = TestBed.createComponent(InputComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    component.ngOnInit();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

InputComponent.ts

@Input() set column(colData: Columns[]) {
    this.columns = colData;
  }

ngOnInit(){
this.colName = this.columns.map(col => col.name);
}

TypeError: Cannot read property 'map' of undefined

Upvotes: 2

Views: 45

Answers (1)

Ahfa
Ahfa

Reputation: 180

You just need to input initial value of the variable and make sure it's array. because map is a property of array.

InputComponent.ts

columns = []; <-- initial value;

@Input() set column(colData: Columns[]) {
    this.columns = colData;
  }

ngOnInit(){
this.colName = this.columns.map(col => col.name); <-- You haven't declared the initial value of the "this.columns"
}

Upvotes: 1

Related Questions