Fix profile store mocks in usage tests

This commit is contained in:
ekko
2026-05-24 10:07:53 +08:00
committed by ekko
parent f4c70bd849
commit dca8fc6d8a
2 changed files with 28 additions and 2 deletions
+12
View File
@@ -3,11 +3,20 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'
import { mount, flushPromises } from '@vue/test-utils' import { mount, flushPromises } from '@vue/test-utils'
const fetchSkillUsageStatsMock = vi.hoisted(() => vi.fn()) const fetchSkillUsageStatsMock = vi.hoisted(() => vi.fn())
const mockProfilesStore = vi.hoisted(() => ({
activeProfileName: 'default',
profiles: [{ name: 'default' }],
fetchProfiles: vi.fn(),
}))
vi.mock('@/api/hermes/skills', () => ({ vi.mock('@/api/hermes/skills', () => ({
fetchSkillUsageStats: fetchSkillUsageStatsMock, fetchSkillUsageStats: fetchSkillUsageStatsMock,
})) }))
vi.mock('@/stores/hermes/profiles', () => ({
useProfilesStore: () => mockProfilesStore,
}))
vi.mock('vue-i18n', () => ({ vi.mock('vue-i18n', () => ({
useI18n: () => ({ useI18n: () => ({
t: (key: string, params?: Record<string, unknown>) => { t: (key: string, params?: Record<string, unknown>) => {
@@ -70,6 +79,9 @@ describe('SkillsUsageView', () => {
beforeEach(() => { beforeEach(() => {
fetchSkillUsageStatsMock.mockReset() fetchSkillUsageStatsMock.mockReset()
fetchSkillUsageStatsMock.mockResolvedValue(sevenDayStats) fetchSkillUsageStatsMock.mockResolvedValue(sevenDayStats)
mockProfilesStore.activeProfileName = 'default'
mockProfilesStore.profiles = [{ name: 'default' }]
mockProfilesStore.fetchProfiles.mockReset()
}) })
it('loads rolling 7 day skill usage and renders statistics beside a skill-colored visual trend', async () => { it('loads rolling 7 day skill usage and renders statistics beside a skill-colored visual trend', async () => {
+16 -2
View File
@@ -1,7 +1,7 @@
// @vitest-environment jsdom // @vitest-environment jsdom
import { beforeEach, describe, expect, it, vi } from 'vitest' import { beforeEach, describe, expect, it, vi } from 'vitest'
import { defineComponent } from 'vue' import { defineComponent } from 'vue'
import { mount } from '@vue/test-utils' import { flushPromises, mount } from '@vue/test-utils'
const mockUsageStore = vi.hoisted(() => ({ const mockUsageStore = vi.hoisted(() => ({
isLoading: false, isLoading: false,
@@ -9,10 +9,20 @@ const mockUsageStore = vi.hoisted(() => ({
loadSessions: vi.fn(), loadSessions: vi.fn(),
})) }))
const mockProfilesStore = vi.hoisted(() => ({
activeProfileName: 'default',
profiles: [{ name: 'default' }],
fetchProfiles: vi.fn(),
}))
vi.mock('@/stores/hermes/usage', () => ({ vi.mock('@/stores/hermes/usage', () => ({
useUsageStore: () => mockUsageStore, useUsageStore: () => mockUsageStore,
})) }))
vi.mock('@/stores/hermes/profiles', () => ({
useProfilesStore: () => mockProfilesStore,
}))
vi.mock('vue-i18n', () => ({ vi.mock('vue-i18n', () => ({
useI18n: () => ({ useI18n: () => ({
t: (key: string) => key, t: (key: string) => key,
@@ -54,10 +64,14 @@ describe('UsageView period selector', () => {
mockUsageStore.isLoading = false mockUsageStore.isLoading = false
mockUsageStore.hasData = true mockUsageStore.hasData = true
mockUsageStore.loadSessions.mockReset() mockUsageStore.loadSessions.mockReset()
mockProfilesStore.activeProfileName = 'default'
mockProfilesStore.profiles = [{ name: 'default' }]
mockProfilesStore.fetchProfiles.mockReset()
}) })
it('loads the default 30-day period on mount', () => { it('loads the default 30-day period on mount', async () => {
mount(UsageView) mount(UsageView)
await flushPromises()
expect(mockUsageStore.loadSessions).toHaveBeenCalledWith(30) expect(mockUsageStore.loadSessions).toHaveBeenCalledWith(30)
}) })