Skip to content

Commit

Permalink
test: more CommonDao patch tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Mar 29, 2024
1 parent 72c2e9a commit 2833669
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/commondao/common.dao.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,47 @@ test('patch', async () => {
`)
})

test('patch cyclerStatus-like', async () => {
const item: TestItemBM = await dao.save({
id: 'id1',
k1: 'k1',
})

// We mutate it, but it wasn't yet saved to DB
item.k1 = 'k2'

// Now we gonna call `patch`
// It should compare item+patch with loaded (not loaded+patch with item!), and still do save
await dao.patch(item, { k1: 'k2' })
const loaded = await dao.requireById('id1')
expect(loaded.k1).toBe('k2')
})

test('patch where item is stale', async () => {
const item: TestItemBM = await dao.save({
id: 'id1',
k1: 'k1',
})

// Some external process saves k2 (different property, which was undefined)
await dao.save({
...item,
k2: 'k2',
})

// item stays as before
expect(item.k2).toBeUndefined()

// The patch should succeed, but item should be patched with k2=k2
await dao.patch(item, { k1: 'k2' })
expect(item.k2).toBe('k2')
const loaded = await dao.requireById('id1')
expect(loaded).toMatchObject({
k1: 'k2',
k2: 'k2',
})
})

// todo: fix jest mock
test.skip('ensureUniqueId', async () => {
const opt: CommonDaoSaveBatchOptions<TestItemBM> = {
Expand Down

0 comments on commit 2833669

Please sign in to comment.