4 Commits
Author SHA1 Message Date
jakob.scheid 890404a63e test(settings): add test cases for empty setting value return value 2026-08-05 01:44:03 +02:00
jakob.scheid d2e6a335a3 feat(settings): make setting value function synchronous
Added a store to cache the settings configuration. The getSetting
function from useSettings is now synchronous and uses this cache.
The settings cache is set when loading the settings initially
(asnychronously).
2026-08-05 01:33:12 +02:00
jakob.scheid e71b917c8e test(settings): add tests for useSettings composable
Added some tests for the getSetting function from the useSettings
composable.
2026-08-05 01:05:12 +02:00
jakob.scheid de5bee2c4a feat(settings): add composable for settings values
Added the composable useSettings that provides the function getSetting.
This function returns the set value of the setting, or otherwise the
default value. If there is no default value, it returns undefined.
2026-08-05 00:32:53 +02:00
7 changed files with 16 additions and 82 deletions
+1 -2
View File
@@ -26,7 +26,7 @@ import { useRoute } from 'vue-router';
const route = useRoute(); const route = useRoute();
const { getColorScheme, updateColorScheme } = useColorScheme(); const { getColorScheme, updateColorScheme } = useColorScheme();
const colorScheme = ref(null); const colorScheme = ref(getColorScheme());
provide('colorScheme', colorScheme); provide('colorScheme', colorScheme);
watch(colorScheme, (newValue) => { watch(colorScheme, (newValue) => {
updateColorScheme(newValue); updateColorScheme(newValue);
@@ -46,7 +46,6 @@ watch(colorScheme, (newValue) => {
document.body.classList.remove('color-scheme-auto'); document.body.classList.remove('color-scheme-auto');
} }
}); });
colorScheme.value = getColorScheme();
watchEffect(() => updatePageTitle(route)); watchEffect(() => updatePageTitle(route));
</script> </script>
@@ -54,7 +54,6 @@ const copyrightPeriod =
.footer-segment { .footer-segment {
display: flex; display: flex;
justify-content: center; justify-content: center;
align-items: center;
gap: 32px; gap: 32px;
padding: var(--padding-y); padding: var(--padding-y);
background-color: var(--light-bg); background-color: var(--light-bg);
+3 -11
View File
@@ -16,7 +16,7 @@ limitations under the License.
<script setup> <script setup>
import { useSettingsStore } from '../stores/settingsStore'; import { useSettingsStore } from '../stores/settingsStore';
import { ref, useId, watch } from 'vue'; import { useId } from 'vue';
import { useI18n } from 'vue-i18n'; import { useI18n } from 'vue-i18n';
const { t } = useI18n(); const { t } = useI18n();
@@ -35,16 +35,8 @@ const switchId = useId();
const store = useSettingsStore(); const store = useSettingsStore();
const enabled = ref(null);
watch(store, (newStore) => {
enabled.value = newStore.get(props.path);
});
// set value after registering watcher to avoid immediate value change
enabled.value = store.get(props.path) ?? props.setting.default;
const toggle = function toggle () { const toggle = function toggle () {
enabled.value = !enabled.value; store.set(props.path, !store.get(props.path));
store.set(props.path, enabled.value);
}; };
</script> </script>
@@ -55,7 +47,7 @@ const toggle = function toggle () {
</label> </label>
<div <div
class="switch-wrapper" class="switch-wrapper"
:class="{ enabled }" :class="{ enabled: store.get(props.path) }"
:title="t('settings.switch.title')" :title="t('settings.switch.title')"
:aria-label="t('settings.switch.ariaLabel')" :aria-label="t('settings.switch.ariaLabel')"
:aria-labelledby="labelId" :aria-labelledby="labelId"
@@ -20,23 +20,17 @@ import { useSettingsStore } from '../../stores/settingsStore.js';
import { expect, describe, test } from 'vitest'; import { expect, describe, test } from 'vitest';
import { nextTick } from 'vue'; import { nextTick } from 'vue';
const getWrapper = function getWrapper ({ const getWrapper = function getWrapper ({ i18n = 'switch1', translations = {} } = {}) {
i18n = 'switch1',
defaultValue = undefined,
translations = {},
piniaOptions = {}
} = {}) {
return mountComponent(Switch, { return mountComponent(Switch, {
attrs: { attrs: {
setting: { setting: {
type: 'bool', type: 'bool',
name: 'switch', name: 'switch',
i18n, i18n
default: defaultValue
}, },
path: 'switch' path: 'switch'
} }
}, translations, [], piniaOptions); }, translations);
}; };
describe('Switch', () => { describe('Switch', () => {
@@ -59,42 +53,6 @@ describe('Switch', () => {
expect(switchElementClasses2).not.toContain('enabled'); expect(switchElementClasses2).not.toContain('enabled');
}); });
test('shows default value', () => {
const wrapper = getWrapper({
defaultValue: true
});
const switchElement = wrapper.find('.switch-wrapper');
expect(switchElement.classes('enabled')).toBeTruthy();
});
test('shows stored value instead of default value', () => {
const wrapper = getWrapper({
defaultValue: true,
piniaOptions: {
setupStores: () => {
const store = useSettingsStore();
store.set('switch', false);
}
}
});
const switchElement = wrapper.find('.switch-wrapper');
expect(switchElement.classes('enabled')).toBeFalsy();
});
test('reacts on store change', async () => {
const wrapper = getWrapper();
const store = useSettingsStore();
const switchElement = wrapper.find('.switch-wrapper');
expect(switchElement.classes('enabled')).toBeFalsy();
store.set('switch', true);
await nextTick();
expect(switchElement.classes('enabled')).toBeTruthy();
});
test('toggles value in store', async () => { test('toggles value in store', async () => {
const wrapper = getWrapper(); const wrapper = getWrapper();
@@ -26,7 +26,7 @@ const settingPath = ['test', 'number'];
describe('useSettings', () => { describe('useSettings', () => {
describe('getSetting', () => { describe('getSetting', () => {
test('returns stored value', () => { test('returns stored value', async () => {
const { getSetting } = useSettings(); const { getSetting } = useSettings();
const settings = useSettingsStore(); const settings = useSettingsStore();
@@ -35,24 +35,24 @@ describe('useSettings', () => {
settings.set(key, value); settings.set(key, value);
expect(getSetting(key)).toBe(value); expect(await getSetting(key)).toBe(value);
}); });
test('returns default value', () => { test('returns default value', async () => {
const { getSetting } = useSettings(); const { getSetting } = useSettings();
expect(getSetting(settingPath)).toBe(settingDefaultValue); expect(await getSetting(settingPath)).toBe(settingDefaultValue);
}); });
test('returns stored value instead of default value', () => { test('returns stored value instead of default value', async () => {
const { getSetting } = useSettings(); const { getSetting } = useSettings();
const settings = useSettingsStore(); const settings = useSettingsStore();
const value = 43; const value = 43;
expect(getSetting(settingPath)).toBe(settingDefaultValue); expect(await getSetting(settingPath)).toBe(settingDefaultValue);
settings.set(settingPath, value); settings.set(settingPath, value);
expect(getSetting(settingPath)).toBe(value); expect(await getSetting(settingPath)).toBe(value);
}); });
test('returns undefined if there is no value', () => { test('returns undefined if there is no value', () => {
@@ -40,15 +40,5 @@ export const useSettings = function useSettings () {
); );
}; };
/** return { getSetting };
* Sets the value of a specific setting.
* @param {string[]} key - The setting key.
* @param value - The new value for the setting.
*/
const setSetting = function setSetting (key, value) {
const settingsStore = useSettingsStore();
settingsStore.set(key.join('.'), value);
};
return { getSetting, setSetting };
}; };
+1 -5
View File
@@ -36,7 +36,7 @@ const toggleSidebar = function toggleSidebar () {
<div class="sidebar-controls"> <div class="sidebar-controls">
<SidebarExpandButton @click="toggleSidebar" class="sidebar-expand-button" /> <SidebarExpandButton @click="toggleSidebar" class="sidebar-expand-button" />
</div> </div>
<div class="sidebar-content" v-show="props.expanded"> <div class="sidebar-content" v-if="props.expanded">
<slot /> <slot />
</div> </div>
</nav> </nav>
@@ -61,10 +61,6 @@ const toggleSidebar = function toggleSidebar () {
} }
@media (max-width: 48rem) { @media (max-width: 48rem) {
.sidebar {
border: none;
}
.sidebar:not(.expanded) .sidebar-expand-button { .sidebar:not(.expanded) .sidebar-expand-button {
display: none; display: none;
} }