I am writing an integration test for a React application, i.e. A test that tests many components together and I want to mock any calls to external services.
The problem is that the test seems to run before the asynchronous callback is executed, which will cause my tests to fail.
Anyway, around? Can I somehow wait for the asynchronous code call to finish?
Here is some bad pseudo code to illustrate my point.
I would like to verify that when I mount Parent, its child component renders the content returned from the external service that I will mock.
class Parent extends component
{
render ()
{
<div>
<Child />
</div>
}
}
class Child extends component
{
DoStuff()
{
aThingThatReturnsAPromise().then((result) => {
Store.Result = result
})
}
render()
{
DoStuff()
return(<div>{Store.Result}</div>)
}
}
function aThingThatReturnsAPromise()
{
return new Promise(resolve =>{
eternalService.doSomething(function callback(result) {
resolve(result)
}
}
}
, , , .
jest.mock('eternalService', () => {
return jest.fn(() => {
return { doSomething: jest.fn((cb) => cb('fakeReturnValue');
});
});
describe('When rendering Parent', () => {
var parent;
beforeAll(() => {
parent = mount(<Parent />)
});
it('should display Child with response of the service', () => {
expect(parent.html()).toMatch('fakeReturnValue')
});
});
? , zonejs, React?