Нема описа

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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).then((err) => {
  126. report(<Assert key="folder should be created without error"
  127. expect={undefined}
  128. actual={err} />)
  129. return fs.exists(p)
  130. })
  131. .then((exist) => {
  132. report(<Assert key="mkdir should work correctly" expect={true} actual={exist} />)
  133. return fs.isDir(p)
  134. })
  135. .then((isDir) => {
  136. report(<Assert key="isDir should work correctly" expect={true} actual={isDir} />)
  137. return fs.mkdir(p)
  138. })
  139. .then()
  140. .catch((err) => {
  141. report(<Assert key="isDir should not work when folder exists"
  142. expect={err}
  143. comparer={Comparer.hasValue}/>)
  144. done()
  145. })
  146. })
  147. describe('unlink and mkdir API test', (report, done) => {
  148. let p = dirs.DocumentDir + '/unlink-test-' + Date.now()
  149. fs.createFile(p, 'write' + Date.now(), 'utf8').then(() => {
  150. return fs.exists(p)
  151. })
  152. .then((exist) => {
  153. report(<Assert key="file created" expect={true} actual={exist} />)
  154. return fs.unlink(p).then(() => {
  155. return fs.exists(p)
  156. })
  157. })
  158. .then((exist) => {
  159. report(<Assert key="file removed" expect={false} actual={exist} />)
  160. return fs.mkdir(p + '-dir')
  161. })
  162. .then((err) => fs.exists(p + '-dir'))
  163. .then((exist) => {
  164. report(<Assert key="mkdir should success" expect={true} actual={exist} />)
  165. return fs.unlink(p + '-dir')
  166. })
  167. .then(() => fs.exists(p + '-dir'))
  168. .then((exist) => {
  169. report(<Assert key="folder should be removed" expect={false} actual={exist} />)
  170. done()
  171. })
  172. })
  173. describe('write stream API test', (report, done) => {
  174. let p = dirs.DocumentDir + '/write-stream' + Date.now()
  175. let expect = ''
  176. fs.createFile(p, '1234567890', 'utf8')
  177. .then(() => fs.writeStream(p, 'utf8', true))
  178. .then((ws) => {
  179. ws.write('11')
  180. ws.write('12')
  181. ws.write('13')
  182. ws.write('14')
  183. return ws.close()
  184. })
  185. .then(() => {
  186. let rs = fs.readStream(p, 'utf8')
  187. let d1 = ''
  188. rs.onData((chunk) => {
  189. d1 += chunk
  190. })
  191. rs.onEnd(() => {
  192. report(
  193. <Assert key="write data async test"
  194. expect={'123456789011121314'}
  195. actual={d1}/>)
  196. base64Test()
  197. })
  198. })
  199. function base64Test() {
  200. fs.writeStream(p, 'base64', false)
  201. .then((ws) => {
  202. for(let i = 0; i< 100; i++) {
  203. expect += String(i)
  204. }
  205. ws.write(RNFetchBlob.base64.encode(expect))
  206. return ws.close()
  207. })
  208. .then(() => {
  209. let rs = fs.readStream(p, 'base64')
  210. let d2 = ''
  211. rs.onData((chunk) => {
  212. d2 += chunk
  213. })
  214. rs.onEnd(() => {
  215. console.log(RNFetchBlob.base64.encode(expect),d2)
  216. report(
  217. <Assert key="file should be overwritten by base64 encoded data"
  218. expect={RNFetchBlob.base64.encode(expect)}
  219. actual={d2} />)
  220. done()
  221. })
  222. })
  223. }
  224. })
  225. describe('mv API test', {timeout : 10000},(report, done) => {
  226. let p = dirs.DocumentDir + '/mvTest' + Date.now()
  227. let dest = p + '-dest-' + Date.now()
  228. let content = Date.now() + '-test'
  229. fs.createFile(p, content, 'utf8')
  230. .then(() => fs.mkdir(dest))
  231. .then(() => fs.mv(p, dest +'/moved'))
  232. .then(() => fs.exists(p))
  233. .then((exist) => {
  234. report(<Assert key="file should not exist in old path" expect={false} actual={exist}/>)
  235. return fs.exists(dest + '/moved')
  236. })
  237. .then((exist) => {
  238. report(<Assert key="file should be moved to destination" expect={true} actual={exist}/>)
  239. return fs.ls(dest)
  240. })
  241. .then((files) => {
  242. report(<Assert key="file name should be correct" expect={'moved'} actual={files[0]}/>)
  243. let rs = fs.readStream(dest + '/moved')
  244. let actual = ''
  245. rs.onData((chunk) => {
  246. actual += chunk
  247. })
  248. rs.onEnd(() => {
  249. report(<Assert key="file content should be correct" expect={content} actual={actual}/>)
  250. done()
  251. })
  252. })
  253. })
  254. describe('cp API test', {timeout : 10000},(report, done) => {
  255. let p = dirs.DocumentDir + '/cpTest' + Date.now()
  256. let dest = p + '-dest-' + Date.now()
  257. let content = Date.now() + '-test'
  258. fs.createFile(p, content, 'utf8')
  259. .then(() => fs.mkdir(dest))
  260. .then(() => fs.cp(p, dest +'/cp'))
  261. .then(() => fs.exists(dest +'/cp'))
  262. .then((exist) => {
  263. report(<Assert key="file should be copy to destination" expect={true} actual={exist}/>)
  264. return fs.ls(dest)
  265. })
  266. .then((files) => {
  267. report(<Assert key="file name should be correct" expect={'cp'} actual={files[0]}/>)
  268. let rs = fs.readStream(dest + '/cp')
  269. let actual = ''
  270. rs.onData((chunk) => {
  271. actual += chunk
  272. })
  273. rs.onEnd(() => {
  274. report(<Assert key="file content should be correct" expect={content} actual={actual}/>)
  275. done()
  276. })
  277. })
  278. })