I want to mock/stub the resolve of a class method that returns a promise when testing another method of the same class. I'm using jest.
I have the following class Blah of which I want to test the foo() method:
export default class Blah {
foo () {
this.bar().then(result => {
// some logic here
})
}
bar () {
return new Promise(function (resolve, reject) {
// fetches some stuff over the network
resolve('hello world')
})
}
}
Because foo() uses bar() - a promise returning method that does stuff over the network - I would like to mock the resolve of bar(); let's say make it resolve what up.
Let's say the test would look something like this:
import Blah from './blah'
test('foo() - bar() resolves "what up"', () => {
const blah = new Blah()
// mock blah.bar() so that is resolves "what up"
// {some assertions on blah.foo() here}
})
It's my first time working with jest and I've read through the docs, but I'm still having a hard time wrapping my head around this case.
How can I mock the resolved value of bar() while testing foo()?