user1015388
user1015388

Reputation: 1515

How to mock an @INPUT value in tests for angular component

In my angular app, I am writing a testcase for a component which has @Input value. How can I mock the @Input value. The main component's testSave() method uses subComponent's InputObject's id. When I run the testcase it says undefined is an object at 'this.subComponent.InputObject.id;'

export class SubComponent implements OnInit {
  @Input() inputObj: InputObject;
}

export class MainComponent implements OnInit {
  @Input() subComponent: SubComponent;

  testsave() {
  this.subComponent.InputObject.id;
  }
}

export class InputObject {
  contructor(id:string, name: string)
}

TestCase:

 it('should save', fakeAsync(()  => {
//     const event: MockEvent = new MockEvent();
//     fixture = TestBed.createComponent(MainComponent);
//     component = fixture.componentInstance;
 });

Upvotes: 3

Views: 7797

Answers (1)

Buczkowski
Buczkowski

Reputation: 2416

Since subComponent and inputObj are public you can simply override it, if you test MainComponent you can have something like:

component.subComponent = {inputObj: {id: 'someId', name :'someName'}};

Upvotes: 6

Related Questions