firestore-jest-mock
Advanced tools
Comparing version 0.7.2 to 0.8.0
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, |
@@ -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, | ||
}, | ||
}; |
{ | ||
"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": "", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
119584
31
1764