Nenhuma descrição

test-blob.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. .catch((err) => {
  98. console.warn(err)
  99. })
  100. .then((b) => fs.readFile(b.getRNFetchBlobRef(), 'utf8'))
  101. .then((data) => {
  102. report(
  103. <Assert key="Blob cache still working properly after clearCache"
  104. expect={expect}
  105. actual={data}/>)
  106. return fs.lstat(fs.dirs.DocumentDir + '/RNFetchBlob-blobs/')
  107. })
  108. .then((stat) => {
  109. report(
  110. <Assert
  111. key="should remain one file in cache directory."
  112. expect={1}
  113. actual={stat.length}/>)
  114. done()
  115. })
  116. })
  117. describe('create blob using FormData', (report, done) => {
  118. let form = new FormData()
  119. let fname = 'blob-test' + Date.now()
  120. File.build('test.png', RNTest.prop('image'), { type:'image/png;base64' })
  121. .then((f) => {
  122. form.append('name', fname)
  123. form.append('blob', f)
  124. return Blob.build(form)
  125. })
  126. .then((b) => {
  127. let body = RNFetchBlob.wrap(b.getRNFetchBlobRef())
  128. return RNFetchBlob.fetch(
  129. 'POST',
  130. `${TEST_SERVER_URL}/upload-form`,
  131. {
  132. 'content-type' : 'multipart/form-data; boundary='+b.multipartBoundary
  133. },
  134. body)
  135. })
  136. .then((resp) => {
  137. report(
  138. <Assert key="form data verification #1"
  139. actual={resp.json().files[0].originalname}
  140. expect={'test.png'}/>,
  141. <Assert key="form data verification #2"
  142. actual={resp.json().fields.name}
  143. expect={fname}/>)
  144. done()
  145. })
  146. })