dexie / Dexie.js

A Minimalistic Wrapper for IndexedDB
https://dexie.org
Apache License 2.0
11.49k stars 642 forks source link

How can we get useLiveQuery mocked for unit testing? #2070

Open itmarck opened 1 week ago

itmarck commented 1 week ago

I'm making some unit tests for React components and the hook is not returning anything.

My question is what's the recommended way to implement a mock for a test environment or how can we set a default/initial value? I saw this hook implementation is coupled to the IndexedDb instance, but I guess there should be an alternative to be able to include the useLiveQuery hook on the tests.

dfahlander commented 1 week ago

That's right, If indexedDB isn't present (as it normally is not in node unless polyfilled with fakeIndexedDB or indexedDBShim), it won't ever try to execute the given querier callback. Instead the hook will return the defaultValue (3rd argument to useLiveQuery())

https://dexie.org/docs/dexie-react-hooks/useLiveQuery()#syntax