|
| 1 | +import { describe, it, expect, beforeEach, vi } from 'vitest' |
| 2 | +import { setActivePinia, createPinia } from 'pinia' |
| 3 | + |
| 4 | +const hoisted = vi.hoisted(() => ({ |
| 5 | + trackSettingChanged: vi.fn(), |
| 6 | + storeSetting: vi.fn().mockResolvedValue(undefined) |
| 7 | +})) |
| 8 | + |
| 9 | +let isSettingsDialogOpen = false |
| 10 | + |
| 11 | +vi.mock('@/platform/telemetry', () => { |
| 12 | + return { |
| 13 | + useTelemetry: () => ({ |
| 14 | + trackSettingChanged: hoisted.trackSettingChanged |
| 15 | + }) |
| 16 | + } |
| 17 | +}) |
| 18 | + |
| 19 | +vi.mock('@/stores/dialogStore', () => { |
| 20 | + return { |
| 21 | + useDialogStore: () => ({ |
| 22 | + isDialogOpen: (key: string) => |
| 23 | + isSettingsDialogOpen && key === 'global-settings' |
| 24 | + }) |
| 25 | + } |
| 26 | +}) |
| 27 | + |
| 28 | +vi.mock('@/scripts/api', () => { |
| 29 | + return { |
| 30 | + api: { |
| 31 | + storeSetting: hoisted.storeSetting, |
| 32 | + getSettings: vi.fn().mockResolvedValue({}) |
| 33 | + } |
| 34 | + } |
| 35 | +}) |
| 36 | + |
| 37 | +vi.mock('@/scripts/app', () => { |
| 38 | + return { |
| 39 | + app: { |
| 40 | + ui: { |
| 41 | + settings: { |
| 42 | + dispatchChange: vi.fn() |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | +}) |
| 48 | + |
| 49 | +import { useSettingStore } from '@/platform/settings/settingStore' |
| 50 | + |
| 51 | +describe('useSettingStore telemetry', () => { |
| 52 | + beforeEach(() => { |
| 53 | + setActivePinia(createPinia()) |
| 54 | + hoisted.trackSettingChanged.mockReset() |
| 55 | + hoisted.storeSetting.mockReset().mockResolvedValue(undefined) |
| 56 | + isSettingsDialogOpen = false |
| 57 | + }) |
| 58 | + |
| 59 | + it('tracks telemetry when settings dialog is open', async () => { |
| 60 | + isSettingsDialogOpen = true |
| 61 | + |
| 62 | + const store = useSettingStore() |
| 63 | + store.addSetting({ |
| 64 | + id: 'main.sub.setting.name', |
| 65 | + name: 'Test Setting', |
| 66 | + type: 'text', |
| 67 | + defaultValue: 'old' |
| 68 | + }) |
| 69 | + |
| 70 | + await store.set('main.sub.setting.name', 'new') |
| 71 | + |
| 72 | + expect(hoisted.trackSettingChanged).toHaveBeenCalledTimes(1) |
| 73 | + expect(hoisted.trackSettingChanged).toHaveBeenCalledWith({ |
| 74 | + setting_id: 'main.sub.setting.name', |
| 75 | + input_type: 'text', |
| 76 | + category: 'main', |
| 77 | + sub_category: 'sub', |
| 78 | + previous_value: 'old', |
| 79 | + new_value: 'new' |
| 80 | + }) |
| 81 | + }) |
| 82 | + |
| 83 | + it('does not track telemetry when settings dialog is closed', async () => { |
| 84 | + isSettingsDialogOpen = false |
| 85 | + |
| 86 | + const store = useSettingStore() |
| 87 | + store.addSetting({ |
| 88 | + id: 'single.setting', |
| 89 | + name: 'Another Setting', |
| 90 | + type: 'text', |
| 91 | + defaultValue: 'x' |
| 92 | + }) |
| 93 | + |
| 94 | + await store.set('single.setting', 'y') |
| 95 | + |
| 96 | + expect(hoisted.trackSettingChanged).not.toHaveBeenCalled() |
| 97 | + }) |
| 98 | +}) |
0 commit comments