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

@this-dot/cypress-indexeddb

v2.0.1

Published

A Cypress.io helper library for reading and manipulating data inside IndexedDB

Downloads

28,622

Readme

Cypress IndexedDB helpers are a set of custom cypress commands that helps you handle indexedDB related operations in your Cypress tests.

It supports:

✅  Creating new IndexedDB databases and Object Stores ✅  Making CRUD operations on the above-mentioned stores ✅  Read data out of indexedDB and assert the results



Usage

Installation

With Cypress versions above 12.6.0:

  1. Install the package:
    npm install @this-dot/cypress-indexeddb
    or
    yarn add @this-dot/cypress-indexeddb

  2. Import the plugin in your cypress/support/commands.js or cypress/support/commands.ts file:

    import '@this-dot/cypress-indexeddb';

Please note, that cypress introduced the Cypress.Commands.overwriteQuery command in 12.6.0, therefore, cypress versions 12.0.0 to 12.5.x are not supported by this library.

With Cypress versions below 12.0.0:

  1. Install the 1.2.1 version of the package:
    npm install @this-dot/[email protected]
    or
    yarn add @this-dot/[email protected]

  2. Import the plugin in your cypress/support/commands.js or cypress/support/commands.ts file:

    import '@this-dot/cypress-indexeddb';

Using the commands in your Cypress tests

How to clear a database?

In order to make your tests reliable, before every test it is recommended to clear the existing database. You can do it by using the cy.clearIndexedDb('databaseName') command.

beforeEach(() => {
  cy.clearIndexedDb('EXAMPLE_DATABASE');

  // ...

  cy.visit('/');
});

How to create a database connection?

In order to create an object store, first, you need to initiate a database connection by calling the cy.openIndexedDb('databaseName') command and use the as chainer to store it with an alias.

cy.openIndexedDb('EXAMPLE_DATABASE').as('database');

The openIndexedDb command supports providing a database version number optionally

cy.openIndexedDb('EXAMPLE_DATABASE', 12) // initiate with database version 12
  .as('database');

You can access the aliased database with the getIndexedDb('@yourAlias') command.

How to create an Object Store?

You can chain off the createObjectStore('storeName') method from methods that yield an IDBDatabase instance (openIndexedDb or getIndexedDb). You can use the as chainer to save the store using an alias.

cy.getIndexedDb('@database').createObjectStore('example_store').as('exampleStore');

You can also pass an optional options parameter to configure your object store. For example, you can create an object store with autoIncrement with the following command:

cy.getIndexedDb('@database')
  .createObjectStore('example_autoincrement_store', { autoIncrement: true })
  .as('exampleAutoincrementStore');

You can retrieve the saved object store using the cy.getStore('@exampleStore');

How to make CRUD operations on an Object Store?

You can chain off CRUD operation methods from methods that yield an IDBObjectStore instance (createObjectStore or getStore).

The createItem, updateItem and the deleteItem methods yield the store, therefore, you can chain these methods to save multiple entries into the target Object Store.

cy.getStore('@exampleStore')
  .createItem('example', { test: 'test' }) // creates the 'example' key and saves the second parameter as the value.
  .updateItem('example', { test: 'replace' }) // updates the 'example' key's value with the second parameter.
  .deleteItem('example') // deletes the 'example' key
  .createItem('example2', ['testValue', 'testValue2'])
  .createItem('example3', { exampleKey: 1337 });

The readItem method yields the value of the provided key, or undefined if it does not exist. You can chain assertions from this method. If you use TypeScript, you can set the type of the returned value.

cy.getStore('@exampleStore').readItem<string[]>('example2').should('have.length', 2);

cy.getStore('@exampleStore')
  .readItem<number>('example3')
  .should('have.property', 'exampleKey', 1337);

How to handle Object Stores with autoIncrement?

When you need to manipulate or assert data stored in an Object Store, that was set up with { autoIncrement: true }, you have the following commands at your disposal: addItem, keys and entries.

The addItem method stores the provided value into the Object Store at a new index

cy.getStore('@exampleAutoincrementStore').addItem('test').addItem({ test: 'object' }).addItem(1337);

The keys method returns an IDBValidKey[]. You can assert the results using the .should() method.

cy.getStore('@exampleAutoincrementStore')
  .keys()
  .should('have.length', 3)
  .and('deep.equal', [1, 2, 3]);

The entries method returns all the values that are stored in order. You can assert the results using the .should() method.

cy.getStore('@exampleAutoincrementStore')
  .entries()
  .should('have.length', 3)
  .and('deep.equal', ['test', { test: 'object' }, 1337]);

Feel free to check our cypress tests in our git repository for some examples.