I'm currently dealing with a problem related to unit testing a function. The function SOP3loginConfig
returns an object that contains another function called isSOP3
which returns a boolean value. I need to write tests for this specific function.
The code snippet for the function sop3login.ts
export const SOP3loginConfig = (props: IVariables) => {
const { i18n } = props;
return {
buttonLabel: props.user != null ? i18n('StartJourney') : i18n('logIn'),
loginLink:"/login?redirectUrl="+window.location.href,
isSOP3: async() => {
let userData = await ADCServices.getUserInfo();
if (!userData.session.tammUserInfo || userData.session.tammUserInfo.Type!="SOP3") {
props.SOP3toggleModal(props,true);
setTimeout(()=>props.SOP3toggleModal(props,false),5000)
return false;
} else {
return true;
}
},
};
};
The part of my integration in work.ts
import { SOP3loginConfig } from 'client/.../sop3login';
const start = async (props: IVariables) => {
if (props.user) {
if (await SOP3loginConfig(props).isSOP3()) {
props.history.push('/adc/card-renewal/customs');
}
} else {
props.history.push(SOP3loginConfig(props).loginLink);
}
};
My unit testing implementation in work.test.ts
describe('Testing if SOP3loginConfig should be called', () => {
it('Should call the SOP3 function', async () => {
props.user = true;
let SOP3loginConfig = jest.fn(props => {
return {
isSOP3: jest.fn(() => {
return true;
}),
};
});
functions.start(props);
expect(await SOP3loginConfig(props).isSOP3).toHaveBeenCalled();
expect(props.history.push).toHaveBeenCalled();
});
});
Error message I encountered
expect(jest.fn()).toHaveBeenCalled()
Expected number of calls: >= 1
Received number of calls: 0
97 | });
98 | functions.start(props);
> 99 | expect(await SOP3loginConfig(props).isSOP3).toHaveBeenCalled();
| ^
100 | expect(props.history.push).toHaveBeenCalled();
101 | });
102 | });
I just need to cover the section
inif (await SOP3loginConfig(props).isSOP3())
work.ts
.