tests for pre-existing content retrieval

This commit is contained in:
Martti Malmi 2023-08-30 15:08:15 +03:00
parent cabbe9a4bf
commit a827d28a7b
3 changed files with 25 additions and 2 deletions

View File

@ -74,6 +74,15 @@ describe('Node', () => {
const result = await node.once(undefined, true);
expect(result).toBe(undefined);
});
it('should return if the data was pre-existing in an adapter', async () => {
const adapter = new MemoryAdapter();
const node = new Node({ id: 'user', adapters: [adapter] });
await node.put({ name: 'Snowden', age: 30 });
const node2 = new Node({ id: 'user', adapters: [adapter] });
const result = await node2.once();
expect(result).toEqual({ name: 'Snowden', age: 30 });
});
});
describe('node.map()', () => {
@ -143,6 +152,21 @@ describe('Node', () => {
unsubscribe();
});
it('should return if the data was pre-existing in an adapter', async () => {
const adapter = new MemoryAdapter();
const node = new Node({ id: 'user', adapters: [adapter] });
await node.put({ name: 'Snowden', age: 30 });
const node2 = new Node({ id: 'user', adapters: [adapter] });
const fn = vi.fn();
node2.map(fn);
expect(fn).toHaveBeenCalledWith(
'Snowden',
'user/name',
expect.any(Number),
expect.any(Function),
);
});
});
describe('Branch node behavior', () => {

View File

@ -12,5 +12,5 @@ export type Callback = (
export abstract class Adapter {
abstract get(path: string, callback: Callback): Unsubscribe;
abstract set(path: string, data: NodeValue): Promise<void>;
// abstract list(path: string, callback: Callback): Unsubscribe; ?
//abstract list(path: string, callback: Callback): Unsubscribe;
}

View File

@ -5,7 +5,6 @@ import localState from '@/state/LocalState.ts';
export default function useLocalState(key: string, initialValue: any = undefined, once = false) {
if (!initialValue) {
localState.get(key).once((val) => {
// TODO some way to get memory value
initialValue = val;
});
}