123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- 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(
- <Assert key="system folders should exists" expect={resp} comparer={Comparer.exists} />,
- <Assert key="check properties"
- expect={resp}
- comparer={Comparer.hasProperties}
- actual={['DocumentDir', 'CacheDir', 'DCIMDir', 'DownloadDir']}
- />,
- <Info key="System Folders">
- <Text>{`${JSON.stringify(dirs)}`}</Text>
- </Info>
- )
- done()
- })
-
- })
-
- describe('ls API test', (report, done) => {
- fs.ls(dirs.DocumentDir).then((list) => {
- report(<Assert key="result must be an Array" expect={true} actual={Array.isArray(list)} />)
- return fs.ls('hh87h8uhi')
- })
- .then(()=>{})
- .catch((err) => {
- report(<Assert key="Wrong path should have error"
- expect={err}
- comparer={Comparer.exists}/>)
- done()
- })
- })
-
- describe('exists API test', (report, done) => {
- let exists = fs.exists
- exists(dirs.DocumentDir).then((exist, isDir) => {
- report(
- <Assert key="document dir should exist" expect={true} actual={exist}/>
- )
- return exists('blabajsdio')
- })
- .then((exist, isDir) => {
- report(
- <Assert key="path should not exist" expect={false} actual={exist}/>
- )
- 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(<Assert key="utf8 content test" expect={raw} actual={d}/>)
- 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(<Assert
- key="base64 content test"
- expect={raw}
- actual={d}/>)
- 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(<Assert
- key="ASCII content test"
- expect={raw}
- actual={d}/>)
- 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 + '/mkdir').then((err) => {
- report(<Assert key="folder should be created without error"
- expect={undefined}
- actual={err} />)
- return fs.isDir(p + '/mkdir')
- })
- .then((isDir) => {
- report(<Assert key="isDir should work correctly" expect={true} actual={isDir} />)
- return fs.mkdir(p + '/mkdir')
- })
- .then()
- .catch((err) => {
- report(<Assert key="isDir should not work when folder exists"
- expect={err}
- comparer={Comparer.hasValue}/>)
- 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(<Assert key="file created" expect={true} actual={exist} />)
- return fs.unlink(p).then(() => {
- return fs.exists(p)
- })
- })
- .then((exist) => {
- report(<Assert key="file removed" expect={false} actual={exist} />)
- return fs.mkdir(p + '/dir')
- })
- .then((err) => fs.exists(p + '/dir'))
- .then((exist) => {
- report(<Assert key="mkdir should success" expect={true} actual={exist} />)
- return fs.unlink(p + '/dir')
- })
- .then(() => fs.exists(p + '/dir'))
- .then((exist) => {
- report(<Assert key="folder should be removed" expect={false} actual={exist} />)
- 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(
- <Assert key="write data async test"
- expect={'123456789011121314'}
- actual={d1}/>)
- base64Test()
- })
- })
- function base64Test() {
- fs.writeStream(p, 'base64', false)
- .then((ws) => {
- for(let i = 0; i< 100; i++) {
- expect += RNFetchBlob.base64.encode(i)
- ws.write(RNFetchBlob.base64.encode(i))
- }
- return ws.close()
- })
- .then(() => {
- let rs = fs.readStream(p, 'base64')
- let d2 = ''
- rs.onData((chunk) => {
- d2 += chunk
- })
- rs.onEnd(() => {
- report(
- <Assert key="file should be overwritten by base64 encoded data"
- expect={expect}
- actual={RNFetchBlob.base64.decode(d2)} />)
- 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(<Assert key="file should not exist in old path" expect={false} actual={exist}/>)
- return fs.exists(dest + '/moved')
- })
- .then((exist) => {
- report(<Assert key="file should be moved to destination" expect={true} actual={exist}/>)
- return fs.ls(dest)
- })
- .then((files) => {
- report(<Assert key="file name should be correct" expect={'moved'} actual={files[0]}/>)
- let rs = fs.readStream(dest + '/moved')
- let actual = ''
- rs.onData((chunk) => {
- actual += chunk
- })
- rs.onEnd(() => {
- report(<Assert key="file content should be correct" expect={content} actual={actual}/>)
- 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(<Assert key="file should be copy to destination" expect={true} actual={exist}/>)
- return fs.ls(dest)
- })
- .then((files) => {
- report(<Assert key="file name should be correct" expect={'cp'} actual={files[0]}/>)
- let rs = fs.readStream(dest + '/cp')
- let actual = ''
- rs.onData((chunk) => {
- actual += chunk
- })
- rs.onEnd(() => {
- report(<Assert key="file content should be correct" expect={content} actual={actual}/>)
- done()
- })
- })
- })
|