Reputation: 676
I am trying to write unit test for my component method i.e populateSprintDropDown. Here is my component code
setResponseForButton() {
let sortedList = this.teamboardService.sortSprintsBasedOnEndDate();
this.populateSprintDropDown(sortedList);
}
//finalSprintList that is returned by service is = [{id: "3712", name: "DCT 3.128.1 Sp1 (11Dec-24Dec)",
status: "ACTIVE", sprintLink: null, startDate: "2019-12-11"}]
populateSprintDropDown(finalSprintList: Array<any>) {
this.sprintsList = (<Array<Sprint>>finalSprintList).map(sprint => ({ item_id: sprint.id, item_text: sprint.name }));
}
Test:
it('populate sprint in sprint dropdown upon button click', () => {
let list = [{item_id: "3712", item_text: "DCT 3.128.1 Sp1 (11Dec-24Dec)"}]
component.sprintsList = list;
const spy = spyOn(component, 'populateSprintDropDown').and.callThrough();
component.setResponseForButton();
expect(spy).toBeDefined();
expect(spy).toHaveBeenCalled();
});
But when I run my test I get this error:
TypeError: finalSprintsList.map is not a function. I cannot understand what does it mean and how to resolve it.
Upvotes: 0
Views: 4320
Reputation: 26190
You should mock the method teamboardService.sortSprintsBasedOnEndDate
.
Please note that no spy is needed for method component.populateSprintDropDown
since you're able to check the value component.sprintsList
that is updated by this method.
Try to rewrite your test as follows:
it('dropdown upon button click should update sprintsList', () => {
// given
const sortedList = [{
id: "3712",
name: "DCT 3.128.1 Sp1 (11Dec-24Dec)",
status: "ACTIVE",
sprintLink: null,
startDate: "2019-12-11"
}];
spyOn(component.teamboardService, 'sortSprintsBasedOnEndDate').and.returnValue(sortedList);
// when
component.setResponseForButton();
// then
const expected = [{item_id: "3712", item_text: "DCT 3.128.1 Sp1 (11Dec-24Dec)"}];
expect(component.sprintsList).toEqual(expected );
});
Upvotes: 1