Нема описа

test-firebase.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 : false,
  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. // create Blob from BASE64 data
  56. let blob = new Blob(RNTest.prop('image'), { type : 'image/png;BASE64'})
  57. let testImage = `firebase-test-${Platform.OS}-${Date.now()}.png`
  58. RNTest.prop('firebase-image', testImage)
  59. // start test after Blob created
  60. blob.onCreated(() => {
  61. let storage = firebase.storage().ref('rnfbtest')
  62. let task = storage
  63. .child(RNTest.prop('firebase-image'))
  64. .put(blob, { contentType : 'image/png' })
  65. .then((snapshot) => {
  66. console.log(snapshot.metadata)
  67. report(<Assert key="upload success"
  68. expect={true}
  69. actual={true}/>,
  70. <Info key="uploaded file stat" >
  71. <Text>{snapshot.totalBytes}</Text>
  72. <Text>{JSON.stringify(snapshot.metadata)}</Text>
  73. </Info>)
  74. done()
  75. })
  76. })
  77. })
  78. describe('download firebase storage item', (report, done) => {
  79. let storage = firebase.storage().ref('rnfbtest/' + RNTest.prop('firebase-image'))
  80. storage.getDownloadURL().then((url) => {
  81. console.log(url)
  82. report(<Info key="image viewer">
  83. <Image style={styles.image} source={{uri : url}}/>
  84. </Info>)
  85. done()
  86. })
  87. })
  88. let tier2FileName = `firebase-test-${Platform.OS}-github2.jpg`
  89. describe('upload using file path', (report, done) => {
  90. RNFetchBlob
  91. .config({ fileCache : true, appendExt : 'jpg' })
  92. .fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`)
  93. .then((resp) => {
  94. report(<Info key="test image">
  95. <Image style={styles.image} source={{uri : prefix + resp.path()}}/>
  96. </Info>)
  97. let blob = new Blob(RNFetchBlob.wrap(resp.path()), { type : 'image/jpg' })
  98. blob.onCreated(() => {
  99. firebase.storage().ref('rnfbtest')
  100. .child(tier2FileName)
  101. .put(blob, { contentType : 'image/jpg' })
  102. .then(() => {
  103. report(<Assert key="upload finished" />)
  104. done()
  105. })
  106. })
  107. })
  108. })
  109. let directURL = null
  110. describe('verify uploaded file', (report, done) => {
  111. firebase.storage().ref('rnfbtest/' + tier2FileName)
  112. .getDownloadURL()
  113. .then((url) => {
  114. directURL = url
  115. report(
  116. <Info key="image viewer">
  117. <Image style={styles.image} source={{uri : url}}/>
  118. </Info>)
  119. done()
  120. })
  121. })
  122. describe('download to base64', (report, done) => {
  123. RNFetchBlob.fetch('GET', directURL).then((resp) => {
  124. report(
  125. <Info key="image data">
  126. <Image
  127. style={styles.image}
  128. source={{uri : 'data:image/jpg;base64 ,'+ resp.base64()}}/>
  129. </Info>)
  130. done()
  131. })
  132. })