1import * as FS from 'expo-file-system'; 2import * as SMS from 'expo-sms'; 3 4async function assertExists(testFile, expectedToExist, expect) { 5 const { exists } = await FS.getInfoAsync(testFile.localUri); 6 if (expectedToExist) { 7 expect(exists).toBeTruthy(); 8 } else { 9 expect(exists).not.toBeTruthy(); 10 } 11} 12 13async function loadAndSaveFile(fileInfo, expect) { 14 await FS.deleteAsync(fileInfo.localUri, { idempotent: true }); 15 await assertExists(fileInfo, false, expect); 16 const { md5, headers } = await FS.downloadAsync(fileInfo.remoteUri, fileInfo.localUri, { 17 md5: true, 18 }); 19 expect(md5).toBe(fileInfo.md5); 20 await assertExists(fileInfo, true, expect); 21 expect(headers['Content-Type'] || headers['content-type']).toBe(fileInfo.mimeType); 22} 23 24async function cleanupFile(fileInfo, expect) { 25 await FS.deleteAsync(fileInfo.localUri); 26 await assertExists(fileInfo, false, expect); 27} 28 29const pngFile = { 30 localUri: FS.documentDirectory + 'image.png', 31 remoteUri: 'https://s3-us-west-1.amazonaws.com/test-suite-data/avatar2.png', 32 md5: '1e02045c10b8f1145edc7c8375998f87', 33 mimeType: 'image/png', 34}; 35 36const gifFile = { 37 localUri: FS.documentDirectory + 'image.gif', 38 remoteUri: 'https://upload.wikimedia.org/wikipedia/commons/2/2c/Rotating_earth_%28large%29.gif', 39 md5: '090592ebd01ac1e425b2766989040f80', 40 mimeType: 'image/gif', 41}; 42 43const audioFile = { 44 localUri: FS.documentDirectory + 'sound.mp3', 45 remoteUri: 'https://dl.espressif.com/dl/audio/gs-16b-1c-44100hz.mp3', 46 md5: 'e21d733c3506280974842f11c6d30005', 47 mimeType: 'audio/mpeg', 48}; 49 50const numbers = ['0123456789', '9876543210']; 51 52export async function loadAttachmentsAsync(expect) { 53 const files = [pngFile, gifFile, audioFile]; 54 await Promise.all(files.map((file) => loadAndSaveFile(file, expect))); 55} 56 57export async function cleanupAttachmentsAsync(expect) { 58 const files = [pngFile, gifFile, audioFile]; 59 await Promise.all(files.map((file) => cleanupFile(file, expect))); 60} 61 62export async function testSMSComposeWithSingleImageAttachment(expect) { 63 const contentUri = await FS.getContentUriAsync(pngFile.localUri); 64 await SMS.sendSMSAsync(numbers, 'test with image', { 65 attachments: { 66 uri: contentUri, 67 mimeType: 'image/png', 68 filename: 'image.png', 69 }, 70 }); 71} 72 73export async function testSMSComposeWithTwoImageAttachments(expect) { 74 await SMS.sendSMSAsync(numbers, 'test with two images', { 75 attachments: [ 76 { 77 uri: await FS.getContentUriAsync(gifFile.localUri), 78 mimeType: 'image/gif', 79 filename: 'image.gif', 80 }, 81 { 82 uri: await FS.getContentUriAsync(pngFile.localUri), 83 mimeType: 'image/png', 84 filename: 'image.png', 85 }, 86 ], 87 }); 88} 89 90export async function testSMSComposeWithAudioAttachment(expect) { 91 await SMS.sendSMSAsync(numbers, 'test with audio', { 92 attachments: [ 93 { 94 uri: await FS.getContentUriAsync(audioFile.localUri), 95 mimeType: 'audio/mpeg', 96 filename: 'sound.mp3', 97 }, 98 ], 99 }); 100} 101 102export async function testSMSComposeWithNullRecipient() { 103 await SMS.sendSMSAsync(null, 'test with null recipient, no attachments', null); 104} 105 106export async function testSMSComposeWithUndefinedRecipient() { 107 await SMS.sendSMSAsync(undefined, 'test with undefined recipient, no attachments', null); 108} 109