From e71b917c8eebb33812d0138951ce8311da35a2cd Mon Sep 17 00:00:00 2001 From: Jakob Scheid Date: Wed, 5 Aug 2026 01:05:12 +0200 Subject: [PATCH] test(settings): add tests for useSettings composable Added some tests for the getSetting function from the useSettings composable. --- .../composables/__tests__/useSettings.test.js | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 src/features/settings/composables/__tests__/useSettings.test.js diff --git a/src/features/settings/composables/__tests__/useSettings.test.js b/src/features/settings/composables/__tests__/useSettings.test.js new file mode 100644 index 0000000..b6637fe --- /dev/null +++ b/src/features/settings/composables/__tests__/useSettings.test.js @@ -0,0 +1,85 @@ +/* +Copyright 2026 Seekra + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import { useSettingsStore } from '../../stores/settingsStore'; +import { useSettings } from '../useSettings'; +import { createTestingPinia } from '@pinia/testing'; +import { setActivePinia } from 'pinia'; +import { beforeEach, describe, expect, test, vi } from 'vitest'; + +const settingDefaultValue = vi.hoisted(() => 42); +const settingPath = vi.hoisted(() => ['test', 'number']); + +vi.mock('../../utils/settingsParser', () => ({ + loadSettingsConfig: vi.fn().mockResolvedValue({ + contents: [ + { + name: 'test', + i18n: '', + content: [ + { + type: 'number', + i18n: '', + name: 'number', + default: settingDefaultValue + } + ] + } + ] + }) +})); + +describe('useSettings', () => { + describe('getSetting', () => { + test('returns stored value', async () => { + const { getSetting } = useSettings(); + const settings = useSettingsStore(); + + const key = 'setting'; + const value = 42; + + settings.set(key, value); + + expect(await getSetting(key)).toBe(value); + }); + + test('returns default value', async () => { + const { getSetting } = useSettings(); + expect(await getSetting(settingPath)).toBe(settingDefaultValue); + }); + + test('returns stored value instead of default value', async () => { + const { getSetting } = useSettings(); + const settings = useSettingsStore(); + + const value = 43; + + expect(await getSetting(settingPath)).toBe(settingDefaultValue); + + settings.set(settingPath, value); + expect(await getSetting(settingPath)).toBe(value); + }); + }); +}); + +beforeEach(() => { + setActivePinia( + createTestingPinia({ + createSpy: vi.fn, + stubActions: false + }) + ); +});