暫無描述

test-blob.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 Blob = RNFetchBlob.polyfill.Blob
  17. const File = RNFetchBlob.polyfill.File
  18. const { Assert, Comparer, Info, prop } = RNTest
  19. const describe = RNTest.config({
  20. group : 'Blob',
  21. run : true,
  22. expand : false,
  23. timeout : 20000,
  24. })
  25. const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, DROPBOX_TOKEN, styles } = prop()
  26. const dirs = RNFetchBlob.fs.dirs
  27. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  28. let file = RNTest.prop('image')
  29. describe('create Blob from string', (report, done) => {
  30. Blob.build('hello world !')
  31. .then((b) => fs.readFile(b.getRNFetchBlobRef(), 'utf8'))
  32. .then((data) => {
  33. report(
  34. <Assert
  35. key="string data verification"
  36. expect={'hello world !'}
  37. actual={data}/>)
  38. done()
  39. })
  40. })
  41. describe('create blob from BASE64 encoded data', (report, done) => {
  42. let image = RNTest.prop('image')
  43. Blob.build(image, {type : 'image/png;base64'})
  44. .then((b) => fs.readFile(b.getRNFetchBlobRef(), 'base64'))
  45. .then((data) => {
  46. report(
  47. <Assert
  48. key="compare content length"
  49. expect={image.length}
  50. actual={data.length} />,
  51. <Assert
  52. key="compare content"
  53. expect={image}
  54. actual={data} />)
  55. done()
  56. })
  57. })
  58. describe('create blob from file', (report, done) => {
  59. let path = fs.dirs.DocumentDir + '/blob-test-temp-img'
  60. let image = RNTest.prop('image')
  61. fs.writeFile(path, image, 'base64')
  62. .then(() => Blob.build(RNFetchBlob.wrap(path)))
  63. .then((b) => fs.readFile(b.getRNFetchBlobRef(), 'base64'))
  64. .then((data) => {
  65. report(
  66. <Assert
  67. key="compare content length"
  68. expect={image.length}
  69. actual={data.length} />,
  70. <Assert
  71. key="compare content"
  72. expect={image}
  73. actual={data} />)
  74. done()
  75. })
  76. })
  77. describe('create Blob without any agument', (report, done) => {
  78. Blob.build().then((b) => fs.stat(b.getRNFetchBlobRef()))
  79. .then((stat) => {
  80. report(
  81. <Assert
  82. key="cache file exists"
  83. expect={true}
  84. actual={stat !== undefined && stat !== null}
  85. />,
  86. <Assert
  87. key="cache file size is 0"
  88. expect={0}
  89. actual={Math.floor(stat.size)}/>)
  90. done()
  91. })
  92. })
  93. describe('blob clear cache test', (report, done) => {
  94. let expect = 'test-' + Date.now()
  95. Blob.clearCache()
  96. .then(() => Blob.build(expect))
  97. .then((b) => fs.readFile(b.getRNFetchBlobRef(), 'utf8'))
  98. .then((data) => {
  99. report(
  100. <Assert key="Blob cache still working properly after clearCache"
  101. expect={expect}
  102. actual={data}/>)
  103. return fs.lstat(fs.dirs.DocumentDir + '/RNFetchBlob-blobs/')
  104. })
  105. .then((stat) => {
  106. report(
  107. <Assert
  108. key="should remain one file in cache directory."
  109. expect={1}
  110. actual={stat.length}/>)
  111. done()
  112. })
  113. })
  114. describe('create blob using FormData', (report, done) => {
  115. let form = new FormData()
  116. let fname = 'blob-test' + Date.now()
  117. File.build(RNTest.prop('image'), { type:'image/png;base64' })
  118. .then((f) => {
  119. f.name = 'test.png'
  120. form.append('name', fname)
  121. form.append('blob', f)
  122. return Blob.build(form)
  123. })
  124. .then((b) => RNFetchBlob
  125. .fetch('POST', `${TEST_SERVER_URL}/upload-form`, {
  126. 'content-type' : 'multipart/form-data; boundary='+b.multipartBoundary
  127. }, RNFetchBlob.wrap(b.getRNFetchBlobRef())))
  128. .then((resp) => {
  129. report(
  130. <Assert key="form data verification #1"
  131. actual={resp.json().files[0].originalname}
  132. expect={'test.png'}/>,
  133. <Assert key="form data verification #2"
  134. actual={resp.json().fields.name}
  135. expect={fname}/>)
  136. done()
  137. })
  138. })