parent
e3fd5b6a70
commit
15dc542117
@ -0,0 +1,222 @@
|
||||
// @vitest-environment jsdom
|
||||
|
||||
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 { ToggleGroupItemProvider, ToggleGroupProvider } from './toggle-group-provider.svelte';
|
||||
|
||||
function withEffectRoot<T>(fn: () => T): { result: T; cleanup: () => void } {
|
||||
let result!: T;
|
||||
const cleanup = $effect.root(() => {
|
||||
result = fn();
|
||||
});
|
||||
return { result, cleanup };
|
||||
}
|
||||
|
||||
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(ToggleGroupProvider.ctx, 'set').mockImplementation((value) => value);
|
||||
|
||||
return { dom };
|
||||
}
|
||||
|
||||
function createRoot() {
|
||||
const root = document.createElement('div');
|
||||
root.setAttribute('data-toggle-group', '');
|
||||
document.body.appendChild(root);
|
||||
return root;
|
||||
}
|
||||
|
||||
function createItemEl(value: string, disabled = false) {
|
||||
const item = document.createElement('button');
|
||||
item.setAttribute('data-toggle-group-item', '');
|
||||
item.setAttribute('data-value', value);
|
||||
if (disabled) item.setAttribute('data-disabled', '');
|
||||
return item;
|
||||
}
|
||||
|
||||
function groupOpts(root = createRoot()) {
|
||||
return {
|
||||
id: state('toggle-group-root'),
|
||||
ref: state<HTMLElement | null>(root),
|
||||
value: state<string[]>([]),
|
||||
type: state<'single' | 'multiple'>('single'),
|
||||
disabled: state(false),
|
||||
orientation: state<'horizontal' | 'vertical'>('horizontal'),
|
||||
loop: state(true),
|
||||
dir: state<'ltr' | 'rtl'>('ltr')
|
||||
};
|
||||
}
|
||||
|
||||
function itemOpts(id: string, value: string, el: HTMLElement, disabled = false) {
|
||||
return {
|
||||
id: state(id),
|
||||
ref: state<HTMLElement | null>(el),
|
||||
value: state(value),
|
||||
disabled: state(disabled)
|
||||
};
|
||||
}
|
||||
|
||||
function applyAttrs(el: HTMLElement, props: Record<string, unknown>) {
|
||||
for (const [key, value] of Object.entries(props)) {
|
||||
if (
|
||||
!key.startsWith('data-') &&
|
||||
!key.startsWith('aria-') &&
|
||||
key !== 'role' &&
|
||||
key !== 'id' &&
|
||||
key !== 'disabled' &&
|
||||
key !== 'type'
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
if (value === undefined || value === false || typeof value === 'function') continue;
|
||||
el.setAttribute(key, value === true ? '' : String(value));
|
||||
}
|
||||
}
|
||||
|
||||
function keyEvent(key: string, currentTarget: HTMLElement) {
|
||||
return {
|
||||
key,
|
||||
currentTarget,
|
||||
preventDefault: vi.fn()
|
||||
};
|
||||
}
|
||||
|
||||
describe('ToggleGroupProvider', () => {
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
document.body.innerHTML = '';
|
||||
});
|
||||
|
||||
it('exposes root props and toggles single selection', () => {
|
||||
const { dom } = installSomaHarness();
|
||||
const opts = groupOpts();
|
||||
|
||||
const { result: provider, cleanup } = withEffectRoot(() => ToggleGroupProvider.create(opts));
|
||||
|
||||
expect(provider.props).toMatchObject({
|
||||
id: 'toggle-group-root',
|
||||
'data-toggle-group': '',
|
||||
dir: 'ltr'
|
||||
});
|
||||
|
||||
provider.toggleItem('bold');
|
||||
expect(opts.value.current).toEqual(['bold']);
|
||||
provider.toggleItem('italic');
|
||||
expect(opts.value.current).toEqual(['italic']);
|
||||
provider.toggleItem('italic');
|
||||
expect(opts.value.current).toEqual([]);
|
||||
|
||||
cleanup();
|
||||
dom.dispose();
|
||||
});
|
||||
|
||||
it('supports multiple selection mode', () => {
|
||||
const { dom } = installSomaHarness();
|
||||
const opts = groupOpts();
|
||||
opts.type.current = 'multiple';
|
||||
|
||||
const { result: provider, cleanup } = withEffectRoot(() => ToggleGroupProvider.create(opts));
|
||||
|
||||
provider.toggleItem('bold');
|
||||
provider.toggleItem('italic');
|
||||
expect(opts.value.current).toEqual(['bold', 'italic']);
|
||||
|
||||
provider.toggleItem('bold');
|
||||
expect(opts.value.current).toEqual(['italic']);
|
||||
|
||||
cleanup();
|
||||
dom.dispose();
|
||||
});
|
||||
|
||||
it('derives roving target from pressed item or first enabled item', () => {
|
||||
const { dom } = installSomaHarness();
|
||||
const root = createRoot();
|
||||
const boldEl = createItemEl('bold');
|
||||
const italicEl = createItemEl('italic');
|
||||
root.append(boldEl, italicEl);
|
||||
const opts = groupOpts(root);
|
||||
|
||||
const { result, cleanup } = withEffectRoot(() => {
|
||||
const provider = ToggleGroupProvider.create(opts);
|
||||
vi.spyOn(ToggleGroupProvider, 'require').mockReturnValue(provider);
|
||||
const bold = ToggleGroupItemProvider.create(itemOpts('toggle-bold', 'bold', boldEl));
|
||||
const italic = ToggleGroupItemProvider.create(itemOpts('toggle-italic', 'italic', italicEl));
|
||||
return { bold, italic };
|
||||
});
|
||||
|
||||
expect(result.bold.props).toMatchObject({
|
||||
tabindex: 0,
|
||||
'data-value': 'bold',
|
||||
disabled: undefined
|
||||
});
|
||||
expect(result.italic.props.tabindex).toBe(-1);
|
||||
|
||||
opts.value.current = ['italic'];
|
||||
expect(result.bold.props.tabindex).toBe(-1);
|
||||
expect(result.italic.props.tabindex).toBe(0);
|
||||
|
||||
result.italic.onclick({} as never);
|
||||
expect(opts.value.current).toEqual([]);
|
||||
|
||||
cleanup();
|
||||
dom.dispose();
|
||||
});
|
||||
|
||||
it('navigates enabled items by keyboard and skips disabled ones', () => {
|
||||
const { dom } = installSomaHarness();
|
||||
const focusSpy = vi.spyOn(dom, 'focus');
|
||||
const root = createRoot();
|
||||
const boldEl = createItemEl('bold');
|
||||
const disabledEl = createItemEl('disabled', true);
|
||||
const italicEl = createItemEl('italic');
|
||||
root.append(boldEl, disabledEl, italicEl);
|
||||
const opts = groupOpts(root);
|
||||
|
||||
const { result, cleanup } = withEffectRoot(() => {
|
||||
const provider = ToggleGroupProvider.create(opts);
|
||||
vi.spyOn(ToggleGroupProvider, 'require').mockReturnValue(provider);
|
||||
const bold = ToggleGroupItemProvider.create(itemOpts('toggle-bold', 'bold', boldEl));
|
||||
const disabled = ToggleGroupItemProvider.create(
|
||||
itemOpts('toggle-disabled', 'disabled', disabledEl, true)
|
||||
);
|
||||
applyAttrs(disabledEl, disabled.props);
|
||||
const italic = ToggleGroupItemProvider.create(itemOpts('toggle-italic', 'italic', italicEl));
|
||||
return { bold, italic };
|
||||
});
|
||||
|
||||
const next = keyEvent('ArrowRight', boldEl);
|
||||
result.bold.onkeydown(next as never);
|
||||
expect(next.preventDefault).toHaveBeenCalledOnce();
|
||||
expect(focusSpy).toHaveBeenCalledWith(italicEl);
|
||||
|
||||
const home = keyEvent('Home', italicEl);
|
||||
result.italic.onkeydown(home as never);
|
||||
expect(home.preventDefault).toHaveBeenCalledOnce();
|
||||
expect(focusSpy).toHaveBeenLastCalledWith(boldEl);
|
||||
|
||||
const end = keyEvent('End', boldEl);
|
||||
result.bold.onkeydown(end as never);
|
||||
expect(end.preventDefault).toHaveBeenCalledOnce();
|
||||
expect(focusSpy).toHaveBeenLastCalledWith(italicEl);
|
||||
|
||||
cleanup();
|
||||
dom.dispose();
|
||||
});
|
||||
});
|
||||
Loading…
Reference in new issue