I've got a ModalComponent which accepts some properties from parent via @Input.
That causes the problem with testing
TypeError: Cannot read property 'name' of undefined
Name is being used in ngOnInit and should come from @Input displayeddata.
How do I pass it in my unit test?
Currently my test looks so
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ModalComponent, FilterComponent],
imports: [ReactiveFormsModule, TranslateModule]
})
.compileComponents();
}));
beforeEach(async () => {
fixture = TestBed.createComponent(ModalComponent);
component = fixture.componentInstance;
component.displayeddata = {
name: 'One component',
caption: 'Caption',
componentName: 'TextareaComponent'
};
fixture.detectChanges();
});
it('should create', async () => {
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(component).toBeTruthy();
});
});
That is the one test for my component and fails.
Update #1
Here is what I get on console.log(this.displayeddata);
Object
caption: "Caption"
component: "TextareaComponent"
name: "One component"
I also changed my code a bit (updated code) and now getting a new error TypeError: Cannot read property 'caption' of undefined
Update #2
modal.component.ts
export class ModalComponent implements OnInit {
@Input() showenpunctsnow;
@Input() displayeddata;
@Output() ComponentParametrsInputs: FormGroup = this.fb.group({
name: ['', Validators.required],
caption: ['', Validators.required],
component: ['']
});
constructor(private fb: FormBuilder) {}
ngOnInit() {
console.log(this.displayeddata);
this.ComponentParametrsInputs = this.fb.group({
name: [this.displayeddata.name, Validators.required],
caption: [this.displayeddata.caption, Validators.required],
component: [this.displayeddata.componentName]
});
}