Skip to content

Open source personal finance application

fix: resolve test type errors and fix localforage test

Ilya Komichev42fc0f512286
diff --git a/app/components/amount/getTotal.test.ts b/app/components/amount/getTotal.test.ts
index f84cb3d9..63dbbbeb 100644
--- a/app/components/amount/getTotal.test.ts
+++ b/app/components/amount/getTotal.test.ts
@@ -245,5 +245,4 @@ describe('getWalletsTotals', () => {
expect(totals.get('walletRUB')).toBe(700)
expect(totals.get('walletCreditUSD')).toBe(-40)
})
-
})
diff --git a/app/components/offline/replay.test.ts b/app/components/offline/replay.test.ts
index f93fead6..2bef00fc 100644
--- a/app/components/offline/replay.test.ts
+++ b/app/components/offline/replay.test.ts
@@ -1,9 +1,12 @@
-import { describe, expect, it, vi, beforeEach } from 'vitest'
+import { beforeEach, describe, expect, it, vi } from 'vitest'
import type { OfflineOp } from '~/components/offline/types'
import { OfflineEntityType } from '~/components/offline/types'
+// Import once - _isReplaying resets via finally block after each test
+import { isReplaying, replayOfflineQueue } from './replay'
+
// --- Mocks ---
const mockClearOfflineQueue = vi.fn(() => Promise.resolve())
@@ -14,12 +17,12 @@ const mockSetOfflineQueueUserId = vi.fn()
const mockSetQueueUserId = vi.fn(() => Promise.resolve())
vi.mock('~/components/offline/helpers', () => ({
- clearOfflineQueue: (...args: unknown[]) => mockClearOfflineQueue(...args),
- getAllOfflineOps: (...args: unknown[]) => mockGetAllOfflineOps(...args),
- getQueueUserId: (...args: unknown[]) => mockGetQueueUserId(...args),
- removeOfflineOp: (...args: unknown[]) => mockRemoveOfflineOp(...args),
- setOfflineQueueUserId: (...args: unknown[]) => mockSetOfflineQueueUserId(...args),
- setQueueUserId: (...args: unknown[]) => mockSetQueueUserId(...args),
+ clearOfflineQueue: (...args: any[]) => (mockClearOfflineQueue as any)(...args),
+ getAllOfflineOps: (...args: any[]) => (mockGetAllOfflineOps as any)(...args),
+ getQueueUserId: (...args: any[]) => (mockGetQueueUserId as any)(...args),
+ removeOfflineOp: (...args: any[]) => (mockRemoveOfflineOp as any)(...args),
+ setOfflineQueueUserId: (...args: any[]) => (mockSetOfflineQueueUserId as any)(...args),
+ setQueueUserId: (...args: any[]) => (mockSetQueueUserId as any)(...args),
}))
const mockReplayWalletOps = vi.fn(() => Promise.resolve())
@@ -34,11 +37,11 @@ const mockGroupOpsByEntity = vi.fn(() => ({
}))
vi.mock('~/components/offline/replayHelpers', () => ({
- groupOpsByEntity: (...args: unknown[]) => mockGroupOpsByEntity(...args),
- replayCategoryOps: (...args: unknown[]) => mockReplayCategoryOps(...args),
- replaySettingsOps: (...args: unknown[]) => mockReplaySettingsOps(...args),
- replayTrnOps: (...args: unknown[]) => mockReplayTrnOps(...args),
- replayWalletOps: (...args: unknown[]) => mockReplayWalletOps(...args),
+ groupOpsByEntity: (...args: any[]) => (mockGroupOpsByEntity as any)(...args),
+ replayCategoryOps: (...args: any[]) => (mockReplayCategoryOps as any)(...args),
+ replaySettingsOps: (...args: any[]) => (mockReplaySettingsOps as any)(...args),
+ replayTrnOps: (...args: any[]) => (mockReplayTrnOps as any)(...args),
+ replayWalletOps: (...args: any[]) => (mockReplayWalletOps as any)(...args),
}))
let mockUid: string | null = 'user1'
@@ -57,9 +60,9 @@ vi.mock('~/components/trns/useTrnsStore', () => ({
vi.mock('~/components/user/useUserStore', () => ({
useUserStore: () => ({
- get uid() { return mockUid },
saveUserBaseCurrency: vi.fn(),
saveUserLocale: vi.fn(),
+ get uid() { return mockUid },
}),
}))
@@ -80,9 +83,6 @@ function makeOp(entity: OfflineOp['entity'], id: string, type: OfflineOp['type']
return { data, entity, id, timestamp: Date.now(), type }
}
-// Import once - _isReplaying resets via finally block after each test
-import { isReplaying, replayOfflineQueue } from './replay'
-
beforeEach(() => {
vi.clearAllMocks()
mockUid = 'user1'
@@ -161,10 +161,19 @@ describe('replayOfflineQueue', () => {
it('replays phases in order: wallets -> categories -> trns -> settings', async () => {
const callOrder: string[] = []
- mockReplayWalletOps.mockImplementation(async () => { callOrder.push('wallets') })
- mockReplayCategoryOps.mockImplementation(async () => { callOrder.push('categories') })
- mockReplayTrnOps.mockImplementation(async () => { callOrder.push('trns'); return 0 })
- mockReplaySettingsOps.mockImplementation(async () => { callOrder.push('settings') })
+ mockReplayWalletOps.mockImplementation(async () => {
+ callOrder.push('wallets')
+ })
+ mockReplayCategoryOps.mockImplementation(async () => {
+ callOrder.push('categories')
+ })
+ mockReplayTrnOps.mockImplementation(async () => {
+ callOrder.push('trns')
+ return 0
+ })
+ mockReplaySettingsOps.mockImplementation(async () => {
+ callOrder.push('settings')
+ })
mockGetAllOfflineOps.mockResolvedValue([
makeOp(OfflineEntityType.Wallets, 'w1', 'create'),
diff --git a/app/components/stat/categories/collectAndGroup.test.ts b/app/components/stat/categories/collectAndGroup.test.ts
index b9f262fc..27a90444 100644
--- a/app/components/stat/categories/collectAndGroup.test.ts
+++ b/app/components/stat/categories/collectAndGroup.test.ts
@@ -61,7 +61,7 @@ describe('collectCategoriesByTrns', () => {
const result = collectCategoriesByTrns({
categoriesItems: categories,
trnsIds: ['t1'],
- trnsItems: { t1: {} },
+ trnsItems: { t1: {} as any },
})
expect(Object.keys(result)).toEqual([])
diff --git a/app/components/stat/useStatItem.test.ts b/app/components/stat/useStatItem.test.ts
index a23f2404..dd95ee3f 100644
--- a/app/components/stat/useStatItem.test.ts
+++ b/app/components/stat/useStatItem.test.ts
@@ -22,7 +22,7 @@ vi.mock('@vueuse/core', () => ({
// ---------------------------------------------------------------------------
// Mock stores / composables used inside useStatItem
// ---------------------------------------------------------------------------
-const getStoreTrnsIdsMock = vi.fn(({ trnsIds }: { trnsIds?: string[] }) => trnsIds ?? [])
+const getStoreTrnsIdsMock = vi.fn(({ trnsIds }: { sort?: boolean, trnsIds?: string[] }) => trnsIds ?? [])
vi.mock('~/components/trns/useTrnsStore', () => ({
useTrnsStore: () => ({
@@ -32,10 +32,13 @@ vi.mock('~/components/trns/useTrnsStore', () => ({
}))
const computeTotalMock = vi.fn((): TotalReturns => ({
+ adjustment: 0,
expense: 0,
+ expenseTransfers: 0,
income: 0,
+ incomeTransfers: 0,
sum: 0,
- transfer: 0,
+ sumTransfers: 0,
}))
vi.mock('~/components/amount/useAmount', () => ({
@@ -73,7 +76,7 @@ vi.mock('~/components/stat/intervals', async (importOriginal) => {
const actual = await importOriginal() as any
return {
...actual,
- bucketTrnsByIntervals: (...args: any[]) => bucketTrnsByIntervalsMock(...args),
+ bucketTrnsByIntervals: (...args: any[]) => (bucketTrnsByIntervalsMock as any)(...args),
}
})
@@ -170,7 +173,7 @@ function createStatItem(overrides?: {
describe('useStatItem', () => {
beforeEach(() => {
vi.clearAllMocks()
- getStoreTrnsIdsMock.mockImplementation(({ trnsIds }: { trnsIds?: string[] }) => trnsIds ?? [])
+ getStoreTrnsIdsMock.mockImplementation(({ trnsIds }: { sort?: boolean, trnsIds?: string[] }) => trnsIds ?? [])
})
// -------------------------------------------------------------------------
@@ -255,8 +258,8 @@ describe('useStatItem', () => {
]
bucketTrnsByIntervalsMock.mockReturnValue([{
- range: intervals[0],
- total: { expense: 0, income: 0, sum: 0, transfer: 0 },
+ range: intervals[0]!,
+ total: { adjustment: 0, expense: 0, expenseTransfers: 0, income: 0, incomeTransfers: 0, sum: 0, sumTransfers: 0 },
trnsIds: intervalTrnsIds,
}])
diff --git a/app/components/trnForm/useTrnsFormStore.test.ts b/app/components/trnForm/useTrnsFormStore.test.ts
index a78e05f1..35db0248 100644
--- a/app/components/trnForm/useTrnsFormStore.test.ts
+++ b/app/components/trnForm/useTrnsFormStore.test.ts
@@ -1,8 +1,10 @@
import { createPinia, setActivePinia } from 'pinia'
-import { describe, expect, it, vi, beforeEach } from 'vitest'
+import { beforeEach, describe, expect, it, vi } from 'vitest'
import { TrnType } from '~/components/trns/types'
+import { useTrnsFormStore } from './useTrnsFormStore'
+
// --- Mocks ---
vi.mock('~/components/categories/useCategoriesStore', () => ({
@@ -14,7 +16,7 @@ vi.mock('~/components/categories/useCategoriesStore', () => ({
vi.mock('~/components/wallets/useWalletsStore', () => ({
useWalletsStore: () => ({
- items: { w1: { name: 'Cash', currency: 'USD' }, w2: { name: 'Card', currency: 'USD' } },
+ items: { w1: { currency: 'USD', name: 'Cash' }, w2: { currency: 'USD', name: 'Card' } },
sortedIds: ['w1', 'w2'],
}),
}))
@@ -42,8 +44,6 @@ beforeEach(() => {
setActivePinia(createPinia())
})
-import { useTrnsFormStore } from './useTrnsFormStore'
-
describe('useTrnsFormStore', () => {
describe('initial state', () => {
it('has default values', () => {
diff --git a/app/components/user/useUserStore.test.ts b/app/components/user/useUserStore.test.ts
index 4d04f43b..62488ae4 100644
--- a/app/components/user/useUserStore.test.ts
+++ b/app/components/user/useUserStore.test.ts
@@ -37,6 +37,11 @@ vi.stubGlobal('useAuth', () => ({
useSession: useSessionMock,
}))
+const setTrnsMock = vi.fn()
+const setCategoriesMock = vi.fn()
+const setWalletsMock = vi.fn()
+const trnFormResetMock = vi.fn()
+
vi.mock('~/components/categories/useCategoriesStore', () => ({
useCategoriesStore: () => ({
setCategories: (...args: unknown[]) => setCategoriesMock(...args),
@@ -77,18 +82,13 @@ vi.mock('~/composables/useStoreSync', async (importOriginal) => {
}
})
-const setTrnsMock = vi.fn()
-const setCategoriesMock = vi.fn()
-const setWalletsMock = vi.fn()
-const trnFormResetMock = vi.fn()
-
const clearOfflineQueueMock = vi.fn(() => Promise.resolve())
const setOfflineQueueUserIdMock = vi.fn()
vi.mock('~/components/offline/helpers', () => ({
- clearOfflineQueue: (...args: unknown[]) => clearOfflineQueueMock(...args),
+ clearOfflineQueue: (...args: any[]) => (clearOfflineQueueMock as any)(...args),
getOfflineOpsByEntity: vi.fn(() => Promise.resolve([])),
pushOfflineOp: vi.fn(() => Promise.resolve()),
- setOfflineQueueUserId: (...args: unknown[]) => setOfflineQueueUserIdMock(...args),
+ setOfflineQueueUserId: (...args: any[]) => (setOfflineQueueUserIdMock as any)(...args),
}))
const useCookieMock = vi.fn(() => ({ value: null }))
@@ -296,11 +296,11 @@ describe('useUserStore', () => {
})
it('clears localforage', async () => {
- await localforage.setItem('testKey', 'testValue')
+ await localforage.setItem('finapp.wallets', 'testValue')
const store = useUserStore()
await store.signOut()
- const value = await localforage.getItem('testKey')
+ const value = await localforage.getItem('finapp.wallets')
expect(value).toBeNull()
})