Skip to content

Commit

Permalink
fix: allow storeSplit to be used in hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
mesqueeb committed Jun 4, 2024
1 parent 1d75d87 commit 788a01f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/moduleActions/handleWritePerStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ export function handleWritePerStore(
}>((dic, storeName) => ({ ...dic, [storeName]: unwrapStoreSplits(payload, storeName) }), {})

for (const modifyFn of modifyPayloadFnsMap[actionName]) {
storePayloadDic = mapObject(storePayloadDic, (payloadValue) =>
modifyFn(payloadValue, docId),
storePayloadDic = mapObject(storePayloadDic, (payloadValue, storeName) =>
unwrapStoreSplits(modifyFn(payloadValue, docId), `${storeName}`),
)
}

Expand Down
40 changes: 40 additions & 0 deletions packages/plugin-firestore/test/internal/storeSplit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,43 @@ import { firestoreDeepEqual } from '../helpers/firestoreDeepEqual.js'
assert.deepEqual(doc.data?.base.HP as any, '9000')
})
}
{
const testName = 'write: merge (document) with storeSplit in hook'
test(testName, async () => {
const { pokedexModule } = await createMagnetarInstance(testName, {
insertDocs: { 'pokedex/1': pokedex(1) },
})

const doc = pokedexModule.doc('1')
assert.deepEqual(doc.data, pokedex(1))
await firestoreDeepEqual(testName, 'pokedex/1', pokedex(1))

try {
await doc.merge(
{ base: { HP: 9000 } },
{
syncDebounceMs: 1,
modifyPayloadOn: {
write: (payload) => {
assert.deepEqual(payload, { base: { HP: 9000 } })
return { base: { HP: storeSplit({ cache: 9000, remote: '9000' }) } }
},
},
},
)
} catch (error) {
assert.fail(JSON.stringify(error))
}

const mergedResult = merge(pokedex(1), { base: { HP: 9000 } })

assert.deepEqual(pokedexModule.data.get('1'), mergedResult)
assert.deepEqual(doc.data, mergedResult)

await firestoreDeepEqual(testName, 'pokedex/1', merge(pokedex(1), { base: { HP: '9000' } }))

const fetchedDoc = await doc.fetch({ force: true })
assert.deepEqual(fetchedDoc?.base.HP as any, '9000')
assert.deepEqual(doc.data?.base.HP as any, '9000')
})
}

0 comments on commit 788a01f

Please sign in to comment.