Last active
March 14, 2019 08:53
-
-
Save himanshusinghs/bd86262ce3c9e6f4d691b5242de707ef to your computer and use it in GitHub Desktop.
React Native Firebase Mock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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 () {} | |
} |
@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
@echohs28
The library has been updated, and the notification part is very puzzling.
The notifications looks like a static object as well as a function. I couldn't make the mock work: