Skip to content

Commit

Permalink
test: add a console warning when persistence is set up but without a …
Browse files Browse the repository at this point in the history
…name
  • Loading branch information
jmeistrich committed Jan 26, 2025
1 parent 472adc2 commit d13bffc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/sync/syncObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,18 @@ async function loadLocal<T>(
].filter(Boolean),
) as unknown as Promise<void>;
} else {
// Console.warn if no persist name is provided. It's ok if name is undefined as that can be the user manually disabling it,
// but if not provided at all it's likely user error.
if (
(process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') &&
persist &&
persist.plugin &&
!Object.hasOwn(persist, 'name')
) {
console.warn(
'[legend-state] Trying to syncObservable without `name` defined. Please include a `name` property in the `persist` configuration.',
);
}
nodeValue.resetPersistence = () => prevResetPersistence?.();
}
// TODOV3 Remove
Expand Down
3 changes: 3 additions & 0 deletions tests/computed-persist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ describe('caching with new computed', () => {
const nodes = observable(
mySynced({
initial: [],
persist: {
name: getPersistName(),
},
}),
);

Expand Down
17 changes: 16 additions & 1 deletion tests/persist-indexeddb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { configureSynced } from '../src/sync/configureSynced';
import { mapSyncPlugins, syncObservable } from '../src/sync/syncObservable';
import type { ObservablePersistPlugin, ObservablePersistPluginOptions } from '../src/sync/syncTypes';
import { when } from '../src/when';
import { promiseTimeout } from './testglobals';
import { expectLog, promiseTimeout } from './testglobals';

const TableNameBase = 'jestlocal';
const tableNames = Array.from({ length: 100 }, (_, i) => TableNameBase + i);
Expand Down Expand Up @@ -371,4 +371,19 @@ describe('Persist IDB', () => {

await expectIDB(persistName, []);
});
test('Persist without a name errors', async () => {
const obs = observable<Record<string, any>>({});

expectLog(
() => {
syncObservable(obs, {
persist: {
plugin: myIndexedDBPlugin,
},
});
},
'[legend-state] Trying to syncObservable without `name` defined. Please include a `name` property in the `persist` configuration.',
'warn',
);
});
});
7 changes: 7 additions & 0 deletions tests/testglobals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,10 @@ export function supressActWarning(fn: () => void) {

console.error = originalError;
}

export function expectLog(fn: () => any, msg: string, logType: 'log' | 'warn' | 'error') {
jest.spyOn(console, logType).mockImplementation(() => {});
fn();
expect(console[logType]).toHaveBeenCalledWith(msg);
(console[logType] as jest.Mock).mockRestore();
}

0 comments on commit d13bffc

Please sign in to comment.