설명 없음

test-0.5.x.js 3.2KB

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