Skip to content

Instantly share code, notes, and snippets.

@himanshusinghs
Last active March 14, 2019 08:53
Show Gist options
  • Save himanshusinghs/bd86262ce3c9e6f4d691b5242de707ef to your computer and use it in GitHub Desktop.
Save himanshusinghs/bd86262ce3c9e6f4d691b5242de707ef to your computer and use it in GitHub Desktop.
React Native Firebase Mock
1. Create a __mocks__ folder in the project's root (on the same level as of node_modules).
2. Inside __mocks__ folder, create a file named react-native-firebase.js and copy-paste the content from RNFirebaseMock.js in this gist to your react-native-firebase.js that you just created.
'use strict'
export class Database {
ref = (path) => {
if (!this[path]) {
this[path] = new Reference(path)
}
return this[path]
}
}
export class Reference {
constructor(path) {
this.path = path
this.snap = { val: () => this._val()}
this.data = null
}
_val = jest.fn(() => {
return this.data
})
once = jest.fn((param, callback) => {
const promise = new Promise ((resolve, reject) => {
if (callback) {
callback(this.snap)
resolve()
} else {
resolve(this.snap)
}
})
RNFirebase.promises.push(promise)
return promise
})
on = jest.fn((param, callback) => {
const promise = new Promise ((resolve, reject) => {
if (callback) {
callback(this.snap)
resolve()
} else {
resolve(this.snap)
}
})
RNFirebase.promises.push(promise)
return promise
})
off = jest.fn((param, callback) => {
const promise = Promise.resolve()
RNFirebase.promises.push(promise)
return promise
})
update = jest.fn((data) => {
const promise = Promise.resolve()
RNFirebase.promises.push(promise)
return promise
})
remove = jest.fn(() => {
const promise = Promise.resolve()
RNFirebase.promises.push(promise)
return promise
})
}
export class MockFirebase {
constructor() {
this.database = () => {
if (!this.databaseInstance) {
this.databaseInstance = new Database()
}
return this.databaseInstance
}
}
}
export default class RNFirebase {
static initializeApp() {
RNFirebase.firebase = new MockFirebase()
RNFirebase.promises = []
return RNFirebase.firebase
}
static reset() {
RNFirebase.promises = []
RNFirebase.firebase.databaseInstance = null
}
static waitForPromises() {
return Promise.all(RNFirebase.promises)
}
static analytics () {}
static app () {}
}
@laukaichung
Copy link

@echohs28
The library has been updated, and the notification part is very puzzling.

import firebase from 'react-native-firebase';
const FCM = firebase.messaging();
const notification = new firebase.notifications.Notification()
const notifications = firebase.notifications();

The notifications looks like a static object as well as a function. I couldn't make the mock work:

export default class RNFirebase {
    static messaging(){

    }

    static get notifications() {
        return {
            Notification: class Notification {

            }
        }
    }

}

@himanshusinghs
Copy link
Author

@stonecold123 Ahh that's sad. Also I haven't updated this gist for quite some time. Will find time this weekend to update the mock. Thanks for the notification though 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment