1

I am using jest for unit testing of a react-native app. I have used AsyncStorage in one of my component.

componentWillMount() {
    this.setState({ avatarSource: null });
    AsyncStorage.getItem('UserDetails', (err, result) => {
      var objData = JSON.parse(result);
      this.setState({ userdetail: objData });
      axios.get(SERVER_URL + '/image/' + objData.userId + '.jpg' + '?$' + Math.random())
          .then(response => {
              var obj = { uri: response.config.url };
              this.setState({ avatarSource: obj });
      });
    });
}

How do I mock the 'result' from AsyncStorage in my test file?

skyboyer
  • 22,209
  • 7
  • 57
  • 64
komalV
  • 49
  • 2
  • 5

1 Answers1

0

The best way to test the get method is:

describe('Verifying getData function returns the right value', () => {
    it('Function getData must return the correct value given date', async function () {
        await getData('SOME_KEY');
        expect(AsyncStorage.getItem).toBeCalledWith('SOME_KEY');
    });
});
René Lazo
  • 651
  • 9
  • 7