No Description

test-blob.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 { Assert, Comparer, Info, prop } = RNTest
  18. const describe = RNTest.config({
  19. group : 'Blob',
  20. run : true,
  21. expand : false,
  22. timeout : 20000,
  23. })
  24. const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, DROPBOX_TOKEN, styles } = prop()
  25. const dirs = RNFetchBlob.fs.dirs
  26. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  27. let file = RNTest.prop('image')
  28. describe('create Blob from string', (report, done) => {
  29. Blob.build('hello world !')
  30. .then((b) => fs.readFile(b.getRNFetchBlobRef(), 'utf8'))
  31. .then((data) => {
  32. report(
  33. <Assert
  34. key="string data verification"
  35. expect={'hello world !'}
  36. actual={data}/>)
  37. done()
  38. })
  39. })
  40. describe('create blob from BASE64 encoded data', (report, done) => {
  41. let image = RNTest.prop('image')
  42. Blob.build(image, {type : 'image/png;base64'})
  43. .then((b) => fs.readFile(b.getRNFetchBlobRef(), 'base64'))
  44. .then((data) => {
  45. report(
  46. <Assert
  47. key="compare content length"
  48. expect={image.length}
  49. actual={data.length} />,
  50. <Assert
  51. key="compare content"
  52. expect={image}
  53. actual={data} />)
  54. done()
  55. })
  56. })
  57. describe('create blob from file', (report, done) => {
  58. let path = fs.dirs.DocumentDir + '/blob-test-temp-img'
  59. let image = RNTest.prop('image')
  60. fs.writeFile(path, image, 'base64')
  61. .then(() => Blob.build(RNFetchBlob.wrap(path)))
  62. .then((b) => fs.readFile(b.getRNFetchBlobRef(), 'base64'))
  63. .then((data) => {
  64. report(
  65. <Assert
  66. key="compare content length"
  67. expect={image.length}
  68. actual={data.length} />,
  69. <Assert
  70. key="compare content"
  71. expect={image}
  72. actual={data} />)
  73. done()
  74. })
  75. })
  76. describe('create Blob without any agument', (report, done) => {
  77. Blob.build().then((b) => fs.stat(b.getRNFetchBlobRef()))
  78. .then((stat) => {
  79. report(
  80. <Assert
  81. key="cache file exists"
  82. expect={true}
  83. actual={stat !== undefined && stat !== null}
  84. />,
  85. <Assert
  86. key="cache file size is 0"
  87. expect={0}
  88. actual={Math.floor(stat.size)}/>)
  89. done()
  90. })
  91. })
  92. describe('blob clear cache test', (report, done) => {
  93. let expect = 'test-'+Date.now()
  94. Blob.clearCache()
  95. .then(() => Blob.build(expect))
  96. .then((b) => fs.readFile(b.getRNFetchBlobRef(), 'utf8'))
  97. .then((data) => {
  98. report(
  99. <Assert key="Blob cache still working properly after clearCache"
  100. expect={expect}
  101. actual={data}/>)
  102. return fs.lstat(fs.dirs.DocumentDir + '/RNFetchBlob-blobs/')
  103. })
  104. .then((stat) => {
  105. report(
  106. <Assert
  107. key="should remain one file in cache directory."
  108. expect={1}
  109. actual={stat.length}/>)
  110. done()
  111. })
  112. })