npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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();
   })