No Description

test-firebase.js 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. const { Assert, Comparer, Info, prop } = RNTest
  21. const describe = RNTest.config({
  22. group : 'firebase',
  23. run : true,
  24. expand : true,
  25. timeout : 300000000,
  26. })
  27. const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, DROPBOX_TOKEN, styles } = prop()
  28. const dirs = RNFetchBlob.fs.dirs
  29. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  30. let file = RNTest.prop('image')
  31. // Initialize Firebase
  32. var config = {
  33. apiKey: "AIzaSyCnoNvJu2tYYHe87Sm-FrW7j-G-c0MPWGQ",
  34. authDomain: "rnfb-test-app.firebaseapp.com",
  35. databaseURL: "https://rnfb-test-app.firebaseio.com",
  36. storageBucket: "rnfb-test-app.appspot.com",
  37. };
  38. firebase.initializeApp(config);
  39. describe('firebase login', (report, done) => {
  40. firebase.auth().signInWithEmailAndPassword('xeiyan@gmail.com', 'rnfbtest1024')
  41. .catch((err) => {
  42. console.log('firebase sigin failed', err)
  43. })
  44. firebase.auth().onAuthStateChanged((user) => {
  45. report(<Assert key="login status" uid="100"
  46. expect={true}
  47. actual={user !== null}/>,
  48. <Info key="user content" uid="user data">
  49. <Text>{JSON.stringify(user)}</Text>
  50. </Info>)
  51. done()
  52. })
  53. })
  54. describe('upload file to firebase', (report, done) => {
  55. try {
  56. let blob = new Blob(RNTest.prop('image'), 'application/octet-binary')
  57. blob.onCreated(() => {
  58. console.log('BINGO')
  59. let storage = firebase.storage().ref()
  60. let task = storage
  61. .child(`testdata/firebase-test-${Platform.OS}.png`)
  62. .put(blob, { contentType : 'image/png' })
  63. task.on('state_change', null, (err) => {
  64. }, () => {
  65. report(<Assert key="upload success"
  66. expect={true}
  67. actual={true}/>,
  68. <Info key="uploaded file stat" >
  69. <Text>{task.snapshot.totalBytes}</Text>
  70. <Text>{JSON.stringify(task.snapshot.metadata)}</Text>
  71. </Info>)
  72. done()
  73. })
  74. })
  75. } catch(ex) {
  76. console.log('firebase polyfill error', ex)
  77. }
  78. })