暫無描述

test-fs.js 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. import RNTest from './react-native-testkit/'
  2. import React from 'react'
  3. import RNFetchBlob from 'react-native-fetch-blob'
  4. import {
  5. StyleSheet,
  6. Text,
  7. View,
  8. ScrollView,
  9. Dimensions,
  10. Image,
  11. } from 'react-native';
  12. const { Assert, Comparer, Info, prop } = RNTest
  13. const fs = RNFetchBlob.fs
  14. const describe = RNTest.config({
  15. group : 'fs',
  16. expand : false,
  17. run : true
  18. })
  19. let { TEST_SERVER_URL, FILENAME, DROPBOX_TOKEN, styles, image } = prop()
  20. let dirs = null
  21. describe('Get storage folders', (report, done) => {
  22. fs.getSystemDirs().then((resp) => {
  23. dirs = resp
  24. report(
  25. <Assert key="system folders should exists" expect={resp} comparer={Comparer.exists} />,
  26. <Assert key="check properties"
  27. expect={resp}
  28. comparer={Comparer.hasProperties}
  29. actual={['DocumentDir', 'CacheDir', 'DCIMDir', 'DownloadDir']}
  30. />,
  31. <Info key="System Folders">
  32. <Text>{`${JSON.stringify(dirs)}`}</Text>
  33. </Info>
  34. )
  35. done()
  36. })
  37. })
  38. describe('ls API test', (report, done) => {
  39. fs.ls(dirs.DocumentDir).then((list) => {
  40. report(<Assert key="result must be an Array" expect={true} actual={Array.isArray(list)} />)
  41. return fs.ls('hh87h8uhi')
  42. })
  43. .then(()=>{})
  44. .catch((err) => {
  45. report(<Assert key="Wrong path should have error"
  46. expect={err}
  47. comparer={Comparer.exists}/>)
  48. done()
  49. })
  50. })
  51. describe('exists API test', (report, done) => {
  52. let exists = fs.exists
  53. exists(dirs.DocumentDir).then((exist, isDir) => {
  54. report(
  55. <Assert key="document dir should exist" expect={true} actual={exist}/>
  56. )
  57. return exists('blabajsdio')
  58. })
  59. .then((exist, isDir) => {
  60. report(
  61. <Assert key="path should not exist" expect={false} actual={exist}/>
  62. )
  63. done()
  64. })
  65. })
  66. describe('create file API test', (report, done) => {
  67. let p = dirs.DocumentDir + '/test-' + Date.now()
  68. let raw = 'hello ' + Date.now()
  69. let base64 = RNFetchBlob.base64.encode(raw)
  70. fs.createFile(p, raw, 'utf8')
  71. .then(() => {
  72. let stream = fs.readStream(p, 'utf8')
  73. let d = ''
  74. stream.onData((chunk) => {
  75. d += chunk
  76. })
  77. stream.onEnd(() => {
  78. report(<Assert key="utf8 content test" expect={raw} actual={d}/>)
  79. testBase64()
  80. })
  81. })
  82. function testBase64() {
  83. fs.createFile(p + '-base64', RNFetchBlob.base64.encode(raw), 'base64')
  84. .then(() => {
  85. let stream = fs.readStream(p + '-base64', 'utf8')
  86. let d = ''
  87. stream.onData((chunk) => {
  88. d += chunk
  89. })
  90. stream.onEnd(() => {
  91. report(<Assert
  92. key="base64 content test"
  93. expect={raw}
  94. actual={d}/>)
  95. testASCII()
  96. })
  97. })
  98. .catch((err) => {
  99. console.log(err)
  100. })
  101. }
  102. function testASCII() {
  103. fs.createFile(p + '-ascii', raw, 'ascii')
  104. .then(() => {
  105. let stream = fs.readStream(p + '-ascii', 'ascii')
  106. let d = ''
  107. stream.onData((chunk) => {
  108. d += chunk
  109. })
  110. stream.onEnd(() => {
  111. report(<Assert
  112. key="ASCII content test"
  113. expect={raw}
  114. actual={d}/>)
  115. done()
  116. })
  117. })
  118. .catch((err) => {
  119. console.log(err)
  120. })
  121. }
  122. })
  123. describe('mkdir and isDir API test', (report, done) => {
  124. let p = dirs.DocumentDir + '/mkdir-test-' + Date.now()
  125. fs.mkdir(p + '/mkdir').then((err) => {
  126. report(<Assert key="folder should be created without error"
  127. expect={undefined}
  128. actual={err} />)
  129. return fs.isDir(p + '/mkdir')
  130. })
  131. .then((isDir) => {
  132. report(<Assert key="isDir should work correctly" expect={true} actual={isDir} />)
  133. return fs.mkdir(p + '/mkdir')
  134. })
  135. .then()
  136. .catch((err) => {
  137. report(<Assert key="isDir should not work when folder exists"
  138. expect={err}
  139. comparer={Comparer.hasValue}/>)
  140. done()
  141. })
  142. })
  143. describe('unlink and mkdir API test', (report, done) => {
  144. let p = dirs.DocumentDir + '/unlink-test-' + Date.now()
  145. fs.createFile(p, 'write' + Date.now(), 'utf8').then(() => {
  146. return fs.exists(p)
  147. })
  148. .then((exist) => {
  149. report(<Assert key="file created" expect={true} actual={exist} />)
  150. return fs.unlink(p).then(() => {
  151. return fs.exists(p)
  152. })
  153. })
  154. .then((exist) => {
  155. report(<Assert key="file removed" expect={false} actual={exist} />)
  156. return fs.mkdir(p + '/dir')
  157. })
  158. .then((err) => fs.exists(p + '/dir'))
  159. .then((exist) => {
  160. report(<Assert key="mkdir should success" expect={true} actual={exist} />)
  161. return fs.unlink(p + '/dir')
  162. })
  163. .then(() => fs.exists(p + '/dir'))
  164. .then((exist) => {
  165. report(<Assert key="folder should be removed" expect={false} actual={exist} />)
  166. done()
  167. })
  168. })
  169. describe('write stream API test', (report, done) => {
  170. let p = dirs.DocumentDir + '/write-stream' + Date.now()
  171. let expect = ''
  172. fs.createFile(p, '1234567890', 'utf8')
  173. .then(() => fs.writeStream(p, 'utf8', true))
  174. .then((ws) => {
  175. ws.write('11')
  176. ws.write('12')
  177. ws.write('13')
  178. ws.write('14')
  179. return ws.close()
  180. })
  181. .then(() => {
  182. let rs = fs.readStream(p, 'utf8')
  183. let d1 = ''
  184. rs.onData((chunk) => {
  185. d1 += chunk
  186. })
  187. rs.onEnd(() => {
  188. report(
  189. <Assert key="write data async test"
  190. expect={'123456789011121314'}
  191. actual={d1}/>)
  192. base64Test()
  193. })
  194. })
  195. function base64Test() {
  196. fs.writeStream(p, 'base64', false)
  197. .then((ws) => {
  198. for(let i = 0; i< 100; i++) {
  199. expect += RNFetchBlob.base64.encode(i)
  200. ws.write(RNFetchBlob.base64.encode(i))
  201. }
  202. return ws.close()
  203. })
  204. .then(() => {
  205. let rs = fs.readStream(p, 'base64')
  206. let d2 = ''
  207. rs.onData((chunk) => {
  208. d2 += chunk
  209. })
  210. rs.onEnd(() => {
  211. report(
  212. <Assert key="file should be overwritten by base64 encoded data"
  213. expect={expect}
  214. actual={RNFetchBlob.base64.decode(d2)} />)
  215. done()
  216. })
  217. })
  218. }
  219. })
  220. describe('mv API test', {timeout : 10000},(report, done) => {
  221. let p = dirs.DocumentDir + '/mvTest' + Date.now()
  222. let dest = p + '-dest-' + Date.now()
  223. let content = Date.now() + '-test'
  224. fs.createFile(p, content, 'utf8')
  225. .then(() => fs.mkdir(dest))
  226. .then(() => fs.mv(p, dest +'/moved'))
  227. .then(() => fs.exists(p))
  228. .then((exist) => {
  229. report(<Assert key="file should not exist in old path" expect={false} actual={exist}/>)
  230. return fs.exists(dest + '/moved')
  231. })
  232. .then((exist) => {
  233. report(<Assert key="file should be moved to destination" expect={true} actual={exist}/>)
  234. return fs.ls(dest)
  235. })
  236. .then((files) => {
  237. report(<Assert key="file name should be correct" expect={'moved'} actual={files[0]}/>)
  238. let rs = fs.readStream(dest + '/moved')
  239. let actual = ''
  240. rs.onData((chunk) => {
  241. actual += chunk
  242. })
  243. rs.onEnd(() => {
  244. report(<Assert key="file content should be correct" expect={content} actual={actual}/>)
  245. done()
  246. })
  247. })
  248. })
  249. describe('cp API test', {timeout : 10000},(report, done) => {
  250. let p = dirs.DocumentDir + '/cpTest' + Date.now()
  251. let dest = p + '-dest-' + Date.now()
  252. let content = Date.now() + '-test'
  253. fs.createFile(p, content, 'utf8')
  254. .then(() => fs.mkdir(dest))
  255. .then(() => fs.cp(p, dest +'/cp'))
  256. .then(() => fs.exists(dest +'/cp'))
  257. .then((exist) => {
  258. report(<Assert key="file should be copy to destination" expect={true} actual={exist}/>)
  259. return fs.ls(dest)
  260. })
  261. .then((files) => {
  262. report(<Assert key="file name should be correct" expect={'cp'} actual={files[0]}/>)
  263. let rs = fs.readStream(dest + '/cp')
  264. let actual = ''
  265. rs.onData((chunk) => {
  266. actual += chunk
  267. })
  268. rs.onEnd(() => {
  269. report(<Assert key="file content should be correct" expect={content} actual={actual}/>)
  270. done()
  271. })
  272. })
  273. })