90 lines
2.2 KiB
TypeScript
90 lines
2.2 KiB
TypeScript
import { get } from 'svelte/store';
|
|
import {
|
|
beforeEach,
|
|
describe,
|
|
expect,
|
|
it,
|
|
} from 'vitest';
|
|
import {
|
|
type ControlModel,
|
|
createControlStore,
|
|
} from './createControlStore';
|
|
|
|
describe('createControlStore', () => {
|
|
let store: ReturnType<typeof createControlStore<number>>;
|
|
|
|
beforeEach(() => {
|
|
const initialState: ControlModel<number> = {
|
|
value: 10,
|
|
min: 0,
|
|
max: 100,
|
|
step: 5,
|
|
};
|
|
store = createControlStore(initialState);
|
|
});
|
|
|
|
it('initializes with correct state', () => {
|
|
expect(get(store)).toEqual({
|
|
value: 10,
|
|
min: 0,
|
|
max: 100,
|
|
step: 5,
|
|
});
|
|
});
|
|
|
|
it('increases value by step', () => {
|
|
store.increase();
|
|
expect(get(store).value).toBe(15);
|
|
});
|
|
|
|
it('decreases value by step', () => {
|
|
store.decrease();
|
|
expect(get(store).value).toBe(5);
|
|
});
|
|
|
|
it('clamps value at maximum', () => {
|
|
store.setValue(200);
|
|
expect(get(store).value).toBe(100);
|
|
});
|
|
|
|
it('clamps value at minimum', () => {
|
|
store.setValue(-10);
|
|
expect(get(store).value).toBe(0);
|
|
});
|
|
|
|
it('rounds to step precision', () => {
|
|
store.setValue(12.34);
|
|
// With step=5, 12.34 is clamped and rounded to nearest integer (0 decimal places)
|
|
expect(get(store).value).toBe(12);
|
|
});
|
|
|
|
it('handles decimal steps correctly', () => {
|
|
const decimalStore = createControlStore({
|
|
value: 1.0,
|
|
min: 0,
|
|
max: 2,
|
|
step: 0.05,
|
|
});
|
|
decimalStore.increase();
|
|
expect(get(decimalStore).value).toBe(1.05);
|
|
});
|
|
|
|
it('isAtMax returns true when at maximum', () => {
|
|
store.setValue(100);
|
|
expect(store.isAtMax()).toBe(true);
|
|
});
|
|
|
|
it('isAtMax returns false when not at maximum', () => {
|
|
expect(store.isAtMax()).toBe(false);
|
|
});
|
|
|
|
it('isAtMin returns true when at minimum', () => {
|
|
store.setValue(0);
|
|
expect(store.isAtMin()).toBe(true);
|
|
});
|
|
|
|
it('isAtMin returns false when not at minimum', () => {
|
|
expect(store.isAtMin()).toBe(false);
|
|
});
|
|
});
|