How can I effectively test the forEach loop in jasmine with karma? Below is the code for the component:
getData(studentsList:any, day:any, game:any){
let strength:any;
let location:string;
if(studentsList){
studentsList.forEach( value => {
strength=value.schedule[day][game].strength;
location=value.schedule[day][game].location;
});
}
}
The data within studentsList is as follows:
(Edit:- The data has been updated and appears like this)
[{
"activityName": "tournament1",
"schedule": {
"first": {
"Baseball": {
"strength": 23,
"location": "abc"
}
},
"second": {
"Cricket": {
"strength": 20,
"location": "bcd"
}
},
"third": {
"Football": {
"strength": 19,
"location": "cde"
}
}
}
},
{
"activityName": "tournament2",
"schedule": {
"first": {
"Baseball": {
"strength": 23,
"location": "abc"
}
},
"second": {
"Cricket": {
"strength": 20,
"location": "bcd"
}
},
"third": {
"Football": {
"strength": 19,
"location": "cde"
}
}
}
},
{
"activityName": "tournament3",
"schedule": {
"first": {
"Baseball": {
"strength": 23,
"location": "abc"
}
},
"second": {
"Cricket": {
"strength": 20,
"location": "bcd"
}
},
"third": {
"Football": {
"strength": 19,
"location": "cde"
}
}
}
}]
Here's the test that I attempted:
it('should validate getData()', () => {
fixture = TestBed.createComponent(GameComponent);
const app = fixture.debugElement.componentInstance;
const data={
"activityName": "tournament",
"schedule": {
"first": {
"Baseball": {
"strength": 23,
"location": "abc"
}
},
"second": {
"Cricket": {
"strength": 20,
"location": "bcd"
}
},
"third": {
"Football": {
"strength": 19,
"location": "cde"
}
}
}
}
app.getData(data, 'first', 'Baseball');
fixture.detectChanges();
expect(app.location).toContain('abc');
expect(app.location).toContain('bcd');
expect(app.location).toContain('cde');
}
However, I am consistently receiving a 'forEach is not a function' error. Should I explore another approach instead of using static data in the test?