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 7d18f2f
Showing 1 changed file with 72 additions and 1 deletion.
73 changes: 72 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,77 @@ describe('persist objects', () => {
]),
});
});
test('Map merges from persistence correctly', async () => {
interface TableState {

Check failure on line 641 in tests/persist.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'TableState' is defined but never used
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 () => {
Expand Down

0 comments on commit 7d18f2f

Please sign in to comment.