'Angular jasmine test private function
I have a private function mutateSectionIds() that deletes id key if it's value is negative, from an array with objects called sections[].
sections = [
{
id: 1,
},
{
id: -1,
},
]
makeTree(sections) {
this.mutateSectionIds(sections);
return this.mutateSectionIds;
}
private mutateSectionIds(sections) {
sections.forEach((section) => {
if (section.id < 0) {
delete section.id;
}
this.mutateSectionIds(sections);
});
}
Testing only the makeTree() function, how can I access the private mutateSectionIds(), and test if sections[1].id, which is a negative value, was deleted?
So far, I wrote this test without luck:
it('should mutate section ids', () => {
const key = sections[1].id;
const obj = [{ id: -1 }];
service.makeTree(sections);
expect(key).toBeLessThan(0);
expect(obj[0].id).toBeLessThan(0);
expect(sections[1].id).not.toContain(key);
})
EDIT:
I keep on trying and finally was able too made a solution by myself, but I have to test the private function directly using service['mutateSectionIds'](sections).
it('should mutate section ids', () => {
service['mutateSectionIds'](sections);
expect(sections[1].id).toBeUndefined;
});
If anyone knows how to achieve the same goal without testing directly the private function I would appreciate.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
