import RNTest from './react-native-testkit/' import React from 'react' import RNFetchBlob from 'react-native-fetch-blob' import { StyleSheet, Text, View, ScrollView, Dimensions, Image, } from 'react-native'; const { Assert, Comparer, Info, prop } = RNTest const fs = RNFetchBlob.fs const describe = RNTest.config({ group : 'fs', expand : false, run : true }) let { TEST_SERVER_URL, FILENAME, DROPBOX_TOKEN, styles, image } = prop() let dirs = null describe('Get storage folders', (report, done) => { fs.getSystemDirs().then((resp) => { dirs = resp report( , , {`${JSON.stringify(dirs)}`} ) done() }) }) describe('ls API test', (report, done) => { fs.ls(dirs.DocumentDir).then((list) => { report() return fs.ls('hh87h8uhi') }) .then(()=>{}) .catch((err) => { report() done() }) }) describe('exists API test', (report, done) => { let exists = fs.exists exists(dirs.DocumentDir).then((exist, isDir) => { report( ) return exists('blabajsdio') }) .then((exist, isDir) => { report( ) done() }) }) describe('create file API test', (report, done) => { let p = dirs.DocumentDir + '/test-' + Date.now() let raw = 'hello ' + Date.now() let base64 = RNFetchBlob.base64.encode(raw) fs.createFile(p, raw, 'utf8') .then(() => { let stream = fs.readStream(p, 'utf8') let d = '' stream.onData((chunk) => { d += chunk }) stream.onEnd(() => { report() testBase64() }) }) function testBase64() { fs.createFile(p + '-base64', RNFetchBlob.base64.encode(raw), 'base64') .then(() => { let stream = fs.readStream(p + '-base64', 'utf8') let d = '' stream.onData((chunk) => { d += chunk }) stream.onEnd(() => { report() testASCII() }) }) .catch((err) => { console.log(err) }) } function testASCII() { fs.createFile(p + '-ascii', raw, 'ascii') .then(() => { let stream = fs.readStream(p + '-ascii', 'ascii') let d = '' stream.onData((chunk) => { d += chunk }) stream.onEnd(() => { report() done() }) }) .catch((err) => { console.log(err) }) } }) describe('mkdir and isDir API test', (report, done) => { let p = dirs.DocumentDir + '/mkdir-test-' + Date.now() fs.mkdir(p).then((err) => { report() return fs.exists(p) }) .then((exist) => { report() return fs.isDir(p) }) .then((isDir) => { report() return fs.mkdir(p) }) .then() .catch((err) => { report() done() }) }) describe('unlink and mkdir API test', (report, done) => { let p = dirs.DocumentDir + '/unlink-test-' + Date.now() fs.createFile(p, 'write' + Date.now(), 'utf8').then(() => { return fs.exists(p) }) .then((exist) => { report() return fs.unlink(p).then(() => { return fs.exists(p) }) }) .then((exist) => { report() return fs.mkdir(p + '-dir') }) .then((err) => fs.exists(p + '-dir')) .then((exist) => { report() return fs.unlink(p + '-dir') }) .then(() => fs.exists(p + '-dir')) .then((exist) => { report() done() }) }) describe('write stream API test', (report, done) => { let p = dirs.DocumentDir + '/write-stream' + Date.now() let expect = '' fs.createFile(p, '1234567890', 'utf8') .then(() => fs.writeStream(p, 'utf8', true)) .then((ws) => { ws.write('11') ws.write('12') ws.write('13') ws.write('14') return ws.close() }) .then(() => { let rs = fs.readStream(p, 'utf8') let d1 = '' rs.onData((chunk) => { d1 += chunk }) rs.onEnd(() => { report( ) base64Test() }) }) function base64Test() { fs.writeStream(p, 'base64', false) .then((ws) => { for(let i = 0; i< 100; i++) { expect += String(i) } ws.write(RNFetchBlob.base64.encode(expect)) return ws.close() }) .then(() => { let rs = fs.readStream(p, 'base64') let d2 = '' rs.onData((chunk) => { d2 += chunk }) rs.onEnd(() => { console.log(RNFetchBlob.base64.encode(expect),d2) report( ) done() }) }) } }) describe('mv API test', {timeout : 10000},(report, done) => { let p = dirs.DocumentDir + '/mvTest' + Date.now() let dest = p + '-dest-' + Date.now() let content = Date.now() + '-test' fs.createFile(p, content, 'utf8') .then(() => fs.mkdir(dest)) .then(() => fs.mv(p, dest +'/moved')) .then(() => fs.exists(p)) .then((exist) => { report() return fs.exists(dest + '/moved') }) .then((exist) => { report() return fs.ls(dest) }) .then((files) => { report() let rs = fs.readStream(dest + '/moved') let actual = '' rs.onData((chunk) => { actual += chunk }) rs.onEnd(() => { report() done() }) }) }) describe('cp API test', {timeout : 10000},(report, done) => { let p = dirs.DocumentDir + '/cpTest' + Date.now() let dest = p + '-dest-' + Date.now() let content = Date.now() + '-test' fs.createFile(p, content, 'utf8') .then(() => fs.mkdir(dest)) .then(() => fs.cp(p, dest +'/cp')) .then(() => fs.exists(dest +'/cp')) .then((exist) => { report() return fs.ls(dest) }) .then((files) => { report() let rs = fs.readStream(dest + '/cp') let actual = '' rs.onData((chunk) => { actual += chunk }) rs.onEnd(() => { report() done() }) }) })