Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

firestore-jest-mock

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

firestore-jest-mock - npm Package Compare versions

Comparing version 0.7.2 to 0.8.0

.github/workflows/node.js.yml

75

__tests__/full-setup.test.js
const { mockFirebase } = require('firestore-jest-mock');
const { mockInitializeApp } = require('../mocks/firebase');
const flushPromises = () => new Promise(setImmediate);
const {

@@ -17,2 +19,3 @@ mockGet,

mockSettings,
mockOnSnapShot,
} = require('../mocks/firestore');

@@ -211,3 +214,75 @@

});
test('onSnapshot single doc', async () => {
const db = this.firebase.firestore();
// Example from documentation:
// https://firebase.google.com/docs/firestore/query-data/listen
db.collection('cities')
.doc('LA')
.onSnapshot(doc => {
expect(doc).toHaveProperty('data');
expect(doc.data).toBeInstanceOf(Function);
expect(doc).toHaveProperty('metadata');
});
await flushPromises();
expect(mockOnSnapShot).toHaveBeenCalled();
});
test('onSnapshot can work with options', async () => {
const db = this.firebase.firestore();
// Example from documentation:
// https://firebase.google.com/docs/firestore/query-data/listen
db.collection('cities')
.doc('LA')
.onSnapshot(
{
// Listen for document metadata changes
includeMetadataChanges: true,
},
doc => {
expect(doc).toHaveProperty('data');
expect(doc.data).toBeInstanceOf(Function);
expect(doc).toHaveProperty('metadata');
},
);
await flushPromises();
expect(mockOnSnapShot).toHaveBeenCalled();
});
test('onSnapshot with query', async () => {
const db = this.firebase.firestore();
// Example from documentation:
// https://firebase.google.com/docs/firestore/query-data/listen
const unsubscribe = db
.collection('cities')
.where('state', '==', 'CA')
.onSnapshot(querySnapshot => {
expect(querySnapshot).toHaveProperty('forEach');
expect(querySnapshot).toHaveProperty('docChanges');
expect(querySnapshot).toHaveProperty('docs');
expect(querySnapshot.forEach).toBeInstanceOf(Function);
expect(querySnapshot.docChanges).toBeInstanceOf(Function);
expect(querySnapshot.docs).toBeInstanceOf(Array);
expect(querySnapshot.docChanges().forEach).toBeInstanceOf(Function);
});
await flushPromises();
expect(unsubscribe).toBeInstanceOf(Function);
expect(mockWhere).toHaveBeenCalled();
expect(mockOnSnapShot).toHaveBeenCalled();
});
});
});

@@ -18,2 +18,4 @@ const mockCollectionGroup = jest.fn();

const mockOnSnapShot = jest.fn();
const timestamp = require('./timestamp');

@@ -143,2 +145,31 @@ const fieldValue = require('./fieldValue');

onSnapshot() {
mockOnSnapShot(...arguments);
let callback;
let errorCallback;
// eslint-disable-next-line
let options;
try {
if (typeof arguments[0] === 'function') {
[callback, errorCallback] = arguments;
} else {
[options, callback, errorCallback] = arguments;
}
this.get()
.then(result => {
callback(result);
})
.catch(e => {
throw e;
});
} catch (e) {
errorCallback(e);
}
// Returns an unsubscribe function
return () => {};
}
get() {

@@ -330,2 +361,3 @@ query.mocks.mockGet(...arguments);

mockBatchSet,
mockOnSnapShot,
...query.mocks,

@@ -332,0 +364,0 @@ ...transaction.mocks,

3

mocks/helpers/buildDocFromHash.js

@@ -7,2 +7,5 @@ module.exports = function buildDocFromHash(hash = {}, id = 'abc123') {

ref: hash && hash._ref,
metadata: {
hasPendingWrites: 'Server',
},
data() {

@@ -9,0 +12,0 @@ if (!exists) {

@@ -19,3 +19,12 @@ const buildDocFromHash = require('./buildDocFromHash');

},
docChanges() {
return {
forEach(callback) {
// eslint-disable-next-line no-console
console.info('Firestore jest mock does not currently support tracking changes');
callback();
},
};
},
};
};

@@ -10,2 +10,3 @@ const buildQuerySnapShot = require('./helpers/buildQuerySnapShot');

const mockStartAt = jest.fn();
const mockQueryOnSnapshot = jest.fn();

@@ -71,2 +72,17 @@ class Query {

}
onSnapshot() {
mockQueryOnSnapshot(...arguments);
const [callback, errorCallback] = arguments;
try {
this.get().then(result => {
callback(result);
});
} catch (e) {
errorCallback(e);
}
// Returns an unsubscribe function
return () => {};
}
}

@@ -84,3 +100,4 @@

mockStartAt,
mockQueryOnSnapshot,
},
};

2

package.json
{
"name": "firestore-jest-mock",
"version": "0.7.2",
"version": "0.8.0",
"description": "Jest helper for mocking Google Cloud Firestore",

@@ -5,0 +5,0 @@ "author": "",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc