import RNTest from './react-native-testkit/' import React from 'react' import RNFetchBlob from 'react-native-fetch-blob' import Timer from 'react-timer-mixin' import firebase from 'firebase' import { StyleSheet, Text, View, ScrollView, CameraRoll, Platform, Dimensions, Image, } from 'react-native'; const fs = RNFetchBlob.fs const Blob = RNFetchBlob.polyfill.Blob window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest window.Blob = Blob const { Assert, Comparer, Info, prop } = RNTest const describe = RNTest.config({ group : 'firebase', run : true, expand : false, timeout : 300000000, }) const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, DROPBOX_TOKEN, styles } = prop() const dirs = RNFetchBlob.fs.dirs let prefix = ((Platform.OS === 'android') ? 'file://' : '') let file = RNTest.prop('image') // Initialize Firebase var config = { apiKey: "AIzaSyCnoNvJu2tYYHe87Sm-FrW7j-G-c0MPWGQ", authDomain: "rnfb-test-app.firebaseapp.com", databaseURL: "https://rnfb-test-app.firebaseio.com", storageBucket: "rnfb-test-app.appspot.com", }; firebase.initializeApp(config); describe('firebase login', (report, done) => { firebase.auth().signInWithEmailAndPassword('xeiyan@gmail.com', 'rnfbtest1024') .catch((err) => { console.log('firebase sigin failed', err) }) firebase.auth().onAuthStateChanged((user) => { report(, {JSON.stringify(user)} ) done() }) }) describe('upload file to firebase', (report, done) => { let testImage = `firebase-test-${Platform.OS}-${Date.now()}.png` RNTest.prop('firebase-image', testImage) // create Blob from BASE64 data Blob.build(RNTest.prop('image'), { type : 'image/png;BASE64'}) .then((blob) => { let storage = firebase.storage().ref('rnfbtest') let task = storage .child(RNTest.prop('firebase-image')) .put(blob, { contentType : 'image/png' }) .then((snapshot) => { console.log(snapshot.metadata) report(, {snapshot.totalBytes} {JSON.stringify(snapshot.metadata)} ) done() }) }) }) describe('download firebase storage item', (report, done) => { let storage = firebase.storage().ref('rnfbtest/' + RNTest.prop('firebase-image')) storage.getDownloadURL().then((url) => { console.log(url) report( ) done() }) }) let tier2FileName = `firebase-test-${Platform.OS}-github2.jpg` describe('upload using file path', (report, done) => { RNFetchBlob .config({ fileCache : true, appendExt : 'jpg' }) .fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`) .then((resp) => { report( ) let blob = new Blob(RNFetchBlob.wrap(resp.path()), { type : 'image/jpg' }) blob.onCreated(() => { firebase.storage().ref('rnfbtest') .child(tier2FileName) .put(blob, { contentType : 'image/jpg' }) .then(() => { report() done() }) }) }) }) let directURL = null describe('verify uploaded file', (report, done) => { firebase.storage().ref('rnfbtest/' + tier2FileName) .getDownloadURL() .then((url) => { directURL = url report( ) done() }) }) describe('download to base64', (report, done) => { RNFetchBlob.fetch('GET', directURL).then((resp) => { report( ) done() }) }) describe('upload from storage', (report, done) => { try { let file = fs.dirs.DocumentDir + '/tempimg.png' fs.writeFile(file, RNTest.prop('image'), 'base64') .then(() => Blob.build(RNFetchBlob.wrap(file), {type : 'image/png'})) .then((blob) => { let storage = firebase.storage().ref('rnfbtest') let task = storage .child(`image-from-storage-${Platform.OS}-${Date.now()}.png`) .put(blob, { contentType : 'image/png' }) .then((snapshot) => { console.log(snapshot.metadata) report(, {snapshot.totalBytes} {JSON.stringify(snapshot.metadata)} ) done() }) }) } catch(err) { console.log(err) } })