This commit is contained in:
Martti Malmi 2023-09-11 16:52:23 +03:00
parent 7145ebcfb7
commit 1f43338644
2 changed files with 70 additions and 3 deletions

View File

@ -0,0 +1,70 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import LocalStorageMemoryAdapter from '@/state/LocalStorageMemoryAdapter.ts';
import { Callback, Unsubscribe } from '@/state/types.ts';
describe('LocalStorageMemoryAdapter', () => {
let adapter: LocalStorageMemoryAdapter;
beforeEach(() => {
localStorage.clear();
adapter = new LocalStorageMemoryAdapter();
});
describe('get()', () => {
it('should retrieve the stored value for a given path', () => {
const mockCallback: Callback = vi.fn();
adapter.set('somePath', { value: 'someValue', updatedAt: Date.now() });
const unsubscribe: Unsubscribe = adapter.get('somePath', mockCallback);
expect(mockCallback).toHaveBeenCalledWith(
'someValue',
'somePath',
expect.any(Number),
expect.any(Function),
);
unsubscribe();
});
});
describe('set()', () => {
it('should set the value at the given path', async () => {
await adapter.set('anotherPath', { value: 'newValue', updatedAt: Date.now() });
const mockCallback: Callback = vi.fn();
adapter.get('anotherPath', mockCallback);
expect(mockCallback).toHaveBeenCalledWith(
'newValue',
'anotherPath',
expect.any(Number),
expect.any(Function),
);
});
});
describe('list()', () => {
it('should list child nodes under the given path', () => {
const mockCallback: Callback = vi.fn();
adapter.set('parent/child1', { value: 'childValue1', updatedAt: Date.now() });
adapter.set('parent/child2', { value: 'childValue2', updatedAt: Date.now() });
const unsubscribe: Unsubscribe = adapter.list('parent', mockCallback);
expect(mockCallback).toHaveBeenCalledWith(
'childValue1',
'parent/child1',
expect.any(Number),
expect.any(Function),
);
expect(mockCallback).toHaveBeenCalledWith(
'childValue2',
'parent/child2',
expect.any(Number),
expect.any(Function),
);
unsubscribe();
});
});
});

View File

@ -56,12 +56,9 @@ export default class LocalStorageMemoryAdapter extends Adapter {
}
async set(path: string, value: NodeValue) {
await this.loadingPromise;
if (value.updatedAt === undefined) {
throw new Error(`Invalid value: ${JSON.stringify(value)}`);
}
this.storage.set(path, value);
localStorage.setItem(path, JSON.stringify(value));
}