Skip to content

Commit

Permalink
fix(NcAppSettingsDialog): Fix some minor issues and only warn on dupl…
Browse files Browse the repository at this point in the history
…icated name instead of error

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Nov 10, 2023
1 parent e99e7ea commit 491acbc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/components/NcAppSettingsDialog/NcAppSettingsDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ import isMobile from '../../mixins/isMobile/index.js'
import { t } from '../../l10n.js'
import debounce from 'debounce'
import Vue from 'vue'
export default {
Expand Down Expand Up @@ -251,7 +252,7 @@ export default {
scroller: null,
/**
* Currently registered settings sections
* @type {{ id: string, name: string, icon?: VNode[] }}
* @type {{ id: string, name: string, icon?: import('vue').VNode[] }[]}
*/
sections: [],
}
Expand Down Expand Up @@ -292,7 +293,7 @@ export default {
mounted() {
// Select first settings section
this.selectedSection = this.$slots.default[0].componentOptions.propsData.id
this.selectedSection = this.$slots.default?.[0]?.componentOptions?.propsData?.id ?? ''
},
updated() {
Expand Down Expand Up @@ -322,13 +323,13 @@ export default {
throw new Error(`Duplicate section id found: ${id}. Settings navigation sections must have unique section ids.`)
}
if (this.sections.some(({ name: otherName }) => name === otherName)) {
throw new Error(`Duplicate section name found: ${name}. Settings navigation sections must have unique section names.`)
Vue.util.warn(`Duplicate section name found: ${name}. Settings navigation sections must have unique section names.`)
}
const newSections = [...this.sections, { id, name, icon }]
// Sort sections by order in slots
this.sections = newSections.sort(({ id: idA }, { id: idB }) => {
const indexOf = (id) => this.$slots.default.indexOf(vnode => vnode?.componentOptions?.propsData?.id === id)
const indexOf = (id) => this.$slots.default?.indexOf?.(vnode => vnode?.componentOptions?.propsData?.id === id) ?? -1
return indexOf(idA) - indexOf(idB)
})
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,44 @@ describe('NcAppSettingsDialog: Sections registration', () => {
expect(wrapper.vm.$data.sections[0]).toEqual({ id: 'test_id', name: 'test_name' })
})

it('warn on register a already registered section name', async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const wrapper = mount<Vue & { registerSection: any }>(NcAppSettingsDialog, {
propsData: {
open: true,
},
})

const spy = jest.spyOn(globalThis.console, 'error')
spy.mockImplementationOnce(() => {})

// First call should be OK
wrapper.vm.registerSection('test_id', 'test_name')
expect(wrapper.vm.$data.sections).toHaveLength(1)
expect(spy).not.toHaveBeenCalled()

// Second one should unregister first and replace with this one, but show an error
wrapper.vm.registerSection('test_id_2', 'test_name')
expect(wrapper.vm.$data.sections).toHaveLength(2)
expect(spy).toHaveBeenCalled()
})

it('error on register a already registered section ID', async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const wrapper = mount<Vue & { registerSection: any }>(NcAppSettingsDialog, {
propsData: {
open: true,
},
})

// First call should be OK
wrapper.vm.registerSection('test_id', 'test_name')
expect(wrapper.vm.$data.sections).toHaveLength(1)
// Second one should unregister first and replace with this one, but show an error
expect(() => wrapper.vm.registerSection('test_id', 'test_other_name')).toThrow()
expect(wrapper.vm.$data.sections).toHaveLength(1)
})

it('can unregister a section', async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const wrapper = mount<Vue & { registerSection: any, unregisterSection: any }>(NcAppSettingsDialog, {
Expand Down

0 comments on commit 491acbc

Please sign in to comment.