Mock library for Cloud Firestore
Assuming that you're using npm as your package manager:
npm install --save-dev mock-cloud-firestore
If you're not using any module bundler, you can use the precompiled UMD builds in the dist
folder. For this build, MockFirebase
would be available as a window.MockFirebase
global variable. You can download the files at unpkg.
This accepts the following option:
isNaiveSnapshotListenerEnabled
- When true, changes to any data would cause theonSnapshot()
to fire. Otherwise,onSnapshot()
won't get realtime updates.
Name | Type | Attributes | Description |
---|---|---|---|
fixtureData | Object | ||
option | Object | optional |
You'll need to replace the Firebase instance that you're app is using. That would depend on how you use it. Below are some sample use-cases:
Example 1 - Overriding the Firebase global variable
import MockFirebase from 'mock-cloud-firestore';
window.firebase = new MockFirebase(fixtureData);
const db = firebase.firestore();
db.collection('users').add({ ... });
Example 2 - Dependency injection
import MockFirebase from 'mock-cloud-firestore';
function addUser(firebase) {
return firebase.firestore.collection('users').add({ ... });
}
const firebase = new MockFirebase(fixtureData);
addUser(firebase);
Here's a sample fixture data
const fixtureData = {
__collection__: {
users: {
__doc__: {
user_a: {
age: 15,
username: 'user_a',
__collection__: {
friends: {
__doc__: {
user_b: {
reference: '__ref__:users/user_b'
}
}
}
}
},
user_b: {
age: 10,
username: 'user_b',
__collection__: {
friends: {
__doc__: {
user_a: {
reference: '__ref__:users/user_a'
}
}
}
}
},
user_c: {
age: 20,
username: 'user_c'
}
}
}
}
}
Here's whats going on with the example above
- There will be a
users
collection - There will be
user_a
,user_b
, anduser_c
documents underusers
collection - There will be a
friends
subcollection underuser_a
anduser_b
documents __ref__:
indicates that this is aReference
data type to a document__ref__:users/user_a
will be equivalent tofirestore.collection('users').doc('user_a')
Not all APIs are supported. Here are some unsupported major ones
- Transaction
onSnapshot()
is supported but doesn't get realtime updates by default.- A naive listener is available in that changes to any data would cause it to fire.
You can look into the source code to see if an API you're using is supported. I've written it in a way that you could quickly scan the APIs.
Help me out for a couple of 🍻!