Mock the insert → values → onConflictDoUpdate → returning chain.
(rows: SnapshotRow[])
| 383 | |
| 384 | /** Mock the insert → values → onConflictDoUpdate → returning chain. */ |
| 385 | function mockUpsertReturning(rows: SnapshotRow[]) { |
| 386 | let capturedConflictConfig: Record<string, unknown> | undefined |
| 387 | const onConflictDoUpdate = vi.fn().mockImplementation((config: Record<string, unknown>) => { |
| 388 | capturedConflictConfig = config |
| 389 | return { returning: vi.fn().mockResolvedValue(rows) } |
| 390 | }) |
| 391 | const values = vi.fn().mockReturnValue({ onConflictDoUpdate }) |
| 392 | databaseMock.db.insert = vi.fn().mockReturnValue({ values }) |
| 393 | databaseMock.db.select = vi.fn() |
| 394 | return { values, onConflictDoUpdate, getConflictConfig: () => capturedConflictConfig } |
| 395 | } |
| 396 | |
| 397 | it('inserts a new snapshot in a single atomic upsert', async () => { |
| 398 | const service = new SnapshotService() |