No Description

test-0.6.0.js 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 fs = RNFetchBlob.fs
  14. const { Assert, Comparer, Info, prop } = RNTest
  15. const describe = RNTest.config({
  16. group : '0.6.0',
  17. run : true,
  18. expand : false,
  19. timeout : 10000,
  20. })
  21. const { TEST_SERVER_URL_SSL, FILENAME, DROPBOX_TOKEN, styles } = prop()
  22. const dirs = RNFetchBlob.fs.dirs
  23. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  24. describe('writeFile and readFile test', (report, done) => {
  25. let path = dirs.DocumentDir + '/0.6.0-'+Date.now()+'/writeFileTest'+Date.now()
  26. let data = 'hellofrom'+Date.now()
  27. fs.writeFile(path, data)
  28. .then(() => fs.readFile(path, 'utf8'))
  29. .then((actual) => {
  30. report(<Assert key="utf8 content should correct" expect={data} actual={actual}/>)
  31. data = 'base64'
  32. return fs.writeFile(path, RNFetchBlob.base64.encode('base64'), 'base64')
  33. })
  34. .then(() => fs.readFile(path, 'base64'))
  35. .then((actual) => {
  36. report(<Assert key="base64 content should correct"
  37. expect={RNFetchBlob.base64.decode(RNFetchBlob.base64.encode(data))}
  38. actual={RNFetchBlob.base64.decode(actual)}/>)
  39. data = 'ascii'
  40. return fs.writeFile(path, getASCIIArray('ascii'), 'ascii');
  41. })
  42. .then(() => fs.readFile(path, 'ascii'))
  43. .then((actual) => {
  44. report(<Assert key="ascii content should correct"
  45. expect={getASCIIArray(data)}
  46. comparer={Comparer.equalToArray}
  47. actual={actual}/>)
  48. done()
  49. })
  50. })
  51. describe('append file test', (report, done) => {
  52. let path = dirs.DocumentDir + '/append-test'+Date.now()
  53. let content = 'test on ' + Date.now()
  54. fs.writeFile(path, content, 'utf8')
  55. .then(() => fs.appendFile(path, '100', 'utf8', true))
  56. .then(() => fs.readFile(path, 'utf8'))
  57. .then((data) => {
  58. report(
  59. <Assert key="utf8 data should be appended"
  60. expect={content + '100'}
  61. actual={data} />)
  62. return fs.appendFile(path, getASCIIArray('200'), 'ascii')
  63. })
  64. .then(() => fs.readFile(path, 'ascii'))
  65. .then((data) => {
  66. report(<Assert key="ascii data should be appended"
  67. expect={getASCIIArray(content + '100' + '200')}
  68. comparer={Comparer.equalToArray}
  69. actual={data} />)
  70. return fs.appendFile(path, RNFetchBlob.base64.encode('300'), 'base64')
  71. })
  72. .then(() => fs.readFile(path, 'base64'))
  73. .then((data) => {
  74. report(<Assert key="base64 data should be appended"
  75. expect={content + '100' + '200' + '300'}
  76. actual={RNFetchBlob.base64.decode(data)} />)
  77. done()
  78. })
  79. })
  80. function getASCIIArray(str) {
  81. let r = []
  82. for(let i=0;i<str.length;i++) {
  83. r.push(str[i].charCodeAt(0))
  84. }
  85. return r
  86. }