Skip to content

Commit

Permalink
Fix error when adding custom setting with no category (#2112)
Browse files Browse the repository at this point in the history
Co-authored-by: huchenlei <[email protected]>
  • Loading branch information
christian-byrne and huchenlei authored Jan 1, 2025
1 parent 3189e31 commit f507142
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/stores/settingStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import { buildTree } from '@/utils/treeUtil'
export const getSettingInfo = (setting: SettingParams) => {
const parts = setting.category || setting.id.split('.')
return {
category: parts[0],
subCategory: parts[1],
name: parts.slice(2).join('.')
category: parts[0] ?? 'Other',
subCategory: parts[1] ?? 'Other'
}
}

Expand Down
70 changes: 69 additions & 1 deletion tests-ui/tests/store/settingStore.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createPinia, setActivePinia } from 'pinia'

import { api } from '@/scripts/api'
import { useSettingStore } from '@/stores/settingStore'
import { getSettingInfo, useSettingStore } from '@/stores/settingStore'
import type { SettingParams } from '@/types/settingTypes'

// Mock the api
Expand Down Expand Up @@ -140,3 +140,71 @@ describe('useSettingStore', () => {
})
})
})

describe('getSettingInfo', () => {
const baseSetting: SettingParams = {
id: 'test.setting',
name: 'test.setting',
type: 'text',
defaultValue: 'default'
}

it('should handle settings with explicit category array', () => {
const setting: SettingParams = {
...baseSetting,
id: 'test.setting',
category: ['Main', 'Sub', 'Detail']
}

const result = getSettingInfo(setting)

expect(result).toEqual({
category: 'Main',
subCategory: 'Sub'
})
})

it('should handle settings with id-based categorization', () => {
const setting: SettingParams = {
...baseSetting,
id: 'main.sub.setting.name'
}

const result = getSettingInfo(setting)

expect(result).toEqual({
category: 'main',
subCategory: 'sub'
})
})

it('should use "Other" as default subCategory when missing', () => {
const setting: SettingParams = {
...baseSetting,
id: 'single.setting',
category: ['single']
}

const result = getSettingInfo(setting)

expect(result).toEqual({
category: 'single',
subCategory: 'Other'
})
})

it('should use "Other" as default category when missing', () => {
const setting: SettingParams = {
...baseSetting,
id: 'single.setting',
category: []
}

const result = getSettingInfo(setting)

expect(result).toEqual({
category: 'Other',
subCategory: 'Other'
})
})
})

0 comments on commit f507142

Please sign in to comment.