I am learning React Native and Jest testing.
I have a piece of code where I am storing some value in an AsyncStorage when a button is clicked. When the button is clicked I call the below method.
saveState = () => {
AsyncStorage.setItem('saveState', 'button clicked');
}
Now I would want to unit test the above method to see if my Async storage is working fine or not.
I was able to create a snapshot and also test remaining functionalities of the method but not this one. But I am not quite familiar in how to do so?
2nd edit ------
jest.mock('react-native', () => ({
setItem: jest.fn()
}));
describe('<MyComponent/>', () => {
let tnCScreen
let props
let instance
beforeEach(() => {
tnCScreen = shallow(<MyComponent {...props} />)
instance = tnCScreen.instance()
})
test('test saveState method is called', () => {
expect(instance.saveState()).toBeCalled
expect(AsyncStorage.setItem).toBeCalledWith('saveState', 'button clicked');
})
})
When I try the above I get below error
expect(received).toBeCalledWith(...expected)
Matcher error: received value must be a mock or spy function
Received has value: undefined