parent
9d7c824652
commit
572779e905
@ -0,0 +1,228 @@
|
|||||||
|
// @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 {
|
||||||
|
ToolbarButtonProvider,
|
||||||
|
ToolbarGroupItemProvider,
|
||||||
|
ToolbarGroupProvider,
|
||||||
|
ToolbarLinkProvider,
|
||||||
|
ToolbarProvider,
|
||||||
|
ToolbarSeparatorProvider
|
||||||
|
} from './toolbar-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(ToolbarProvider.ctx, 'set').mockImplementation((value) => value);
|
||||||
|
vi.spyOn(ToolbarGroupProvider.ctx, 'set').mockImplementation((value) => value);
|
||||||
|
|
||||||
|
return { dom };
|
||||||
|
}
|
||||||
|
|
||||||
|
function toolbarOpts(root: HTMLElement) {
|
||||||
|
return {
|
||||||
|
id: state('toolbar-root'),
|
||||||
|
ref: state<HTMLElement | null>(root),
|
||||||
|
orientation: state<'horizontal' | 'vertical'>('horizontal'),
|
||||||
|
loop: state(true),
|
||||||
|
dir: state<'ltr' | 'rtl'>('ltr')
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function keyEvent(key: string) {
|
||||||
|
return {
|
||||||
|
key,
|
||||||
|
preventDefault: vi.fn()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('ToolbarProvider', () => {
|
||||||
|
afterEach(() => {
|
||||||
|
vi.restoreAllMocks();
|
||||||
|
document.body.innerHTML = '';
|
||||||
|
});
|
||||||
|
|
||||||
|
it('roves tabindex and focus across buttons and links', async () => {
|
||||||
|
const { dom } = installSomaHarness();
|
||||||
|
const focusSpy = vi.spyOn(dom, 'focus');
|
||||||
|
const root = document.createElement('div');
|
||||||
|
root.setAttribute('data-toolbar', '');
|
||||||
|
const firstEl = document.createElement('button');
|
||||||
|
firstEl.setAttribute('data-toolbar-button', '');
|
||||||
|
const disabledEl = document.createElement('button');
|
||||||
|
disabledEl.setAttribute('data-toolbar-button', '');
|
||||||
|
disabledEl.setAttribute('disabled', '');
|
||||||
|
const linkEl = document.createElement('a');
|
||||||
|
linkEl.setAttribute('data-toolbar-link', '');
|
||||||
|
root.append(firstEl, disabledEl, linkEl);
|
||||||
|
document.body.appendChild(root);
|
||||||
|
const opts = toolbarOpts(root);
|
||||||
|
|
||||||
|
const { result, cleanup } = withEffectRoot(() => {
|
||||||
|
const provider = ToolbarProvider.create(opts);
|
||||||
|
vi.spyOn(ToolbarProvider, 'require').mockReturnValue(provider);
|
||||||
|
const first = ToolbarButtonProvider.create({
|
||||||
|
id: state('toolbar-first'),
|
||||||
|
ref: state<HTMLElement | null>(firstEl),
|
||||||
|
disabled: state(false)
|
||||||
|
});
|
||||||
|
const disabled = ToolbarButtonProvider.create({
|
||||||
|
id: state('toolbar-disabled'),
|
||||||
|
ref: state<HTMLElement | null>(disabledEl),
|
||||||
|
disabled: state(true)
|
||||||
|
});
|
||||||
|
const link = ToolbarLinkProvider.create({
|
||||||
|
id: state('toolbar-link'),
|
||||||
|
ref: state<HTMLElement | null>(linkEl)
|
||||||
|
});
|
||||||
|
return { provider, first, disabled, link };
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.provider.props).toMatchObject({
|
||||||
|
id: 'toolbar-root',
|
||||||
|
'data-toolbar': '',
|
||||||
|
dir: 'ltr'
|
||||||
|
});
|
||||||
|
expect(result.first.props.tabindex).toBe(0);
|
||||||
|
expect(result.disabled.props.disabled).toBe(true);
|
||||||
|
expect(result.link.props.tabindex).toBe(-1);
|
||||||
|
|
||||||
|
firstEl.focus();
|
||||||
|
const next = keyEvent('ArrowRight');
|
||||||
|
result.provider.onkeydown(next as never);
|
||||||
|
expect(next.preventDefault).toHaveBeenCalledOnce();
|
||||||
|
expect(focusSpy).toHaveBeenCalledWith(linkEl);
|
||||||
|
expect(result.provider.lastFocusedElement).toBe(linkEl);
|
||||||
|
expect(result.first.props.tabindex).toBe(-1);
|
||||||
|
expect(result.link.props.tabindex).toBe(0);
|
||||||
|
|
||||||
|
const home = keyEvent('Home');
|
||||||
|
linkEl.focus();
|
||||||
|
result.provider.onkeydown(home as never);
|
||||||
|
expect(home.preventDefault).toHaveBeenCalledOnce();
|
||||||
|
expect(focusSpy).toHaveBeenLastCalledWith(firstEl);
|
||||||
|
|
||||||
|
await tick();
|
||||||
|
expect(root.getAttribute('role')).toBe('toolbar');
|
||||||
|
expect(root.getAttribute('data-orientation')).toBe('horizontal');
|
||||||
|
expect(disabledEl.getAttribute('data-disabled')).toBe('');
|
||||||
|
|
||||||
|
cleanup();
|
||||||
|
dom.dispose();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('updates last focused element from focusin', () => {
|
||||||
|
const { dom } = installSomaHarness();
|
||||||
|
const root = document.createElement('div');
|
||||||
|
const buttonEl = document.createElement('button');
|
||||||
|
document.body.append(root, buttonEl);
|
||||||
|
const opts = toolbarOpts(root);
|
||||||
|
|
||||||
|
const { result: provider, cleanup } = withEffectRoot(() => ToolbarProvider.create(opts));
|
||||||
|
|
||||||
|
provider.onfocusin({ target: buttonEl } as unknown as FocusEvent);
|
||||||
|
expect(provider.lastFocusedElement).toBe(buttonEl);
|
||||||
|
|
||||||
|
cleanup();
|
||||||
|
dom.dispose();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('toggles group items and projects separator orientation', async () => {
|
||||||
|
const { dom } = installSomaHarness();
|
||||||
|
const root = document.createElement('div');
|
||||||
|
root.setAttribute('data-toolbar', '');
|
||||||
|
const groupEl = document.createElement('div');
|
||||||
|
const boldEl = document.createElement('button');
|
||||||
|
boldEl.setAttribute('data-toolbar-group-item', '');
|
||||||
|
const italicEl = document.createElement('button');
|
||||||
|
italicEl.setAttribute('data-toolbar-group-item', '');
|
||||||
|
const separatorEl = document.createElement('div');
|
||||||
|
root.append(groupEl, boldEl, italicEl, separatorEl);
|
||||||
|
document.body.appendChild(root);
|
||||||
|
const opts = toolbarOpts(root);
|
||||||
|
const value = state<string[]>([]);
|
||||||
|
const groupType = state<'single' | 'multiple'>('single');
|
||||||
|
const groupDisabled = state(false);
|
||||||
|
|
||||||
|
const { result, cleanup } = withEffectRoot(() => {
|
||||||
|
const provider = ToolbarProvider.create(opts);
|
||||||
|
vi.spyOn(ToolbarProvider, 'require').mockReturnValue(provider);
|
||||||
|
const group = ToolbarGroupProvider.create({
|
||||||
|
id: state('toolbar-group'),
|
||||||
|
ref: state<HTMLElement | null>(groupEl),
|
||||||
|
value,
|
||||||
|
type: groupType,
|
||||||
|
disabled: groupDisabled
|
||||||
|
});
|
||||||
|
vi.spyOn(ToolbarGroupProvider, 'require').mockReturnValue(group);
|
||||||
|
const bold = ToolbarGroupItemProvider.create({
|
||||||
|
id: state('toolbar-bold'),
|
||||||
|
ref: state<HTMLElement | null>(boldEl),
|
||||||
|
value: state('bold'),
|
||||||
|
disabled: state(false)
|
||||||
|
});
|
||||||
|
const italic = ToolbarGroupItemProvider.create({
|
||||||
|
id: state('toolbar-italic'),
|
||||||
|
ref: state<HTMLElement | null>(italicEl),
|
||||||
|
value: state('italic'),
|
||||||
|
disabled: state(false)
|
||||||
|
});
|
||||||
|
const separator = ToolbarSeparatorProvider.create({
|
||||||
|
id: state('toolbar-separator'),
|
||||||
|
ref: state<HTMLElement | null>(separatorEl)
|
||||||
|
});
|
||||||
|
return { group, bold, italic, separator };
|
||||||
|
});
|
||||||
|
|
||||||
|
result.bold.onclick({ currentTarget: boldEl } as never);
|
||||||
|
expect(value.current).toEqual(['bold']);
|
||||||
|
result.italic.onclick({ currentTarget: italicEl } as never);
|
||||||
|
expect(value.current).toEqual(['italic']);
|
||||||
|
|
||||||
|
groupType.current = 'multiple';
|
||||||
|
result.bold.onclick({ currentTarget: boldEl } as never);
|
||||||
|
expect(value.current).toEqual(['italic', 'bold']);
|
||||||
|
result.italic.onclick({ currentTarget: italicEl } as never);
|
||||||
|
expect(value.current).toEqual(['bold']);
|
||||||
|
|
||||||
|
await tick();
|
||||||
|
expect(boldEl.getAttribute('data-state')).toBe('on');
|
||||||
|
expect(italicEl.getAttribute('data-state')).toBe('off');
|
||||||
|
expect(separatorEl.getAttribute('role')).toBe('separator');
|
||||||
|
expect(separatorEl.getAttribute('aria-orientation')).toBe('vertical');
|
||||||
|
|
||||||
|
groupDisabled.current = true;
|
||||||
|
result.bold.onclick({ currentTarget: boldEl } as never);
|
||||||
|
expect(value.current).toEqual(['bold']);
|
||||||
|
expect(result.bold.props.disabled).toBe(true);
|
||||||
|
|
||||||
|
cleanup();
|
||||||
|
dom.dispose();
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Reference in new issue