Fix error when adding custom setting with no category (#2112)

Co-authored-by: huchenlei <huchenlei@proton.me>
This commit is contained in:
bymyself
2024-12-31 17:04:58 -07:00
committed by GitHub
parent 3189e310a8
commit f507142e6a
2 changed files with 71 additions and 4 deletions

View File

@@ -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'
}
}

View File

@@ -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
@@ -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'
})
})
})