wxt-zustand
Version:
High-performance Zustand state management for WXT web extensions with seamless cross-tab synchronization and sub-10ms React re-renders
94 lines • 3.89 kB
JavaScript
import { beforeEach, describe, expect, mock, test } from 'bun:test';
const readyMod = await import('./ready');
const { wxtZustandStoreReady, __setReadyImplementations, __resetReadyImplementations, } = readyMod;
// Shared mocks
const connectAndFetchInitialState = mock(async (_storeName) => ({
service: {},
initialState: { a: 100 },
}));
const setupBidirectionalSync = mock(() => ({
service: {},
unsubscribeLocal: () => { },
unwatchRemote: () => { },
cleanup: () => { },
}));
function makeFakeStore(initial) {
let state = initial;
const subs = new Set();
return {
getState: () => state,
getInitialState: (() => initial),
setState: ((partial, replace) => {
const prev = state;
state = replace
? partial
: { ...state, ...partial };
subs.forEach((cb) => {
cb(state, prev);
});
}),
subscribe: (cb) => {
subs.add(cb);
return () => subs.delete(cb);
},
// Unused by our code path but part of StoreApi in some typings
destroy: (() => { }),
setStateFromDevtools: undefined,
};
}
describe('wxtZustandStoreReady orchestration', () => {
beforeEach(() => {
connectAndFetchInitialState.mockClear();
setupBidirectionalSync.mockClear();
__resetReadyImplementations();
__setReadyImplementations({
connectAndFetchInitialState,
setupBidirectionalSync,
});
});
test('applies initial state (replace) and wires sync once', async () => {
const store = makeFakeStore({ a: 1 });
await wxtZustandStoreReady('alpha', store, { storageStrategy: 'local' });
// connect called exactly once
expect(connectAndFetchInitialState).toHaveBeenCalledTimes(1);
expect(connectAndFetchInitialState.mock.calls[0][0]).toBe('alpha');
// state replaced (not merged)
expect(store.getState()).toEqual({ a: 100 });
// sync wired exactly once with correct args
expect(setupBidirectionalSync).toHaveBeenCalledTimes(1);
const [nameArg, storeArg] = setupBidirectionalSync.mock.calls[0] || [];
expect(nameArg).toBe('alpha');
expect(storeArg).toBe(store);
});
test('caches readiness per store+name (no duplicate connect/sync)', async () => {
const store = makeFakeStore({ a: 0 });
await wxtZustandStoreReady('beta', store);
await wxtZustandStoreReady('beta', store);
expect(connectAndFetchInitialState).toHaveBeenCalledTimes(1);
expect(setupBidirectionalSync).toHaveBeenCalledTimes(1);
});
test('parallel calls share the same readiness promise', async () => {
// Make connect return only after a tick so both calls start before resolution
connectAndFetchInitialState.mockImplementationOnce(async (_name) => {
await new Promise((r) => setTimeout(r, 10));
return { service: {}, initialState: { a: 5 } };
});
const store = makeFakeStore({ a: 0 });
await Promise.all([
wxtZustandStoreReady('gamma', store),
wxtZustandStoreReady('gamma', store),
]);
expect(connectAndFetchInitialState).toHaveBeenCalledTimes(1);
expect(setupBidirectionalSync).toHaveBeenCalledTimes(1);
expect(store.getState()).toEqual({ a: 5 });
});
test('different names have isolated readiness', async () => {
const store = makeFakeStore({ a: 0 });
await wxtZustandStoreReady('d1', store);
await wxtZustandStoreReady('d2', store);
// One connect per distinct name
expect(connectAndFetchInitialState).toHaveBeenCalledTimes(2);
expect(setupBidirectionalSync).toHaveBeenCalledTimes(2);
});
});
//# sourceMappingURL=ready.orchestration.test.js.map