diff --git a/continue.md b/continue.md index 3edc9e0ba..d94dc7ed8 100644 --- a/continue.md +++ b/continue.md @@ -36,9 +36,11 @@ Actualizacion 2026-05-15: - Correccion TimeField: el announcer se inicializa con `$effect` en vez de `onMount()` dentro de la clase, y `datetime` separa `areAllTimeSegmentsFilledDOM(...)` del helper de fecha. + - Cobertura nueva para DateRangeField: validación de rango y foco de label + via `ActiveDom`. - Validado por fase: - `npm run check` -> 0 errores, 0 warnings. - - `npm run test` -> 183 archivos, 2074 tests OK. + - `npm run test` -> 184 archivos, 2077 tests OK. - tests focales de Select/Popover/Toast/RangeCalendar -> 11 tests OK. - tests focales Dialog/Drawer/Popover/Select -> 10 tests OK. - `command-provider.svelte.test.ts` -> 2 tests OK. @@ -49,6 +51,7 @@ Actualizacion 2026-05-15: - `time-range-picker-provider.svelte.test.ts` -> 3 tests OK. - `time-range-field-provider.svelte.test.ts` -> 3 tests OK. - `time-field-provider.svelte.test.ts` -> 3 tests OK. + - `date-range-field-provider.svelte.test.ts` -> 3 tests OK. - guardia `rg "from './.*provider\\.svelte'" src/uix/soma/components -g 'exports.ts'` -> solo queda `select/components/select-provider.svelte`, que es wrapper publico, no runtime provider. diff --git a/src/uix/soma/SOMA_ARCHITECTURE.md b/src/uix/soma/SOMA_ARCHITECTURE.md index 70279dfb9..1058c16dc 100644 --- a/src/uix/soma/SOMA_ARCHITECTURE.md +++ b/src/uix/soma/SOMA_ARCHITECTURE.md @@ -476,11 +476,13 @@ Los tests por componente estan creciendo desde las piezas de mayor riesgo. A - `time-field/time-field-provider.svelte.test.ts` — validación, sincronización de segmentos 12h y commit solo cuando los segmentos renderizados estan completos. +- `date-range-field/date-range-field-provider.svelte.test.ts` — validación de + orden/min/max, validación custom y foco de label via `ActiveDom`. -Pendiente: ampliar cobertura a `DateRangeField`, el provider interno de -fecha/hora de mayor tamano que sigue sin test directo. `table-core`, -`form-core` y el scorer de Command ya no viven dentro de Soma: se consumen -desde `$libs/datagrid`, `$libs/forms` y `$libs/strings`. +Pendiente: seguir ampliando cobertura por familias de alto riesgo fuera del +bloque fecha/hora. `table-core`, `form-core` y el scorer de Command ya no viven +dentro de Soma: se consumen desde `$libs/datagrid`, `$libs/forms` y +`$libs/strings`. ### Convencion diff --git a/src/uix/soma/components/date-range-field/date-range-field-provider.svelte.test.ts b/src/uix/soma/components/date-range-field/date-range-field-provider.svelte.test.ts new file mode 100644 index 000000000..ecfbfd198 --- /dev/null +++ b/src/uix/soma/components/date-range-field/date-range-field-provider.svelte.test.ts @@ -0,0 +1,170 @@ +// @vitest-environment jsdom + +import { afterEach, describe, expect, it, vi } from 'vitest'; + +import { createActiveDom } from '$adom'; +import { CalendarDate } from '$libs/days'; +import type { + DateOnInvalid, + DateRange, + DateRangeValidator, + DateValue, + EditableSegmentPart, + Granularity, + HourCycle +} from '$libs/days'; +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 type { Direction } from '../../types'; +import { FieldProvider } from '../field/field-provider.svelte'; + +import { + DateRangeFieldLabelProvider, + DateRangeFieldProvider +} from './date-range-field-provider.svelte'; + +function withEffectRoot(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, + langs: { + getLocale: () => 'en-US', + ts: (key: string) => key + }, + runtime: (morfo: Morfo, sources: Omit) => + 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(DateRangeFieldProvider.ctx, 'set').mockImplementation((value) => value); + + return { dom }; +} + +function createDateRangeFieldOpts(root = document.createElement('div')) { + return { + id: state('date-range-field-root'), + ref: state(root), + value: state({ start: undefined, end: undefined }), + placeholder: state(new CalendarDate(2026, 5, 15)), + startValue: state(undefined), + endValue: state(undefined), + validate: state(undefined), + onInvalid: state(undefined), + minValue: state(undefined), + maxValue: state(undefined), + disabled: state(false), + readonly: state(false), + startReadonlySegments: state([]), + endReadonlySegments: state([]), + required: state(false), + granularity: state(undefined), + hideTimeZone: state(false), + hourCycle: state(undefined), + locale: state('en-US'), + dir: state('ltr'), + errorMessageId: state(undefined) + }; +} + +describe('DateRangeFieldProvider', () => { + afterEach(() => { + vi.restoreAllMocks(); + document.body.innerHTML = ''; + }); + + it('validates endpoint ordering and min/max bounds', () => { + const { dom } = installSomaHarness(); + const opts = createDateRangeFieldOpts(); + + const { result: provider, cleanup } = withEffectRoot(() => DateRangeFieldProvider.create(opts)); + + opts.value.current = { + start: new CalendarDate(2026, 5, 20), + end: new CalendarDate(2026, 5, 10) + }; + expect(provider.validationStatus).toEqual({ reason: 'invalid' }); + expect(provider.isInvalid).toBe(true); + + opts.value.current = { + start: new CalendarDate(2026, 4, 30), + end: new CalendarDate(2026, 5, 10) + }; + opts.minValue.current = new CalendarDate(2026, 5, 1); + expect(provider.validationStatus).toEqual({ reason: 'min' }); + + opts.minValue.current = undefined; + opts.maxValue.current = new CalendarDate(2026, 5, 30); + opts.value.current = { + start: new CalendarDate(2026, 5, 10), + end: new CalendarDate(2026, 6, 1) + }; + expect(provider.validationStatus).toEqual({ reason: 'max' }); + + cleanup(); + dom.dispose(); + }); + + it('prefers custom validation when both endpoints are present', () => { + const { dom } = installSomaHarness(); + const opts = createDateRangeFieldOpts(); + opts.value.current = { + start: new CalendarDate(2026, 5, 10), + end: new CalendarDate(2026, 5, 11) + }; + opts.validate.current = () => 'Blocked range'; + + const { result: provider, cleanup } = withEffectRoot(() => DateRangeFieldProvider.create(opts)); + + expect(provider.validationStatus).toEqual({ + reason: 'custom', + message: 'Blocked range' + }); + expect(provider.props['aria-invalid']).toBe(true); + + cleanup(); + dom.dispose(); + }); + + it('focuses the first start segment when the label is clicked', () => { + const { dom } = installSomaHarness(); + const root = document.createElement('div'); + root.innerHTML = '
'; + document.body.appendChild(root); + const opts = createDateRangeFieldOpts(root); + const focus = vi.spyOn(dom, 'focus'); + + const { result: provider, cleanup } = withEffectRoot(() => { + const p = DateRangeFieldProvider.create(opts); + vi.spyOn(DateRangeFieldProvider.ctx, 'get').mockReturnValue(p); + const label = DateRangeFieldLabelProvider.create({ + id: state('date-range-label'), + ref: state(document.createElement('label')) + }); + p.startInputId.current = 'start-input'; + label.onclick(); + return p; + }); + + expect(provider.startInputId.current).toBe('start-input'); + expect(focus).toHaveBeenCalledWith(root.querySelector('#start-month')); + + cleanup(); + dom.dispose(); + }); +});