如何在玩笑中模拟AsyncStorage?

问题描述 投票:1回答:1

我正在使用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的“结果”?

unit-testing react-native jestjs enzyme asyncstorage
1个回答
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');
    });
});
© www.soinside.com 2019 - 2024. All rights reserved.