firestore-jest-mock
Advanced tools
Comparing version 0.16.0 to 0.17.0
@@ -160,4 +160,15 @@ const { mockFirebase } = require('firestore-jest-mock'); | ||
}); | ||
test('mocking verify ID token to throw Error', async () => { | ||
const error = new Error('test'); | ||
expect.assertions(1); | ||
mockVerifyIdToken.mockRejectedValueOnce(error); | ||
const result = await this.admin | ||
.auth() | ||
.verifyIdToken('token_string', true) | ||
.catch(err => err); | ||
expect(result).toStrictEqual(error); | ||
}); | ||
}); | ||
}); | ||
}); |
const { mockGoogleCloudFirestore } = require('firestore-jest-mock'); | ||
const flushPromises = () => new Promise(setImmediate); | ||
const { Timestamp } = require('../mocks/timestamp'); | ||
const { | ||
@@ -19,2 +19,4 @@ mockGet, | ||
mockOnSnapShot, | ||
mockListCollections, | ||
mockTimestampNow, | ||
} = require('../mocks/firestore'); | ||
@@ -48,2 +50,4 @@ | ||
afterEach(() => mockTimestampNow.mockClear()); | ||
test('We can start an application', async () => { | ||
@@ -159,5 +163,7 @@ const firestore = new this.Firestore(); | ||
const firestore = new this.Firestore(); | ||
const now = Timestamp._fromMillis(new Date().getTime()); | ||
const washingtonRef = firestore.collection('cities').doc('DC'); | ||
mockTimestampNow.mockReturnValue(now); | ||
return washingtonRef | ||
@@ -167,3 +173,4 @@ .update({ | ||
}) | ||
.then(function() { | ||
.then(function(value) { | ||
expect(value.updateTime).toStrictEqual(now); | ||
expect(mockUpdate).toHaveBeenCalledWith({ capital: true }); | ||
@@ -201,5 +208,56 @@ }); | ||
test('listCollections returns a promise', async () => { | ||
const firestore = new this.Firestore(); | ||
const listCollectionsPromise = firestore | ||
.collection('cities') | ||
.doc('LA') | ||
.listCollections(); | ||
expect(listCollectionsPromise).toEqual(expect.any(Promise)); | ||
}); | ||
test('listCollections resolves with child collections', async () => { | ||
const firestore = new this.Firestore(); | ||
const result = await firestore | ||
.collection('users') | ||
.doc('123abc') | ||
.listCollections(); | ||
expect(result).toEqual(expect.any(Array)); | ||
expect(result).toHaveLength(1); | ||
expect(result[0]).toEqual(expect.any(this.Firestore.CollectionReference)); | ||
expect(result[0].id).toBe('cities'); | ||
}); | ||
test('listCollections resolves with empty array if there are no collections in document', async () => { | ||
const firestore = new this.Firestore(); | ||
const result = await firestore | ||
.collection('users') | ||
.doc('abc123') | ||
.listCollections(); | ||
expect(result).toEqual(expect.any(Array)); | ||
expect(result).toHaveLength(0); | ||
}); | ||
test('listCollections calls mockListCollections', async () => { | ||
const firestore = new this.Firestore(); | ||
await firestore | ||
.collection('users') | ||
.doc('abc123') | ||
.listCollections(); | ||
expect(mockListCollections).toHaveBeenCalled(); | ||
}); | ||
test('onSnapshot single doc', async () => { | ||
const firestore = new this.Firestore(); | ||
const now = Timestamp._fromMillis(new Date().getTime()); | ||
mockTimestampNow.mockReturnValue(now); | ||
firestore | ||
@@ -209,5 +267,9 @@ .collection('cities') | ||
.onSnapshot(doc => { | ||
expect(doc).toHaveProperty('createTime'); | ||
expect(doc).toHaveProperty('data'); | ||
expect(doc.data).toBeInstanceOf(Function); | ||
expect(doc).toHaveProperty('metadata'); | ||
expect(doc).toHaveProperty('readTime'); | ||
expect(doc).toHaveProperty('updateTime'); | ||
expect(doc.readTime).toStrictEqual(now); | ||
}); | ||
@@ -222,3 +284,6 @@ | ||
const firestore = new this.Firestore(); | ||
const now = Timestamp._fromMillis(new Date().getTime()); | ||
mockTimestampNow.mockReturnValue(now); | ||
firestore | ||
@@ -233,5 +298,9 @@ .collection('cities') | ||
doc => { | ||
expect(doc).toHaveProperty('createTime'); | ||
expect(doc).toHaveProperty('data'); | ||
expect(doc.data).toBeInstanceOf(Function); | ||
expect(doc).toHaveProperty('metadata'); | ||
expect(doc).toHaveProperty('readTime'); | ||
expect(doc).toHaveProperty('updateTime'); | ||
expect(doc.readTime).toStrictEqual(now); | ||
}, | ||
@@ -238,0 +307,0 @@ ); |
@@ -14,3 +14,3 @@ describe.each` | ||
const flushPromises = () => new Promise(setImmediate); | ||
const { Timestamp } = require('../mocks/timestamp'); | ||
const { | ||
@@ -36,2 +36,3 @@ mockGet, | ||
mockQueryOnSnapshot, | ||
mockTimestampNow, | ||
} = require('../mocks/firestore'); | ||
@@ -78,2 +79,4 @@ | ||
afterEach(() => mockTimestampNow.mockClear()); | ||
test('We can start an application', async () => { | ||
@@ -231,3 +234,6 @@ const db = firebase.firestore(); | ||
const washingtonRef = db.collection('cities').doc('DC'); | ||
const now = Timestamp._fromMillis(new Date().getTime()); | ||
mockTimestampNow.mockReturnValue(now); | ||
// Set the "capital" field of the city 'DC' | ||
@@ -238,3 +244,4 @@ return washingtonRef | ||
}) | ||
.then(function() { | ||
.then(function(value) { | ||
expect(value.updateTime).toStrictEqual(now); | ||
expect(mockUpdate).toHaveBeenCalledWith({ capital: true }); | ||
@@ -275,5 +282,18 @@ }); | ||
test('listCollections method does not exist', async () => { | ||
const db = firebase.firestore(); | ||
expect(() => { | ||
db.collection('cities') | ||
.doc('LA') | ||
.listCollections(); | ||
}).toThrow(TypeError); | ||
}); | ||
test('onSnapshot single doc', async () => { | ||
const db = firebase.firestore(); | ||
const now = Timestamp._fromMillis(new Date().getTime()); | ||
mockTimestampNow.mockReturnValue(now); | ||
// Example from documentation: | ||
@@ -285,5 +305,9 @@ // https://firebase.google.com/docs/firestore/query-data/listen | ||
.onSnapshot(doc => { | ||
expect(doc).toHaveProperty('createTime'); | ||
expect(doc).toHaveProperty('data'); | ||
expect(doc.data).toBeInstanceOf(Function); | ||
expect(doc).toHaveProperty('metadata'); | ||
expect(doc).toHaveProperty('readTime'); | ||
expect(doc).toHaveProperty('updateTime'); | ||
expect(doc.readTime).toStrictEqual(now); | ||
}); | ||
@@ -299,3 +323,6 @@ | ||
const db = firebase.firestore(); | ||
const now = Timestamp._fromMillis(new Date().getTime()); | ||
mockTimestampNow.mockReturnValue(now); | ||
// Example from documentation: | ||
@@ -312,5 +339,9 @@ // https://firebase.google.com/docs/firestore/query-data/listen | ||
doc => { | ||
expect(doc).toHaveProperty('createTime'); | ||
expect(doc).toHaveProperty('data'); | ||
expect(doc.data).toBeInstanceOf(Function); | ||
expect(doc).toHaveProperty('metadata'); | ||
expect(doc).toHaveProperty('readTime'); | ||
expect(doc).toHaveProperty('updateTime'); | ||
expect(doc.readTime).toStrictEqual(now); | ||
}, | ||
@@ -317,0 +348,0 @@ ); |
@@ -44,4 +44,3 @@ const mockCreateUserWithEmailAndPassword = jest.fn(); | ||
verifyIdToken() { | ||
mockVerifyIdToken(...arguments); | ||
return Promise.resolve(this.currentUserRecord); | ||
return Promise.resolve(mockVerifyIdToken(...arguments) || this.currentUserRecord); | ||
} | ||
@@ -48,0 +47,0 @@ |
@@ -22,2 +22,5 @@ const mockInitializeApp = jest.fn(); | ||
//Remove methods which do not exist in Firebase | ||
delete firestoreConstructor.DocumentReference.prototype.listCollections; | ||
// The Firebase mock | ||
@@ -24,0 +27,0 @@ return { |
@@ -5,3 +5,3 @@ import type { FieldValue } from './fieldValue'; | ||
import type { Transaction } from './transaction'; | ||
import type { FieldPath } from './path' | ||
import type { FieldPath } from './path'; | ||
@@ -8,0 +8,0 @@ import type { MockedDocument, DocumentData } from './helpers/buildDocFromHash'; |
@@ -14,2 +14,3 @@ const mockCollectionGroup = jest.fn(); | ||
const mockListDocuments = jest.fn(); | ||
const mockListCollections = jest.fn(); | ||
@@ -53,6 +54,4 @@ const mockBatchDelete = jest.fn(); | ||
params = params.filter(arg => arg instanceof FakeFirestore.DocumentReference); | ||
return Promise.all( | ||
transaction.mocks.mockGetAll(...params) || [...params].map(r => r.get()), | ||
); | ||
return Promise.all(transaction.mocks.mockGetAll(...params) || [...params].map(r => r.get())); | ||
} | ||
@@ -259,2 +258,18 @@ | ||
listCollections() { | ||
mockListCollections(); | ||
const document = this._getRawObject(); | ||
if (!document._collections) { | ||
return Promise.resolve([]); | ||
} | ||
const collectionRefs = []; | ||
for (const collectionId of Object.keys(document._collections)) { | ||
collectionRefs.push(new FakeFirestore.CollectionReference(collectionId, this)); | ||
} | ||
return Promise.resolve(collectionRefs); | ||
} | ||
delete() { | ||
@@ -304,3 +319,5 @@ mockDelete(...arguments); | ||
} | ||
return Promise.resolve(buildDocFromHash({ ...object, _ref: this })); | ||
return Promise.resolve( | ||
buildDocFromHash({ ...object, _ref: this, _updateTime: timestamp.Timestamp.now() }), | ||
); | ||
} | ||
@@ -311,3 +328,5 @@ | ||
this.firestore._updateData(this.path, object, setOptions.merge); | ||
return Promise.resolve(buildDocFromHash({ ...object, _ref: this })); | ||
return Promise.resolve( | ||
buildDocFromHash({ ...object, _ref: this, _updateTime: timestamp.Timestamp.now() }), | ||
); | ||
} | ||
@@ -343,3 +362,7 @@ | ||
_get() { | ||
/** | ||
* A private method for internal use. | ||
* @returns {Object|null} The raw object of the document or null. | ||
*/ | ||
_getRawObject() { | ||
// Ignore leading slash | ||
@@ -358,3 +381,3 @@ const pathArray = this.path.replace(/^\/+/, '').split('/'); | ||
} else { | ||
return { exists: false, data: () => undefined, id: this.id, ref: this }; | ||
return null; | ||
} | ||
@@ -367,7 +390,7 @@ | ||
if (!document || !document._collections) { | ||
return { exists: false, data: () => undefined, id: this.id, ref: this }; | ||
return null; | ||
} | ||
requestedRecords = document._collections[collectionId] || []; | ||
if (requestedRecords.length === 0) { | ||
return { exists: false, data: () => undefined, id: this.id, ref: this }; | ||
return null; | ||
} | ||
@@ -377,3 +400,3 @@ | ||
if (!document) { | ||
return { exists: false, data: () => undefined, id: this.id, ref: this }; | ||
return null; | ||
} | ||
@@ -385,6 +408,25 @@ | ||
if (!!document || false) { | ||
return document; | ||
} | ||
return null; | ||
} | ||
_get() { | ||
const document = this._getRawObject(); | ||
if (document) { | ||
document._ref = this; | ||
document._readTime = timestamp.Timestamp.now(); | ||
return buildDocFromHash(document); | ||
} else { | ||
return { | ||
createTime: undefined, | ||
exists: false, | ||
data: () => undefined, | ||
id: this.id, | ||
readTime: undefined, | ||
ref: this, | ||
updateTime: undefined, | ||
}; | ||
} | ||
return { exists: false, data: () => undefined, id: this.id, ref: this }; | ||
} | ||
@@ -516,2 +558,3 @@ | ||
mockListDocuments, | ||
mockListCollections, | ||
...query.mocks, | ||
@@ -518,0 +561,0 @@ ...transaction.mocks, |
@@ -8,8 +8,13 @@ import type { FakeFirestore, FakeFirestoreDatabase } from '../firestore'; | ||
_collections: FakeFirestoreDatabase; | ||
_createTime?: typeof FakeFirestore.Timestamp; | ||
_readTime?: typeof FakeFirestore.Timestamp; | ||
_ref: typeof FakeFirestore.DocumentReference; | ||
_updateTime?: typeof FakeFirestore.Timestamp; | ||
} | ||
export interface MockedDocument<T = DocumentData> { | ||
createTime: typeof FakeFirestore.Timestamp; | ||
exists: boolean; | ||
id: string; | ||
readTime: typeof FakeFirestore.Timestamp; | ||
ref: typeof FakeFirestore.DocumentReference; | ||
@@ -19,2 +24,3 @@ metadata: { | ||
}; | ||
updateTime: typeof FakeFirestore.Timestamp; | ||
data(): T | undefined; | ||
@@ -21,0 +27,0 @@ get(fieldPath: string): unknown; |
@@ -0,6 +1,10 @@ | ||
const timestamp = require('../timestamp'); | ||
module.exports = function buildDocFromHash(hash = {}, id = 'abc123') { | ||
const exists = !!hash || false; | ||
return { | ||
createTime: (hash && hash._createTime) || timestamp.Timestamp.now(), | ||
exists, | ||
id: (hash && hash.id) || id, | ||
readTime: hash && hash._readTime, | ||
ref: hash && hash._ref, | ||
@@ -10,2 +14,3 @@ metadata: { | ||
}, | ||
updateTime: hash && hash._updateTime, | ||
data() { | ||
@@ -22,3 +27,6 @@ if (!exists) { | ||
delete copy._collections; | ||
delete copy._createTime; | ||
delete copy._readTime; | ||
delete copy._ref; | ||
delete copy._updateTime; | ||
return copy; | ||
@@ -25,0 +33,0 @@ }, |
{ | ||
"name": "firestore-jest-mock", | ||
"version": "0.16.0", | ||
"version": "0.17.0", | ||
"description": "Jest helper for mocking Google Cloud Firestore", | ||
@@ -5,0 +5,0 @@ "author": "", |
203395
3876