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.
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
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]
});
}
Just gonna comment here even though this has already been solved in case anyone else runs into the same issue.
The issue most likely arrises from the html file rather than the unit test or component itself.
Within your html file:
{variable}.name
*ngIf="{variable}"
This should check to see if this variable exists first. Then within your unit tests, simply do:
it('should create', ()=> {
expect(component).toBeTruthy();
}
If you want to then check to see if the component will load with a specified name
:
it('should create with specified name', () => {
component.{variable}.name = 'Foo'
fixture.detectChanges();
expect(fixture.debugElement.nativeElement.innerHTML)
.toContain(component.{variable}.name)
}
I am putting this update as an "answer" because it won't fit in a comment. :)
Thank you for posting the component logic. With this information I had enough to throw together a quick stackblitz testing environment here: https://stackblitz.com/edit/stackoverflow-q-53086352?file=app%2Fmy.component.spec.ts
I had to deviate slightly from your code above in the following ways:
However, the surprising thing was when I put your code in stackblitz - it all works just fine! So clearly the issue you are running into is being caused by some code that is outside of what you have presented so far.
My suggestion for next steps:
Good luck!
Your approach is good, I think it's failing because of the async. Try this :
it('should create', () => {
component.displayeddata = {
name: 'One component',
caption: 'Caption',
component: 'TextareaComponent'
};
fixture.detectChanges();
expect(component).toBeTruthy();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With