Нема описа

test-0.6.2.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. CameraRoll,
  10. Platform,
  11. Dimensions,
  12. Image,
  13. } from 'react-native';
  14. const fs = RNFetchBlob.fs
  15. const { Assert, Comparer, Info, prop } = RNTest
  16. const describe = RNTest.config({
  17. group : '0.6.2',
  18. run : true,
  19. expand : false,
  20. timeout : 12000,
  21. })
  22. const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, DROPBOX_TOKEN, styles } = prop()
  23. const dirs = RNFetchBlob.fs.dirs
  24. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  25. let photo = null
  26. describe('upload asset from camera roll', (report, done) => {
  27. let imgName = `image-from-camera-roll-${Platform.OS}.jpg`
  28. CameraRoll.getPhotos({first : 10})
  29. .then((resp) => {
  30. let url = resp.edges[0].node.image.uri
  31. photo = url
  32. return RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  33. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  34. 'Dropbox-API-Arg': `{\"path\": \"/rn-upload/${imgName}\",\"mode\": \"add\",\"autorename\": false,\"mute\": false}`,
  35. 'Content-Type' : 'application/octet-stream',
  36. }, RNFetchBlob.wrap(url))
  37. })
  38. .then((resp) => {
  39. resp = resp.json()
  40. report(
  41. <Assert key="confirm the file has been uploaded" expect={imgName} actual={resp.name}/>
  42. )
  43. done()
  44. })
  45. })
  46. describe('Upload multipart data with file from CameraRoll', (report, done) => {
  47. let filename = 'test-from-storage-img-'+Date.now()+'.png'
  48. RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/upload-form`, {
  49. 'Content-Type' : 'multipart/form-data',
  50. }, [
  51. { name : 'test-img', filename : filename, data: RNFetchBlob.wrap(photo)},
  52. { name : 'test-text', filename : 'test-text.txt', data: RNFetchBlob.base64.encode('hello.txt')},
  53. { name : 'field1', data : 'hello !!'},
  54. { name : 'field2', data : 'hello2 !!'}
  55. ])
  56. .then((resp) => {
  57. resp = resp.json()
  58. report(
  59. <Assert key="check posted form data #1" expect="hello !!" actual={resp.fields.field1}/>,
  60. <Assert key="check posted form data #2" expect="hello2 !!" actual={resp.fields.field2}/>,
  61. )
  62. return RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/${filename}`)
  63. })
  64. .then((resp) => {
  65. report(<Info key="uploaded image">
  66. <Image
  67. style={styles.image}
  68. source={{ uri : 'data:image/png;base64, '+ resp.base64()}}/>
  69. </Info>)
  70. done()
  71. })
  72. })
  73. describe('access assets from camera roll', (report, done) => {
  74. let photo = null
  75. CameraRoll.getPhotos({first : 10})
  76. .then((resp) => {
  77. photo = resp.edges[0].node.image.uri
  78. report(<Info key="items">
  79. <Text>{photo}</Text>
  80. </Info>)
  81. return fs.readFile(photo, 'base64')
  82. })
  83. .then((data) => {
  84. report(<Info key="asset image">
  85. <Image
  86. style={styles.image}
  87. source={{uri: `data:image/png;base64, ${data}`}}/>
  88. </Info>)
  89. done()
  90. })
  91. })
  92. describe('read asset in app bundle',(report, done) => {
  93. let target = fs.asset('test-asset2.png')
  94. fs.readFile(target, 'base64')
  95. .then((data) => {
  96. report(<Info key="asset image">
  97. <Image
  98. style={styles.image}
  99. source={{uri: `data:image/png;base64, ${data}`}}/>
  100. </Info>)
  101. return fs.readFile(fs.asset('test-asset1.json'), 'utf8')
  102. })
  103. .then((resp) => {
  104. report(
  105. <Assert key="asset content verify"
  106. expect="asset#1"
  107. actual={JSON.parse(resp).secret}/>)
  108. done()
  109. })
  110. })
  111. describe('stat assets in app', (report, done) => {
  112. fs.stat(fs.asset('test-asset2.png'))
  113. .then((data) => {
  114. report(<Info key="list of assets">
  115. <Text>{JSON.stringify(data)}</Text>
  116. </Info>)
  117. done()
  118. })
  119. })
  120. describe('copy asset', (report, done) => {
  121. let dest = `${dirs.DocumentDir}/test-asset-1-${Date.now()}.json`
  122. fs.cp(fs.asset('test-asset1.json'), dest)
  123. .then(() => fs.readFile(dest, 'utf8'))
  124. .then((data) => {
  125. report(<Assert key="asset copied correctly"
  126. expect={'asset#1'}
  127. actual={JSON.parse(data).secret}/>)
  128. return fs.stat(fs.asset('test-asset1.json'))
  129. })
  130. .then((stat) => {
  131. report(<Assert key="file size check"
  132. expect={27}
  133. actual={Math.floor(stat.size)}/>,
  134. <Info key="dest file info">
  135. <Text>{JSON.stringify(stat)}</Text>
  136. </Info>)
  137. done()
  138. })
  139. })