为什么不调用readAsStringAsync?

时间:2019-10-26 05:26:28

标签: react-native testing mocking jestjs expo

我正在为名为SendData.js的模块编写测试用例。在此模块中,我们使用了expo-file-system和expo-mail-composer在iOS设备上实现电子邮件发送。这是SendData.js中的发送功能

static sendDataEmail = () => {

        // Get all the contents in the root directory of this app's file
        const directory = FileSystem.readDirectoryAsync(FileSystem.documentDirectory);
        directory.then(
            // Return the app's file
            function(result) {

                result.forEach(function(element, index){
                    result[index] = FileSystem.documentDirectory + result[index];
                });

                FileSystem.readAsStringAsync(FileSystem.documentDirectory + "NewUserQuestions.json").then(
                    function(result1) {

                        result1 = JSON.parse(result1)['userID']

                        let mailOptions =  {
                            recipients: ['cpc.qantas@sydney.edu.au'],
                            subject: 'JetSet Results UserID:' + result1,
                            attachments: result,
                        }

                        // Open email client
                        MailComposer.composeAsync(mailOptions);
                    }
                )

            },
            // Failed to extract files from the root directory
            function(err){
                console.log(err);
            }

这是我的测试代码。我想知道为什么不调用FileSystem.readAsStringAsync,任何帮助都很好。谢谢!

import SendData from "../app/resources/SendData"
import * as MailComposer from 'expo-mail-composer';
import * as FileSystem from 'expo-file-system';

jest.mock("../node_modules/expo-file-system/src/FileSystem");
jest.mock("../node_modules/expo-mail-composer/src/MailComposer");

const localDirectory = "../__fakeFiles__";
FileSystem.documentDirectory = localDirectory;


describe("This is test suits for SendData", ()=> {
  it("sendDataEmail() is tested.", async ()=>{
    await SendData.sendDataEmail();
    expect(FileSystem.readDirectoryAsync).toHaveBeenCalled();
    expect(FileSystem.readAsStringAsync).toHaveBeenCalled();
    expect(MailComposer.composeAsync).toHaveBeenCalled();
  })
});

0 个答案:

没有答案