Aucune description

test-firebase.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 firebase from 'firebase'
  6. import {
  7. StyleSheet,
  8. Text,
  9. View,
  10. ScrollView,
  11. CameraRoll,
  12. Platform,
  13. Dimensions,
  14. Image,
  15. } from 'react-native';
  16. const fs = RNFetchBlob.fs
  17. const Blob = RNFetchBlob.polyfill.Blob
  18. window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest
  19. window.Blob = Blob
  20. window.FormData = RNFetchBlob.polyfill.FormData
  21. const { Assert, Comparer, Info, prop } = RNTest
  22. const describe = RNTest.config({
  23. group : 'firebase',
  24. run : true,
  25. expand : false,
  26. timeout : 300000000,
  27. })
  28. const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, DROPBOX_TOKEN, styles } = prop()
  29. const dirs = RNFetchBlob.fs.dirs
  30. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  31. let file = RNTest.prop('image')
  32. // Initialize Firebase
  33. var config = {
  34. apiKey: "AIzaSyCnoNvJu2tYYHe87Sm-FrW7j-G-c0MPWGQ",
  35. authDomain: "rnfb-test-app.firebaseapp.com",
  36. databaseURL: "https://rnfb-test-app.firebaseio.com",
  37. storageBucket: "rnfb-test-app.appspot.com",
  38. };
  39. firebase.initializeApp(config);
  40. describe('firebase login', (report, done) => {
  41. firebase.auth().signInWithEmailAndPassword('xeiyan@gmail.com', 'rnfbtest1024')
  42. .catch((err) => {
  43. console.log('firebase sigin failed', err)
  44. })
  45. firebase.auth().onAuthStateChanged((user) => {
  46. report(<Assert key="login status" uid="100"
  47. expect={true}
  48. actual={user !== null}/>,
  49. <Info key="user content" uid="user data">
  50. <Text>{JSON.stringify(user)}</Text>
  51. </Info>)
  52. done()
  53. })
  54. })
  55. describe('upload file to firebase', (report, done) => {
  56. // create Blob from BASE64 data
  57. let blob = new Blob(RNTest.prop('image'), { type : 'image/png;BASE64'})
  58. let testImage = `firebase-test-${Platform.OS}-${Date.now()}.png`
  59. RNTest.prop('firebase-image', testImage)
  60. // start test after Blob created
  61. blob.onCreated(() => {
  62. let storage = firebase.storage().ref('rnfbtest')
  63. let task = storage
  64. .child(RNTest.prop('firebase-image'))
  65. .put(blob, { contentType : 'image/png' })
  66. .then((snapshot) => {
  67. console.log(snapshot.metadata)
  68. report(<Assert key="upload success"
  69. expect={true}
  70. actual={true}/>,
  71. <Info key="uploaded file stat" >
  72. <Text>{snapshot.totalBytes}</Text>
  73. <Text>{JSON.stringify(snapshot.metadata)}</Text>
  74. </Info>)
  75. done()
  76. })
  77. })
  78. })
  79. describe('download firebase storage item', (report, done) => {
  80. let storage = firebase.storage().ref('rnfbtest/' + RNTest.prop('firebase-image'))
  81. storage.getDownloadURL().then((url) => {
  82. console.log(url)
  83. report(<Info key="image viewer">
  84. <Image style={styles.image} source={{uri : url}}/>
  85. </Info>)
  86. done()
  87. })
  88. })
  89. let tier2FileName = `firebase-test-${Platform.OS}-github2.jpg`
  90. describe('upload using file path', (report, done) => {
  91. RNFetchBlob
  92. .config({ fileCache : true, appendExt : 'jpg' })
  93. .fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`)
  94. .then((resp) => {
  95. report(<Info key="test image">
  96. <Image style={styles.image} source={{uri : prefix + resp.path()}}/>
  97. </Info>)
  98. let blob = new Blob(RNFetchBlob.wrap(resp.path()), { type : 'image/jpg' })
  99. blob.onCreated(() => {
  100. firebase.storage().ref('rnfbtest')
  101. .child(tier2FileName)
  102. .put(blob, { contentType : 'image/jpg' })
  103. .then(() => {
  104. report(<Assert key="upload finished" />)
  105. done()
  106. })
  107. })
  108. })
  109. })
  110. let directURL = null
  111. describe('verify uploaded file', (report, done) => {
  112. firebase.storage().ref('rnfbtest/' + tier2FileName)
  113. .getDownloadURL()
  114. .then((url) => {
  115. directURL = url
  116. report(
  117. <Info key="image viewer">
  118. <Image style={styles.image} source={{uri : url}}/>
  119. </Info>)
  120. done()
  121. })
  122. })
  123. describe('download to base64', (report, done) => {
  124. RNFetchBlob.fetch('GET', directURL).then((resp) => {
  125. report(
  126. <Info key="image data">
  127. <Image
  128. style={styles.image}
  129. source={{uri : 'data:image/jpg;base64 ,'+ resp.base64()}}/>
  130. </Info>)
  131. done()
  132. })
  133. })