I am encountering an issue with testing a simple function that I have created. Despite the fact that the function works correctly in practice, it is not being tested properly...
Explanation of how my function operates (While it functions as intended, the test outcome is incorrect)
- I input a string into the function
- If the input matches an element in my array,
- The function returns the string
- If there is no match with any array element
- The function returns the string 'default'
However, upon running the test provided, I encounter the following error:
Expected 'default' to equal 'hare-failure
Function Code
const state = [
{name: 'failure'}
];
isStatus(current): string {
for (const status of this.state) {
if (status.name === current) {
return current;
}
}
return 'default';
}
Test Case
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [EventComponent, ConfirmationComponent],
imports: [ReactiveFormsModule, FormsModule],
providers: []
});
fixture = TestBed.createComponent(EventComponent);
component = fixture.componentInstance;
component.ngOnInit();
}));
it('should return current status if it is part of exceptional statuses', () => {
const returned = component.isState('failure');
expect(returned).toEqual('failure');
});