No Description

test-0.5.x.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. Platform,
  10. Dimensions,
  11. Image,
  12. } from 'react-native';
  13. const { Assert, Comparer, Info, describe, prop } = RNTest
  14. const { TEST_SERVER_URL, FILENAME, DROPBOX_TOKEN, styles } = prop()
  15. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  16. // added after 0.5.0
  17. describe('Get storage folders', (report, done) => {
  18. RNFetchBlob.getSystemDirs().then((dirs) => {
  19. report(
  20. <Assert key="system folders should exists" expect={dirs} comparer={Comparer.exists} />,
  21. <Assert key="check properties"
  22. expect={dirs}
  23. comparer={Comparer.hasProperties}
  24. actual={['PictureDir', 'MovieDir', 'DocumentDir', 'CacheDir']}
  25. />,
  26. <Info key="System Folders">
  27. <Text>{`${JSON.stringify(dirs)}`}</Text>
  28. </Info>
  29. )
  30. done()
  31. })
  32. })
  33. let tmpFilePath = null
  34. describe('Download file to storage with custom file extension', (report, done) => {
  35. RNFetchBlob.config({
  36. fileCache : true,
  37. appendExt : 'png'
  38. })
  39. .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
  40. .then((resp) => {
  41. tmpFilePath = resp.path()
  42. report(<Info key={`image from ${tmpFilePath}`}>
  43. <Image
  44. source={{ uri : prefix + tmpFilePath}}
  45. style={styles.image}/>
  46. </Info>)
  47. done()
  48. })
  49. })
  50. describe('Read cached file via file stream', (report, done) => {
  51. let data = 'data:image/png;base64, '
  52. let stream = RNFetchBlob.openReadStream(tmpFilePath, 'base64')
  53. stream.onData((chunk) => {
  54. data += chunk
  55. })
  56. stream.onEnd(() => {
  57. console.log(prop('image').length, data.length)
  58. console.log(data)
  59. report(
  60. <Assert key="image should have value"
  61. expect={0}
  62. comparer={Comparer.smaller}
  63. actual={data.length}/>,
  64. <Info key="image from read stream">
  65. <Image source={{uri : data}} style={styles.image}/>
  66. </Info>)
  67. done()
  68. })
  69. stream.onError((err) => {
  70. console.log('stream err', err)
  71. })
  72. })
  73. describe('File stream reader error should be able to handled', (report, done) => {
  74. let stream = RNFetchBlob.openReadStream('^_^ not exists', 'base64')
  75. stream.onError((err) => {
  76. report(<Info key="error message">
  77. <Text>
  78. {err}
  79. </Text>
  80. </Info>)
  81. done()
  82. })
  83. })
  84. //
  85. // describe('Upload from file storage', (report, done) => {
  86. // let filename = ''
  87. // let filepath = ''
  88. // RNFetchBlob.getSystemDirs().then((dirs) => {
  89. // filename = 'ios.5.0-' + Date.now() + '-from-storage.png'
  90. // filepath = dirs.DocumentDir + '/' + filename
  91. // return RNFetchBlob.config({ path : filepath })
  92. // .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
  93. // })
  94. // .then((resp) => {
  95. // let path = resp.path()
  96. // return RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  97. // Authorization : `Bearer ${DROPBOX_TOKEN}`,
  98. // 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+filename+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
  99. // 'Content-Type' : 'application/octet-stream',
  100. // }, 'RNFetchBlob-file://' + path)
  101. // .then((resp) => {
  102. // console.log(resp.text())
  103. // resp = resp.json()
  104. // report(
  105. // <Assert key="confirm the file has been uploaded" expect={filename} actual={resp.name}/>
  106. // )
  107. // done()
  108. // })
  109. // })
  110. //
  111. //
  112. //
  113. // })