From 33f612e85b20a16bd87106d07bd63fadcb79ab95 Mon Sep 17 00:00:00 2001 From: Jay Meistrich Date: Sat, 2 Nov 2024 11:25:02 +0000 Subject: [PATCH] fix: as "value" returning no results was setting to undefined rather than null from a recent commit --- src/sync-plugins/crud.ts | 12 +++++++----- tests/crud.test.ts | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/sync-plugins/crud.ts b/src/sync-plugins/crud.ts index 015b8d07..2de4b6eb 100644 --- a/src/sync-plugins/crud.ts +++ b/src/sync-plugins/crud.ts @@ -241,11 +241,13 @@ export function syncedCrud { if (asType === 'value') { - return transformed.length > 0 - ? transformed[0] - : getFn - ? ((((isLastSyncMode && lastSync) || fieldDeleted) && value) ?? null) - : undefined; + if (transformed.length > 0) { + // Return first value + return transformed[0]; + } else { + // Return null if no cached value, otherwise undefined to not overwrite it + return value ? undefined : null; + } } else { return resultsToOutType(transformed); } diff --git a/tests/crud.test.ts b/tests/crud.test.ts index 99ab2dde..004cf476 100644 --- a/tests/crud.test.ts +++ b/tests/crud.test.ts @@ -420,6 +420,27 @@ describe('Crud as value list', () => { await promiseTimeout(1); expect(obs$.get()).toEqual({ id: 'id', test: 'hi', updatedAt: 1 }); + expect(syncState(obs$).isLoaded.get()).toEqual(true); + }); + test('Set to null if returns []', async () => { + const persistName = getPersistName(); + const obs$ = observable( + syncedCrud({ + list: () => promiseTimeout(0, []), + as: 'value', + persist: { + name: persistName, + plugin: ObservablePersistLocalStorage, + }, + }), + ); + + expect(obs$.get()).toEqual(undefined); + + await promiseTimeout(1); + + expect(obs$.get()).toEqual(null); + expect(syncState(obs$).isLoaded.get()).toEqual(true); }); test('sets if returns new value', async () => { const persistName = getPersistName();