parkzen-helper
v2.1.2
Published
parkzen helpers
Downloads
76
Readme
parkzen-helpers
Installation
npm install parkzen-helper
firebase-mock
- You can mock your firebase operations using firebase-mock. It currently supports firestore and storage.
require("module-alias/register");
const firebaseMock = require("@src/firebase");
const chai = require("chai");
const expect = chai.expect;
const chaiAsPromised = require('chai-as-promised');
const admin = firebaseMock.createAdmin();
const firestore = admin.firestore();
const storage = admin.storage();
chai.use(chaiAsPromised);
// Example for simulating a get document using firestore
it("Should get document using firestore", async () => {
const totalFlushes = 1; // Total number firestore operation awaits. (Meaning get, set or update)
firebaseMock.flush(totalFlushes);
await firestore.collection("SomeCollection").doc("SomeDoc").get();
})
// Example for simulating two get documents using firestore
it("Should get two document using firestore", async () => {
const totalFlushes = 2; // Total number firestore operation awaits. (Meaning get, set or update)
firebaseMock.flush(totalFlushes);
await firestore.collection("SomeCollection").doc("SomeDoc1").get();
await firestore.collection("SomeCollection").doc("SomeDoc2").get();
})
// Example for simulating two get documents with one failing using firestore.
// Pass in a list of failing steps to simulate firestore operation fails.
it("Should fail getting second document using firestore. (Fail second firestore get)", async () => {
const failFlushes = [2]; //fails the second await.
const totalFlushes = 2; // Total number firestore operation awaits. (Meaning get, set or update)
firebaseMock.flush(totalFlushes, failFlushes);
await firestore.collection("SomeCollection").doc("SomeDoc1").get();
await expect(
firestore.collection("SomeCollection").doc("SomeDoc2").get()
).to.be.rejectedWith(Error);
})
it("Should clear firestore collection of given name.", async () => {
firebaseMock.flush(3);
await firestore.collection("SomeCollection")
.add({
"someset": "value"
})
await firestore.collection("SomeCollection")
.add({
"someset": "value"
})
const snapshot = await firestore.collection("SomeCollection").get();
const count = snapshot.size;
expect(count).to.equal(2);
firebaseMock.flush(2);
await firebaseMock.clearFirestoreCollection("SomeCollection");
const snapshot2 = await firestore.collection("SomeCollection").get();
const count2 = snapshot2.size;
expect(count2).to.equal(0);
})
it("Should get firestore documents with fields equal to query value.", async () => {
firebaseMock.flush(3);
const value = stringHelper.randomString();
const queryDocument = {
"name": value
}
const unrelatedDocument = {
"name": "Some Other Value"
}
await firestore.collection("SomeCollection")
.add(queryDocument)
await firestore.collection("SomeCollection")
.add(unrelatedDocument)
const snapshot = await firestore
.collection("SomeCollection")
.where("name", "==", value)
.get();
expect(snapshot.docs.length, 1);
expect(snapshot.docs[0].data()).to.deep.equal(queryDocument);
})
it("Should get firestore documents with array field containing query value.", async () => {
firebaseMock.flush(3);
const queryDocument = {
"array": stringHelper.randomStringArray(10000)
}
const unrelatedDocument = {
"array": stringHelper.randomStringArray(10000)
}
await firestore.collection("SomeCollection")
.add(queryDocument)
await firestore.collection("SomeCollection")
.add(unrelatedDocument)
const randQueryIndex = numberHelper.randomNumber(10000);
const snapshot = await firestore
.collection("SomeCollection")
.where("array", "array-contains", queryDocument["array"][randQueryIndex])
.get();
expect(snapshot.docs.length, 1);
expect(snapshot.docs[0].data()).to.deep.equal(queryDocument);
})
it("Should get firestore documents with query value in array.", async () => {
firebaseMock.flush(3);
const values = stringHelper.randomStringArray(30); // "in supports upto 30 comparisions"
const queryDocument = {
"id": values[0]
}
const unrelatedDocument = {
"id": "Some Value"
}
await firestore.collection("SomeCollection")
.add(queryDocument)
await firestore.collection("SomeCollection")
.add(unrelatedDocument)
const snapshot = await firestore
.collection("SomeCollection")
.where("id", "in", values)
.get();
expect(snapshot.docs.length, 1);
expect(snapshot.docs[0].data()).to.deep.equal(queryDocument);
})
// Example for uploading file from storage.
it("Should upload document using storage", async () => {
const bucket = storage.bucket();
const data = "this is some data";
await bucket.file("example").save(data);
})
// Example for getting file from storage.
it("Should download document using storage", async () => {
const bucket = storage.bucket();
const data = "this is some data";
await bucket.file("example").save(data);
const contents = await bucket.file("example").download();
expect(contents.toString(), data);
})
// Example for failing storage download operation.
it("Should fail downloading document using storage.", async () => {
firebaseMock.setStorageFailStub();
const bucket = storage.bucket();
await expect(
bucket.file("example").download()
).to.be.rejectedWith(Error);
firebaseMock.removeStorageFailStub();
})
// Example for failing storage download operation.
it("Should fail saving document using storage.", async () => {
firebaseMock.setStorageFailStub();
const bucket = storage.bucket();
await expect(
bucket.file("example").save("Some Data")
).to.be.rejectedWith(Error);
firebaseMock.removeStorageFailStub();
})