I would like to write a unit test to test if the expectedData is showing as expected. The HTML code is working and it displays everything correctly.
HTML code:
<div class="col-6 p-0 LHS" *ngIf="data && data.type === 'App'">
<div *ngFor="let d of data">
<div class="row">
<div class="col-auto">Item1:</div>
<div class="appItem1">{{d.item1}}</div>
</div>
</div>
</div>
TS file:
data = [{item1: "Item 1", item2: "Item 2"}, type: "App", display: true]; // sample input data
expectedData;
getExpectedData() {
if (this.data && this.data.item) {
switch (this.data.type) {
case ("File"):
this.expectedData = $('.fileItem1')[0].innerHTML;
break;
case ("App"):
this.expectedData = $('.appItem1')[0].innerHTML;
break;
}
}
}
My test case:
it('should verify expected data', () => {
const mockdata= [{item1: "Item 1"}];
mockdata.type= 'App'; // getting error because this can't be pushed to array
mockdata.display= true; // getting error because this can't be pushed to array
component.data = mockdata;
fixture.detectChanges();
component.getExpectedData();
expect(component.expectedData).toBe("Item 1");
});
However, in my test case, it doesn't seem to be working to mock the data to that type of data, are there any possible way to test the getExpectedData()?
Thanks

this.data.type?? This is how you access object's properties, not array elements.