Skip to content

Commit

Permalink
add a test for persisting map
Browse files Browse the repository at this point in the history
  • Loading branch information
jmeistrich committed Jul 4, 2024
1 parent 5555c0d commit 7e7cfea
Showing 1 changed file with 65 additions and 1 deletion.
66 changes: 65 additions & 1 deletion tests/persist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -637,6 +637,70 @@ describe('persist objects', () => {
]),
});
});
test('Map merges from persistence correctly', async () => {
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 () => {
Expand Down

0 comments on commit 7e7cfea

Please sign in to comment.