parent
0d338d47f2
commit
0dc6141849
@ -0,0 +1,213 @@
|
|||||||
|
// @vitest-environment jsdom
|
||||||
|
|
||||||
|
import { tick } from 'svelte';
|
||||||
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||||
|
|
||||||
|
import { createActiveDom } from '$adom';
|
||||||
|
import { state } from '$libs/reactive';
|
||||||
|
import type { Morfo } from '$uix/morfo';
|
||||||
|
import { Soma } from '$soma/core/soma.svelte';
|
||||||
|
import { createSomaRuntime, type SomaRuntimeSources } from '$soma/runtime.svelte';
|
||||||
|
import { FieldProvider } from '../field/field-provider.svelte';
|
||||||
|
|
||||||
|
import { SwitchProvider, SwitchThumbProvider } from './switch-provider.svelte';
|
||||||
|
|
||||||
|
function withEffectRoot<T>(fn: () => T): { result: T; cleanup: () => void } {
|
||||||
|
let result!: T;
|
||||||
|
const cleanup = $effect.root(() => {
|
||||||
|
result = fn();
|
||||||
|
});
|
||||||
|
return { result, cleanup };
|
||||||
|
}
|
||||||
|
|
||||||
|
async function flushRuntimeTrigger() {
|
||||||
|
await Promise.resolve();
|
||||||
|
await tick();
|
||||||
|
}
|
||||||
|
|
||||||
|
function installSomaHarness() {
|
||||||
|
const dom = createActiveDom();
|
||||||
|
const soma = {
|
||||||
|
dom,
|
||||||
|
runtime: (morfo: Morfo, sources: Omit<SomaRuntimeSources, 'dom' | 'eventEngine'>) =>
|
||||||
|
createSomaRuntime(morfo, {
|
||||||
|
dom,
|
||||||
|
translate: (key) => key,
|
||||||
|
...sources
|
||||||
|
})
|
||||||
|
} as unknown as Soma;
|
||||||
|
|
||||||
|
vi.spyOn(Soma, 'require').mockReturnValue(soma);
|
||||||
|
vi.spyOn(FieldProvider, 'get').mockReturnValue(undefined);
|
||||||
|
vi.spyOn(SwitchProvider.ctx, 'set').mockImplementation((value) => value);
|
||||||
|
|
||||||
|
return { dom };
|
||||||
|
}
|
||||||
|
|
||||||
|
function switchOpts(id = 'switch-root') {
|
||||||
|
return {
|
||||||
|
id: state(id),
|
||||||
|
ref: state<HTMLElement | null>(document.createElement('button')),
|
||||||
|
checked: state(false),
|
||||||
|
disabled: state(false),
|
||||||
|
readonly: state(false),
|
||||||
|
required: state(false),
|
||||||
|
invalid: state(false),
|
||||||
|
intent: state<'neutral' | 'affirm' | 'risk' | 'threat'>('neutral'),
|
||||||
|
color: state<'primary' | 'secondary'>('primary'),
|
||||||
|
name: state(''),
|
||||||
|
value: state('on'),
|
||||||
|
'aria-label': state(''),
|
||||||
|
'aria-labelledby': state(''),
|
||||||
|
onCheckedChange: state<(checked: boolean) => void>(() => undefined)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function keyEvent(key: string) {
|
||||||
|
return {
|
||||||
|
key,
|
||||||
|
shiftKey: false,
|
||||||
|
ctrlKey: false,
|
||||||
|
altKey: false,
|
||||||
|
metaKey: false,
|
||||||
|
preventDefault: vi.fn()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('SwitchProvider', () => {
|
||||||
|
afterEach(() => {
|
||||||
|
vi.restoreAllMocks();
|
||||||
|
document.body.innerHTML = '';
|
||||||
|
});
|
||||||
|
|
||||||
|
it('toggles checked state from click and keyboard through the runtime event', async () => {
|
||||||
|
const { dom } = installSomaHarness();
|
||||||
|
const opts = switchOpts();
|
||||||
|
const onCheckedChange = vi.fn();
|
||||||
|
opts.onCheckedChange.current = onCheckedChange;
|
||||||
|
|
||||||
|
const { result: provider, cleanup } = withEffectRoot(() => SwitchProvider.create(opts));
|
||||||
|
|
||||||
|
expect(provider.props).toMatchObject({
|
||||||
|
id: 'switch-root',
|
||||||
|
disabled: undefined
|
||||||
|
});
|
||||||
|
expect(provider.snippetProps).toEqual({ checked: false });
|
||||||
|
|
||||||
|
provider.onclick({} as never);
|
||||||
|
await flushRuntimeTrigger();
|
||||||
|
expect(opts.checked.current).toBe(true);
|
||||||
|
expect(onCheckedChange).toHaveBeenCalledWith(true);
|
||||||
|
expect(provider.snippetProps).toEqual({ checked: true });
|
||||||
|
|
||||||
|
const event = keyEvent(' ');
|
||||||
|
provider.onkeydown(event as never);
|
||||||
|
await flushRuntimeTrigger();
|
||||||
|
expect(event.preventDefault).toHaveBeenCalledOnce();
|
||||||
|
expect(opts.checked.current).toBe(false);
|
||||||
|
expect(onCheckedChange).toHaveBeenLastCalledWith(false);
|
||||||
|
|
||||||
|
cleanup();
|
||||||
|
dom.dispose();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('exposes hidden input props for native form participation', async () => {
|
||||||
|
const { dom } = installSomaHarness();
|
||||||
|
const opts = switchOpts();
|
||||||
|
opts.name.current = 'notifications';
|
||||||
|
opts.value.current = 'enabled';
|
||||||
|
opts.required.current = true;
|
||||||
|
opts.checked.current = true;
|
||||||
|
|
||||||
|
const { result: provider, cleanup } = withEffectRoot(() => SwitchProvider.create(opts));
|
||||||
|
|
||||||
|
expect(provider.inputProps).toMatchObject({
|
||||||
|
type: 'checkbox',
|
||||||
|
'aria-hidden': true,
|
||||||
|
tabindex: -1,
|
||||||
|
name: 'notifications',
|
||||||
|
value: 'enabled',
|
||||||
|
checked: true,
|
||||||
|
required: true,
|
||||||
|
disabled: undefined
|
||||||
|
});
|
||||||
|
|
||||||
|
opts.checked.current = false;
|
||||||
|
await tick();
|
||||||
|
expect(provider.inputProps.checked).toBe(false);
|
||||||
|
|
||||||
|
cleanup();
|
||||||
|
dom.dispose();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('merges FieldProvider flags and registers itself as field input', async () => {
|
||||||
|
const { dom } = installSomaHarness();
|
||||||
|
const field = {
|
||||||
|
inputId: state(''),
|
||||||
|
isDisabled: true,
|
||||||
|
isReadonly: false,
|
||||||
|
isRequired: true,
|
||||||
|
isInvalid: true
|
||||||
|
};
|
||||||
|
vi.spyOn(FieldProvider, 'get').mockReturnValue(field as FieldProvider);
|
||||||
|
const opts = switchOpts('field-switch');
|
||||||
|
|
||||||
|
const { result: provider, cleanup } = withEffectRoot(() => SwitchProvider.create(opts));
|
||||||
|
|
||||||
|
expect(field.inputId.current).toBe('field-switch');
|
||||||
|
expect(provider.isDisabled).toBe(true);
|
||||||
|
expect(provider.isRequired).toBe(true);
|
||||||
|
expect(provider.isInvalid).toBe(true);
|
||||||
|
expect(provider.props.disabled).toBe(true);
|
||||||
|
expect(provider.inputProps).toMatchObject({
|
||||||
|
required: true,
|
||||||
|
disabled: true
|
||||||
|
});
|
||||||
|
|
||||||
|
provider.toggle();
|
||||||
|
await flushRuntimeTrigger();
|
||||||
|
expect(opts.checked.current).toBe(false);
|
||||||
|
|
||||||
|
cleanup();
|
||||||
|
dom.dispose();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps readonly switches focusable but blocks state changes', async () => {
|
||||||
|
const { dom } = installSomaHarness();
|
||||||
|
const opts = switchOpts();
|
||||||
|
opts.readonly.current = true;
|
||||||
|
|
||||||
|
const { result: provider, cleanup } = withEffectRoot(() => SwitchProvider.create(opts));
|
||||||
|
|
||||||
|
provider.toggle();
|
||||||
|
await flushRuntimeTrigger();
|
||||||
|
expect(opts.checked.current).toBe(false);
|
||||||
|
expect(provider.props.disabled).toBeUndefined();
|
||||||
|
|
||||||
|
cleanup();
|
||||||
|
dom.dispose();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('wires thumb props to the parent runtime', () => {
|
||||||
|
const { dom } = installSomaHarness();
|
||||||
|
const opts = switchOpts();
|
||||||
|
|
||||||
|
const { result, cleanup } = withEffectRoot(() => {
|
||||||
|
const provider = SwitchProvider.create(opts);
|
||||||
|
vi.spyOn(SwitchProvider, 'require').mockReturnValue(provider);
|
||||||
|
const thumb = SwitchThumbProvider.create({
|
||||||
|
id: state('switch-thumb'),
|
||||||
|
ref: state<HTMLElement | null>(document.createElement('span'))
|
||||||
|
});
|
||||||
|
return { thumb };
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.thumb.props).toMatchObject({
|
||||||
|
id: 'switch-thumb',
|
||||||
|
'data-switch-thumb': ''
|
||||||
|
});
|
||||||
|
|
||||||
|
cleanup();
|
||||||
|
dom.dispose();
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Reference in new issue