2.1.2 • Published 6 months ago

parkzen-helper v2.1.2

Weekly downloads
-
License
ISC
Repository
github
Last release
6 months ago

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

7 months ago

2.0.38

7 months ago

2.0.35

7 months ago

2.0.36

7 months ago

2.0.33

7 months ago

2.0.34

7 months ago

2.0.39

7 months ago

2.0.46

7 months ago

2.0.44

7 months ago

2.0.45

7 months ago

2.0.42

7 months ago

2.0.43

7 months ago

2.0.40

7 months ago

2.0.41

7 months ago

2.1.2

6 months ago

2.1.1

6 months ago

2.1.0

6 months ago

2.0.28

10 months ago

2.0.29

10 months ago

2.0.31

9 months ago

2.0.32

8 months ago

2.0.30

9 months ago

2.0.26

10 months ago

2.0.27

10 months ago

2.0.25

1 year ago

2.0.24

1 year ago

2.0.23

1 year ago

2.0.22

1 year ago

2.0.21

1 year ago

2.0.20

1 year ago

2.0.19

1 year ago

2.0.18

1 year ago

2.0.17

1 year ago

2.0.16

1 year ago

2.0.15

1 year ago

2.0.14

1 year ago

2.0.13

1 year ago

2.0.12

1 year ago

2.0.11

1 year ago

2.0.10

1 year ago

2.0.9

1 year ago

2.0.8

1 year ago

2.0.7

1 year ago

2.0.6

1 year ago

2.0.5

1 year ago

2.0.4

1 year ago

2.0.3

1 year ago

2.0.2

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.0

1 year ago