Nav apraksta

test-firebase.js 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 : true,
  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. try {
  57. let blob = new Blob(RNTest.prop('image'), 'application/octet-binary')
  58. blob.onCreated(() => {
  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. })