Adding user to list now only works online, and it never queued #4

Merged
robin merged 1 commits from dev into main 2026-08-21 22:35:01 +02:00
3 changed files with 40 additions and 12 deletions
-1
View File
@@ -14,7 +14,6 @@ export type SyncOperationType =
| 'createListItem'
| 'updateListItem'
| 'setListItemCompleted'
| 'addUserToList'
| 'removeUserFromList'
| 'deleteList'
| 'deleteListItem'
+36
View File
@@ -319,4 +319,40 @@ describe('useListsStore', () => {
expect(listsApiMocks.deleteListItemApi).toHaveBeenCalledWith('item-to-delete')
expect(store.pendingCount).toBe(0)
})
it('shares list with server when online without adding to sync queue', async () => {
listsApiMocks.addUserToListApi.mockResolvedValueOnce(undefined)
const store = useListsStore()
await store.addUserToList('list-1', 'friend@example.com')
expect(listsApiMocks.addUserToListApi).toHaveBeenCalledWith({
list_id: 'list-1',
email: 'friend@example.com',
})
expect(store.pendingCount).toBe(0)
})
it('throws error when sharing list while offline without calling API or adding to sync queue', async () => {
Object.defineProperty(navigator, 'onLine', { value: false, configurable: true })
const store = useListsStore()
await expect(store.addUserToList('list-1', 'friend@example.com')).rejects.toThrow(
'Cannot share list while offline',
)
expect(listsApiMocks.addUserToListApi).not.toHaveBeenCalled()
expect(store.pendingCount).toBe(0)
})
it('propagates error when sharing list fails on server without adding to sync queue', async () => {
listsApiMocks.addUserToListApi.mockRejectedValueOnce(new Error('User not found'))
const store = useListsStore()
await expect(store.addUserToList('list-1', 'unknown@example.com')).rejects.toThrow(
'User not found',
)
expect(store.pendingCount).toBe(0)
})
})
+4 -11
View File
@@ -145,13 +145,10 @@ export const useListsStore = defineStore('lists', () => {
}
async function addUserToList(listId: string, email: string) {
await enqueue({
type: 'addUserToList',
payload: { list_id: listId, email },
localListId: listId,
})
await refresh()
void sync()
if (typeof navigator !== 'undefined' && !navigator.onLine) {
throw new Error('Cannot share list while offline')
}
await addUserToListApi({ list_id: listId, email })
}
async function removeUserFromList(listId: string, email: string) {
@@ -290,10 +287,6 @@ export const useListsStore = defineStore('lists', () => {
await db.listItems.update(entry.localListItemId, { pendingSync: false })
break
}
case 'addUserToList': {
await addUserToListApi(entry.payload as { list_id: string; email: string })
break
}
case 'removeUserFromList': {
await removeUserFromListApi(entry.payload as { list_id: string; email: string })
break