Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native React Native中的Jest和异步存储_React Native_Jestjs_Tdd - Fatal编程技术网

React native React Native中的Jest和异步存储

React native React Native中的Jest和异步存储,react-native,jestjs,tdd,React Native,Jestjs,Tdd,我正在学习TDD,并尝试按照 我已经完成了安装,目前正在第一次测试中尝试模拟异步存储 我创建了一个简单的测试 import React from 'react'; import {render, fireEvent} from 'react-native-testing-library'; import UserWelcome from '../UserWelcome'; import AsyncStorage from '@react-native-async-storage/async-st

我正在学习TDD,并尝试按照 我已经完成了安装,目前正在第一次测试中尝试模拟异步存储

我创建了一个简单的测试

import React from 'react';
import {render, fireEvent} from 'react-native-testing-library';
import UserWelcome from '../UserWelcome';
import AsyncStorage from '@react-native-async-storage/async-storage';

describe('UserWelcome', () => {
  describe('User enters a name and stores in async local storage', () => {
    const firstTimeUser = 'user001';
    let getByTestId;

    beforeEach(() => {
      ({getByTestId} = render(<UserWelcome />));

      fireEvent.changeText(getByTestId('username'), firstTimeUser);
      fireEvent.press(getByTestId('submitUsername'));
    });

    it('checks if Async Storage is used', async () => {
      await asyncOperationOnAsyncStorage();

      expect(AsyncStorage.getItem).toBeCalledWith('currentUser');
    });
  });
});

我想你误解了文件。asyncOperationOnAsyncStorage只是文档中定义的一个示例方法,您必须将asyncOperationOnAsyncStorage替换为具有某种异步存储操作的方法

因此,本质上asyncOperationOnAsyncStorage将是包含异步存储逻辑的方法

asyncOperationOnAsyncStorage = () => { 
   await AsyncStorage.setItem('currentUser', value)
}
你会开玩笑地测试它

asyncOperationOnAsyncStorage = () => { 
   await AsyncStorage.setItem('currentUser', value)
}