jakearchibald / idb-keyval

A super-simple-small promise-based keyval store implemented with IndexedDB

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Testing with Jest and Fake Indexed DB

abhi747 opened this issue · comments

This is not an issue but a query.

I need to write unit tests for some functionalities with regards to idb-keyval library that I have integrated.

I am using jest and fake-indexeddb packages for testing.

As per my knowledge, idb-keyval doesn't use concept of indexes. Correct me if wrong.

But for unit testing, I need to setup indexed DB with Fake Indexed DB library, which requires setting up an index with the createIndex method.

How do I simulate same data format stored by idb-keyval with the Fake Indexed DB library, any idea?

As per my knowledge, idb-keyval doesn't use concept of indexes.

Not sure what you mean? It doesn't add anything IDB would consider an index.

But for unit testing, I need to setup indexed DB with Fake Indexed DB library, which requires setting up an index with the createIndex method.

Why does it need an index?

(closing, but I'll continue replying)

Not sure what you mean? It doesn't add anything IDB would consider an index.

Thanks for the quick response. Yes, I meant, that we don't have to set up an index explicitly when working with idb-keyval.

Why does it need an index?

Yes, you are right, index is not mandatory, but when I remove index and add keypath for Fake Indexed DB as follow, something seems wrong in setting up the DB as compared to DB set by idb-keyval, and as a result, my unit test is failing.

const store = db.createObjectStore('keyval', {
          keyPath: 'timeStamp', // 'timeStamp' is used as key while adding records to DB with idb-keyval. So setting it as keyPath here.
        });

The DB setup in the browser is as below. Does anything seem wrong with my approach?

image

The stuff in https://github.com/jakearchibald/idb-keyval/blob/main/custom-stores.md might help, but once you need to do more complex stuff with the database, you might be better off with idb-keyval's sister library: https://www.npmjs.com/package/idb