Created
October 10, 2018 13:36
-
-
Save laiso/5930572e3e202421411738275168ad70 to your computer and use it in GitHub Desktop.
Jest testing with mock of Cloud Firestore SDK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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