From 7d18f2f7138219dd5dc51f63073195c8af90e4c4 Mon Sep 17 00:00:00 2001 From: Jay Meistrich Date: Thu, 4 Jul 2024 15:24:52 -0700 Subject: [PATCH] add a test for persisting map --- tests/persist.test.ts | 73 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/tests/persist.test.ts b/tests/persist.test.ts index 8efac615..6c59c520 100644 --- a/tests/persist.test.ts +++ b/tests/persist.test.ts @@ -532,7 +532,7 @@ describe('persist objects', () => { expect(JSON.stringify(draftBom2$.get())).toEqual(JSON.stringify({ addItem, deleteItem, items: ['hi'] })); }); - test('Map set works correctly', async () => { + test('Map merges from persistence correctly', async () => { interface TableState { columnFilters: any; sorting: any; @@ -637,6 +637,77 @@ describe('persist objects', () => { ]), }); }); + test('Map merges from persistence correctly', async () => { + interface TableState { + columnFilters: any; + sorting: any; + pagination: any; + search: string; + } + + const obs$ = observable({ + m: new Map([ + ['v1', { h1: 'h1', h2: [] }], + ['v2', { h1: 'h3', h2: [1] }], + ]), + }); + + configureObservableSync({ + persist: { + plugin: ObservablePersistLocalStorage, + }, + }); + + const persistName = getPersistName(); + + syncObservable(obs$, { + persist: { + name: persistName, + }, + }); + + expect(obs$.get()).toEqual({ + m: new Map([ + ['v1', { h1: 'h1', h2: [] }], + ['v2', { h1: 'h3', h2: [1] }], + ]), + }); + + // Set h2 on v2 + obs$.m.get('v2').h2.set([]); + + expect(obs$.get()).toEqual({ + m: new Map([ + ['v1', { h1: 'h1', h2: [] }], + ['v2', { h1: 'h3', h2: [] }], + ]), + }); + + await promiseTimeout(0); + + expect(localStorage.getItem(persistName)).toEqual( + '{"m":{"__LSType":"Map","value":[["v1",{"h1":"h1","h2":[]}],["v2",{"h2":[]}]]}}', + ); + + const obs2$ = observable({ + m: new Map([ + ['v1', { h1: 'h1', h2: [] }], + ['v2', { h1: 'h3', h2: [1] }], + ]), + }); + syncObservable(obs2$, { + persist: { + name: persistName, + }, + }); + + expect(obs2$.get()).toEqual({ + m: new Map([ + ['v1', { h1: 'h1', h2: [] }], + ['v2', { h1: 'h3', h2: [] }], + ]), + }); + }); }); describe('get mode', () => { test('synced get sets by default', async () => {