Skip to content

Commit

Permalink
fix: as "value" returning no results was setting to undefined rather …
Browse files Browse the repository at this point in the history
…than null from a recent commit
  • Loading branch information
jmeistrich committed Nov 2, 2024
1 parent 3a86d00 commit 33f612e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/sync-plugins/crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,13 @@ export function syncedCrud<TRemote extends object, TLocal = TRemote, TAsOption e
// as Promise only if listFn returned a promise
const toOut = (transformed: TLocal[]) => {
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);
}
Expand Down
21 changes: 21 additions & 0 deletions tests/crud.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 33f612e

Please sign in to comment.