Nenhuma descrição

test-0.6.0.js 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. console.log(getASCIIArray(data), actual)
  45. report(<Assert key="ascii content should correct"
  46. expect={getASCIIArray(data)}
  47. comparer={Comparer.equalToArray}
  48. actual={actual}/>)
  49. done()
  50. })
  51. })
  52. describe('append file test', (report, done) => {
  53. let path = dirs.DocumentDir + '/append-test'+Date.now()
  54. let content = 'test on ' + Date.now()
  55. fs.writeFile(path, content, 'utf8')
  56. .then(() => fs.appendFile(path, '100', 'utf8', true))
  57. .then(() => fs.readFile(path, 'utf8'))
  58. .then((data) => {
  59. report(
  60. <Assert key="utf8 data should be appended"
  61. expect={content + '100'}
  62. actual={data} />)
  63. return fs.appendFile(path, getASCIIArray('200'), 'ascii')
  64. })
  65. .then(() => fs.readFile(path, 'ascii'))
  66. .then((data) => {
  67. report(<Assert key="ascii data should be appended"
  68. expect={getASCIIArray(content + '100' + '200')}
  69. comparer={Comparer.equalToArray}
  70. actual={data} />)
  71. return fs.appendFile(path, RNFetchBlob.base64.encode('300'), 'base64')
  72. })
  73. .then(() => fs.readFile(path, 'base64'))
  74. .then((data) => {
  75. report(<Assert key="base64 data should be appended"
  76. expect={content + '100' + '200' + '300'}
  77. actual={RNFetchBlob.base64.decode(data)} />)
  78. done()
  79. })
  80. })
  81. function getASCIIArray(str) {
  82. let r = []
  83. for(let i=0;i<str.length;i++) {
  84. r.push(str[i].charCodeAt(0))
  85. }
  86. return r
  87. }