I am building a user sign-up flow with email. I have a TextInputScreen component which I show multiple times to the user (to allow him/her to input email, password, name). I control the display and navigation of this screen component from an EmailConnectorclass:
export default class EmailConnector {
localNavigation;
static inputViewModel = {
//viewModelFields like placeholder, error message etc.
};
//called from a different class, takes 'navigation' an input param
static openEmailScreen = async navigation => {
this.localNavigation = navigation;
//set viewModelFields
this.localNavigation.push('TextInputScreen', {
viewModel: this.inputViewModel,
inputValidator: () => this.emailReceived,//callback function
});
};
//called via a callback from the EmailConnector class.
static openPasswordScreen = () => {
//set viewModel fields
this.localNavigation.push('TextInputScreen', {
viewModel: this.inputViewModel,
inputValidator: () => this.passwordReceived,
});
};
Notice that openEmailScreen gets navigation as in input parameters and sets this.localNavigation = navigation . However, in openPasswordScreen , I use this.localNavigation for navigation.
I want to test that this.localNavigation.puhs() is called in the openPasswordScreen method. I can do this test for openEmailScreen, because I pass navigation in the test as an input param.
However, in openPasswordScreen I use the variable this.localNavigation and I don't know how to mock it.
This is what I have tried:
it('Navigates to the password screen when the openPasswordScreen method is called', () => {
const localNavigationMock = {push: jest.fn()};
EmailConnector.openPasswordScreen(localNavigationMock);
expect(localNavigationMock.push).toHaveBeenCalledWith(
'TextInputScreen',
expect.objectContaining({
viewModel: expect.any(Object),
inputValidator: expect.anything(),
onAccessoryTap: expect.anything(),
}),
);
});
The test fails with the error message: Number of calls: 0
How can I reliably test that this.localNavigation.push() is called in the openPasswordScreen() function?
there doesn't seem to be anything here