generated from Seekra/repository-template
test(settings): add tests for useSettings composable
Added some tests for the getSetting function from the useSettings composable.
This commit is contained in:
@@ -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
|
||||||
|
})
|
||||||
|
);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user