I am writting test case for a module called SendData.js. In this module, we used expo-file-system and expo-mail-composer to implement email sending on an iOS device. Here is the sending funciton in 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);
}
And here is my testing code. I am wondering why FileSystem.readAsStringAsync is not called, any helps would be great. Thanks!
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();
})
});
Aucun commentaire:
Enregistrer un commentaire