Skip to content

Commit 5d7321e

Browse files
committed
chore(settings): clarify variable names in telemetry tracking and add unit test for settings telemetry trigger
1 parent f2952e9 commit 5d7321e

File tree

2 files changed

+105
-5
lines changed

2 files changed

+105
-5
lines changed

src/platform/settings/settingStore.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,20 @@ export const useSettingStore = defineStore('setting', () => {
8080
const dialogStore = useDialogStore()
8181
if (dialogStore.isDialogOpen('global-settings')) {
8282
const telemetry = useTelemetry()
83-
const param = settingsById.value[key]
83+
const settingParameter = settingsById.value[key]
8484
const { category, subCategory } = getSettingInfo(
85-
param ??
85+
settingParameter ??
8686
({
8787
id: String(key)
8888
} as unknown as SettingParams)
8989
)
9090

9191
const inputType = (() => {
92-
const type = param?.type
93-
if (!type) return undefined
94-
return typeof type === 'function' ? 'custom' : String(type)
92+
const settingType = settingParameter?.type
93+
if (!settingType) return undefined
94+
return typeof settingType === 'function'
95+
? 'custom'
96+
: String(settingType)
9597
})()
9698

9799
telemetry?.trackSettingChanged({
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

Comments
 (0)