如何在开玩笑中模拟AsyncStorage?

时间:2018-06-06 05:48:34

标签: unit-testing react-native jestjs enzyme asyncstorage

我正在使用jest进行反应原生应用的单元测试。我在我的一个组件中使用了AsyncStorage。

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 });
      });
    });
}

如何在我的测试文件中模拟AsyncStorage的“结果”?

1 个答案:

答案 0 :(得分:0)

测试get方法的最佳方法是:

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');
    });
});
相关问题