No Description

test-0.7.0.js 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import RNTest from './react-native-testkit/'
  2. import React from 'react'
  3. import RNFetchBlob from 'react-native-fetch-blob'
  4. import Timer from 'react-timer-mixin'
  5. import {
  6. StyleSheet,
  7. Text,
  8. View,
  9. ScrollView,
  10. CameraRoll,
  11. Platform,
  12. Dimensions,
  13. Image,
  14. } from 'react-native';
  15. const fs = RNFetchBlob.fs
  16. const { Assert, Comparer, Info, prop } = RNTest
  17. const describe = RNTest.config({
  18. group : '0.7.0',
  19. run : true,
  20. expand : true,
  21. timeout : 300000000,
  22. })
  23. const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, DROPBOX_TOKEN, styles } = prop()
  24. const dirs = RNFetchBlob.fs.dirs
  25. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  26. describe('Upload and download large file', (report, done) => {
  27. let filename = '22mb-dummy-' + Date.now()
  28. let begin = -1
  29. let begin2 = -1
  30. let deb = Date.now()
  31. RNFetchBlob.config({
  32. fileCache : true
  33. })
  34. .fetch('GET', `${TEST_SERVER_URL}/public/22mb-dummy`)
  35. .progress((now, total) => {
  36. if(begin === -1)
  37. begin = Date.now()
  38. if(Date.now() - deb < 1000)
  39. return
  40. deb = Date.now()
  41. report(<Info uid="200" key="progress">
  42. <Text>
  43. {`download ${now} / ${total} bytes (${Math.floor(now / (Date.now() - begin))} kb/s)`}
  44. </Text>
  45. </Info>)
  46. })
  47. .then((res) => {
  48. try {
  49. deb = Date.now()
  50. let promise = RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  51. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  52. 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+filename+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
  53. 'Content-Type' : 'application/octet-stream',
  54. }, RNFetchBlob.wrap(res.path()))
  55. promise.uploadProgress((now, total) => {
  56. if(Date.now() - deb < 1000)
  57. return
  58. deb = Date.now()
  59. if(begin2 === -1)
  60. begin2 = Date.now()
  61. let speed = Math.floor(now / (Date.now() - begin2))
  62. report(<Info uid="100" key="progress">
  63. <Text>
  64. {`upload ${now} / ${total} bytes (${speed} kb/s)`}
  65. {` ${Math.floor((total-now)/speed/1000)} seconds left`}
  66. </Text>
  67. </Info>)
  68. })
  69. return promise
  70. } catch(err) { console.log(err) }
  71. })
  72. .then((res) => {
  73. report(<Assert
  74. key="upload should success without crashing app"
  75. expect={filename}
  76. actual={res.json().name}/>)
  77. done()
  78. })
  79. })