Fix dashboard trend test expectations

This commit is contained in:
defiQUG
2026-04-16 13:48:23 -07:00
parent cc70283171
commit 6a27d2c0e8

View File

@@ -1,6 +1,6 @@
import { describe, expect, it, vi } from 'vitest'
import type { Block } from '@/services/api/blocks'
import type { ExplorerStats } from '@/services/api/stats'
import type { ExplorerStats, ExplorerTransactionTrendPoint } from '@/services/api/stats'
import { loadDashboardData } from './dashboard'
const sampleStats: ExplorerStats = {
@@ -23,6 +23,17 @@ const sampleBlocks: Block[] = [
},
]
const sampleTrend: ExplorerTransactionTrendPoint[] = [
{
date: '2026-04-03',
count: 11,
},
{
date: '2026-04-04',
count: 17,
},
]
describe('loadDashboardData', () => {
it('returns both stats and recent blocks when both loaders succeed', async () => {
const result = await loadDashboardData({
@@ -33,6 +44,7 @@ describe('loadDashboardData', () => {
expect(result).toEqual({
stats: sampleStats,
recentBlocks: sampleBlocks,
recentTransactionTrend: [],
})
})
@@ -50,6 +62,7 @@ describe('loadDashboardData', () => {
expect(result).toEqual({
stats: null,
recentBlocks: sampleBlocks,
recentTransactionTrend: [],
})
expect(onError).toHaveBeenCalledTimes(1)
expect(onError).toHaveBeenCalledWith('stats', expect.any(Error))
@@ -69,8 +82,44 @@ describe('loadDashboardData', () => {
expect(result).toEqual({
stats: sampleStats,
recentBlocks: [],
recentTransactionTrend: [],
})
expect(onError).toHaveBeenCalledTimes(1)
expect(onError).toHaveBeenCalledWith('blocks', expect.any(Error))
})
it('returns the recent transaction trend when the optional loader succeeds', async () => {
const result = await loadDashboardData({
loadStats: async () => sampleStats,
loadRecentBlocks: async () => sampleBlocks,
loadRecentTransactionTrend: async () => sampleTrend,
})
expect(result).toEqual({
stats: sampleStats,
recentBlocks: sampleBlocks,
recentTransactionTrend: sampleTrend,
})
})
it('falls back to an empty recent transaction trend when the optional loader fails', async () => {
const onError = vi.fn()
const result = await loadDashboardData({
loadStats: async () => sampleStats,
loadRecentBlocks: async () => sampleBlocks,
loadRecentTransactionTrend: async () => {
throw new Error('trend unavailable')
},
onError,
})
expect(result).toEqual({
stats: sampleStats,
recentBlocks: sampleBlocks,
recentTransactionTrend: [],
})
expect(onError).toHaveBeenCalledTimes(1)
expect(onError).toHaveBeenCalledWith('trend', expect.any(Error))
})
})