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 = RNFetchBlob.fs.dirs describe('Get storage folders', (report, done) => { 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 d = '' fs.readStream(p, 'utf8').then((stream) => { stream.open() stream.onData((chunk) => { d += chunk }) stream.onEnd(() => { report() testBase64() }) }) }) function testBase64() { fs.createFile(p + '-base64', RNFetchBlob.base64.encode(raw), 'base64') .then(() => { fs.readStream(p + '-base64', 'utf8').then((stream) => { stream.open() 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 d1 = '' fs.readStream(p, 'utf8').then((stream) => { stream.open() stream.onData((chunk) => { d1 += chunk }) stream.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(() => { return fs.readStream(p, 'base64') }) .then((stream) => { let d2 = '' stream.open() stream.onData((chunk) => { d2 += chunk }) stream.onEnd(() => { 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() fs.readStream(dest + '/moved').then((rs) => { rs.open() 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() fs.readStream(dest + '/cp').then((rs) => { rs.open() let actual = '' rs.onData((chunk) => { actual += chunk }) rs.onEnd(() => { report() done() }) }) }) }) describe('ASCII data test', (report, done) => { let p = dirs.DocumentDir + '/ASCII-test-' + Date.now() let expect = 'fetch-blob-'+Date.now() fs.createFile(p, 'utf8') .then(() => { return fs.writeStream(p, 'ascii', false) }) .then((ofstream) => { for(let i=0;i { fs.readStream(p, 'ascii').then((ifstream) => { let res = [] ifstream.open() ifstream.onData((chunk) => { res = res.concat(chunk) }) ifstream.onEnd(() => { res = res.map((byte) => { return String.fromCharCode(byte) }).join('') report( ) done() }) }) }) }) describe('ASCII file test', (report, done) => { let p = dirs.DocumentDir + '/' let filename = '' let expect = [] let base64 = RNFetchBlob.base64 filename = 'ASCII-file-test' + Date.now() + '.txt' expect = 'ascii test ' + Date.now() fs.createFile(p + filename, getASCIIArray(expect), 'ascii') .then(() => { fs.readStream(p + filename, 'base64').then((rs) => { let actual = '' rs.open() rs.onData((chunk) => { actual += chunk }) rs.onEnd(() => { report() done() }) }) }) }) describe('format conversion', (report, done) => { let p = dirs.DocumentDir + '/foo-' + Date.now() fs.createFile(p, [102, 111, 111], 'ascii') .then(() => { fs.readStream(p, 'utf8').then((stream) => { let res = [] stream.open() stream.onData((chunk) => { res+=chunk }) stream.onEnd(() => { report( ) done() }) }) }) }) describe('stat and lstat test', (report, done) => { let p = dirs.DocumentDir + '/' + 'ls-stat-test' + Date.now() let file = null fs.lstat(dirs.DocumentDir) // stat a folder .then((stat) => { report( ) file = stat[0].path return fs.stat(file) }) .then((stat) => { report( ) return fs.stat('13123132') }) .then(()=>{}) .catch((err) => { console.log(err) report() done() }) .then(()=>{}) .catch((err) => { console.log(err) report() done() }) }) function getASCIIArray(str) { let r = [] for(let i=0;i