I would like to create a test with Jest to make sure there is an if statement in my code. Let’s say we have a function that receives a string and I need to verify if this string includes something in it, if there is execute another function otherwise just ignore it.
function create (str) {
if (str.includes('something')) {
otherFunction()
}
...
}
So I want not just to make sure there is an if statement, I also want that the string “calling” includes
is str
. I know I can use String.prototype
but it does not make sure the string “calling” includes
is str
, any other string calling the same function passes the test
What I have till now:
test('Should call otherFunction if str includes something', () => {
const { sut, otherObj } = makeSut()
const otherFunctionSpy = jest.spyOn(otherObj, 'otherFunction')
const includesSpy = jest.spyOn(String.prototype, 'includes')
sut.create('has_something')
expect(includesSpy).toHaveBeenCalledWith('something')
expect(otherFunctionSpy).toHaveBeenCalled()
})