No Description

test-fetch.js 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. window.Blob = RNFetchBlob.polyfill.Blob
  14. window.fetch = new RNFetchBlob.polyfill.Fetch({
  15. auto : true,
  16. binaryContentTypes : ['image/', 'video/', 'audio/']
  17. }).build()
  18. const fs = RNFetchBlob.fs
  19. const { Assert, Comparer, Info, prop } = RNTest
  20. const describe = RNTest.config({
  21. group : 'Fetch polyfill',
  22. run : true,
  23. expand : true,
  24. timeout : 10000,
  25. })
  26. const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, FILENAME, DROPBOX_TOKEN, styles } = prop()
  27. const dirs = RNFetchBlob.fs.dirs
  28. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  29. describe('GET request test : text -> any', (report, done) => {
  30. function get(fn1, fn2) {
  31. return fetch(`${TEST_SERVER_URL}/unicode`, { method : 'GET'})
  32. .then((res) => fn1(res))
  33. .then((data) => fn2(data))
  34. }
  35. let promises =
  36. [
  37. get((res) => res.json(), (json) => {
  38. report(<Assert key="json data correct" expect={'你好!'} actual={json.data}/>)
  39. }),
  40. get((res) => res.text(), (text) => {
  41. report(<Assert key="text data correct" expect={'你好!'} actual={JSON.parse(text).data}/>)
  42. }),
  43. get((res) => res.blob(), (blob) => {
  44. let path = blob.getRNFetchBlobRef()
  45. return fs.readFile(path, 'utf8').then((text) => {
  46. console.log(text)
  47. report(<Assert key="blob data correct" expect={'你好!'} actual={JSON.parse(text).data}/>)
  48. })
  49. }),
  50. // get((res) => res.arrayBuffer(), (text) => {
  51. // report(<Assert key="text data correct" expect={'你好!'} actual={JSON.parse(text).data}/>)
  52. // })
  53. ]
  54. Promise.all(promises).then(() => {
  55. done()
  56. })
  57. })
  58. describe('GET request which has json response', (report, done) => {
  59. })
  60. describe('GET request which has blob response', (report, done) => {
  61. })