2.1.2 • Published 2 years ago
parkzen-helper v2.1.2
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();
})
2.0.37
2 years ago
2.0.38
2 years ago
2.0.35
2 years ago
2.0.36
2 years ago
2.0.33
2 years ago
2.0.34
2 years ago
2.0.39
2 years ago
2.0.46
2 years ago
2.0.44
2 years ago
2.0.45
2 years ago
2.0.42
2 years ago
2.0.43
2 years ago
2.0.40
2 years ago
2.0.41
2 years ago
2.1.2
2 years ago
2.1.1
2 years ago
2.1.0
2 years ago
2.0.28
2 years ago
2.0.29
2 years ago
2.0.31
2 years ago
2.0.32
2 years ago
2.0.30
2 years ago
2.0.26
2 years ago
2.0.27
2 years ago
2.0.25
2 years ago
2.0.24
2 years ago
2.0.23
2 years ago
2.0.22
2 years ago
2.0.21
2 years ago
2.0.20
2 years ago
2.0.19
2 years ago
2.0.18
2 years ago
2.0.17
2 years ago
2.0.16
2 years ago
2.0.15
2 years ago
2.0.14
2 years ago
2.0.13
2 years ago
2.0.12
2 years ago
2.0.11
2 years ago
2.0.10
2 years ago
2.0.9
2 years ago
2.0.8
2 years ago
2.0.7
2 years ago
2.0.6
2 years ago
2.0.5
2 years ago
2.0.4
2 years ago
2.0.3
2 years ago
2.0.2
2 years ago
2.0.1
2 years ago
2.0.0
2 years ago
1.0.7
2 years ago
1.0.6
2 years ago
1.0.5
2 years ago
1.0.4
2 years ago
1.0.3
2 years ago
1.0.2
2 years ago
1.0.0
2 years ago