diff --git a/tests/persist.test.ts b/tests/persist.test.ts index e9624d7c0..cbff2f593 100644 --- a/tests/persist.test.ts +++ b/tests/persist.test.ts @@ -1,11 +1,36 @@ import 'fake-indexeddb/auto'; import { transformOutData, persistObservable } from '../src/persist/persistObservable'; import { ObservablePersistLocalStorage } from '../src/persist-plugins/local-storage'; +import { Change } from '../src/observableInterfaces'; +import { observe } from '../src/observe'; +import { observable } from '../src/observable'; +import { when } from '../src/when'; function promiseTimeout(time?: number) { return new Promise((resolve) => setTimeout(resolve, time || 0)); } +class LocalStorageMock { + store: Record; + constructor() { + this.store = {}; + } + clear() { + this.store = {}; + } + getItem(key: string) { + return this.store[key] || null; + } + setItem(key: string, value: any) { + this.store[key] = String(value); + } + removeItem(key: string) { + delete this.store[key]; + } +} +// @ts-expect-error This is ok to do in jest +global.localStorage = new LocalStorageMock(); + describe('Creating', () => { test('Create with object', () => { const obs$ = persistObservable( @@ -41,6 +66,35 @@ describe('Creating', () => { await promiseTimeout(10); expect(setValue).toEqual('hello'); }); + test('Loading state works correctly', async () => { + const nodes = observable<{ key: string }[]>([]); + let lastSet; + const { state } = persistObservable(nodes, { + pluginLocal: ObservablePersistLocalStorage, + pluginRemote: { + get: async () => { + const nodes = await new Promise<{ key: string }[]>((resolve) => + setTimeout(() => resolve([{ key: 'key0' }]), 10), + ); + return nodes.reduce( + (acc, node) => { + acc[node.key] = node; + return acc; + }, + {} as Record, + ); + }, + set: async ({ value }: { value: any; changes: Change[] }) => { + lastSet = value; + }, + }, + local: 'nodes', + }); + + await when(state.isLoadedLocal); + await when(state.isLoaded); + expect(lastSet).toEqual(undefined); + }); }); describe('Adjusting data', () => {