Skip to content

Instantly share code, notes, and snippets.

@defrian
Forked from himanshusinghs/RNFirebaseMock.js
Created January 23, 2019 22:00
Show Gist options
  • Save defrian/6b1528490a9baadf113a4f4e19f38c01 to your computer and use it in GitHub Desktop.
Save defrian/6b1528490a9baadf113a4f4e19f38c01 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 () {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment