Skip to content

Instantly share code, notes, and snippets.

@laiso
Created October 10, 2018 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laiso/5930572e3e202421411738275168ad70 to your computer and use it in GitHub Desktop.
Save laiso/5930572e3e202421411738275168ad70 to your computer and use it in GitHub Desktop.
Jest testing with mock of Cloud Firestore SDK
const admin = require('firebase-admin')
class Notification {
constructor(doc, firestore = admin.firestore()) {
this.doc = doc
this.firestore = firestore
}
notify(toUser) {
const {user} = this.doc.data()
this.firestore.collection('notifications')
.doc()
.set({
created_at: this.firestore.FieldValue.serverTimestamp(),
user: {
id: toUser.id
}
})
}
}
it(`notification parameter is valid`, () => {
const setParams = jest.fn()
const firestore = {
FieldValue: {
serverTimestamp: () => new Date()
},
collection: (name) => ({
doc: () => ({
set: setParams
})
})
}
const stub = {
user: {name: 'Evan'},
image: {url: 'http://...'},
}
const notification = new Notification({data: () => stub}, firestore)
notification.notify({id: 'FRIEND_USER_ID'})
const params = setParams.mock.calls[0][0]
expect(params.user.id).toBe('FRIEND_USER_ID')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment