parent
9bbfd0f3c4
commit
a44338ff2c
@ -0,0 +1,135 @@
|
|||||||
|
// @vitest-environment jsdom
|
||||||
|
|
||||||
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||||
|
|
||||||
|
import { createActiveDom } from '$adom';
|
||||||
|
import { CalendarDate } from '$libs/days';
|
||||||
|
import type {
|
||||||
|
DateMatcher,
|
||||||
|
DateOnInvalid,
|
||||||
|
DateValidator,
|
||||||
|
DateValue,
|
||||||
|
EditableSegmentPart,
|
||||||
|
Granularity,
|
||||||
|
HourCycle,
|
||||||
|
WeekStartsOn
|
||||||
|
} 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, OnChangeFn } from '../../types';
|
||||||
|
|
||||||
|
import { DatePickerProvider } from './date-picker-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,
|
||||||
|
langs: {
|
||||||
|
getLocale: () => 'en-US',
|
||||||
|
ts: (key: string) => key
|
||||||
|
},
|
||||||
|
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(DatePickerProvider.ctx, 'set').mockImplementation((value) => value);
|
||||||
|
|
||||||
|
return { dom };
|
||||||
|
}
|
||||||
|
|
||||||
|
function createDatePickerOpts() {
|
||||||
|
const placeholder = new CalendarDate(2026, 5, 15);
|
||||||
|
|
||||||
|
return {
|
||||||
|
id: state('date-picker-root'),
|
||||||
|
ref: state<HTMLElement | null>(null),
|
||||||
|
value: state<DateValue | undefined>(undefined),
|
||||||
|
placeholder: state<DateValue>(placeholder),
|
||||||
|
open: state(true),
|
||||||
|
validate: state<DateValidator | undefined>(undefined),
|
||||||
|
onInvalid: state<DateOnInvalid | undefined>(undefined),
|
||||||
|
minValue: state<DateValue | undefined>(undefined),
|
||||||
|
maxValue: state<DateValue | undefined>(undefined),
|
||||||
|
isDateDisabled: state<DateMatcher | undefined>(undefined),
|
||||||
|
isDateUnavailable: state<DateMatcher | undefined>(undefined),
|
||||||
|
disabled: state(false),
|
||||||
|
readonly: state(false),
|
||||||
|
readonlySegments: state<EditableSegmentPart[]>([]),
|
||||||
|
required: state(false),
|
||||||
|
closeOnDateSelect: state(true),
|
||||||
|
granularity: state<Granularity | undefined>(undefined),
|
||||||
|
hideTimeZone: state(false),
|
||||||
|
hourCycle: state<HourCycle | undefined>(undefined),
|
||||||
|
locale: state<string | undefined>('en-US'),
|
||||||
|
dir: state<Direction>('ltr'),
|
||||||
|
pagedNavigation: state(false),
|
||||||
|
weekStartsOn: state<WeekStartsOn | undefined>(undefined),
|
||||||
|
weekdayFormat: state<Intl.DateTimeFormatOptions['weekday']>('narrow'),
|
||||||
|
fixedWeeks: state(false),
|
||||||
|
numberOfMonths: state(1),
|
||||||
|
calendarLabel: state<string | undefined>(undefined),
|
||||||
|
disableDaysOutsideMonth: state(false),
|
||||||
|
preventDeselect: state(false),
|
||||||
|
initialFocus: state(false),
|
||||||
|
monthFormat: state<Intl.DateTimeFormatOptions['month'] | ((month: number) => string)>('long'),
|
||||||
|
yearFormat: state<Intl.DateTimeFormatOptions['year'] | ((year: number) => string)>('numeric'),
|
||||||
|
showWeekNumbers: state(false),
|
||||||
|
weekNumberFormat: state<'iso' | 'simple'>('iso'),
|
||||||
|
isDateHoliday: state<DateMatcher | undefined>(undefined),
|
||||||
|
announceNavigation: state(true),
|
||||||
|
errorMessageId: state<string | undefined>(undefined),
|
||||||
|
onOpenChangeComplete: state<OnChangeFn<boolean>>(() => {})
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('DatePickerProvider', () => {
|
||||||
|
afterEach(() => {
|
||||||
|
vi.restoreAllMocks();
|
||||||
|
document.body.innerHTML = '';
|
||||||
|
});
|
||||||
|
|
||||||
|
it('registers the provider part through SomaRuntime', () => {
|
||||||
|
const { dom } = installSomaHarness();
|
||||||
|
const opts = createDatePickerOpts();
|
||||||
|
|
||||||
|
const { result: provider, cleanup } = withEffectRoot(() => DatePickerProvider.create(opts));
|
||||||
|
|
||||||
|
expect(provider.runtimePart.props.id).toBe('date-picker-root');
|
||||||
|
|
||||||
|
cleanup();
|
||||||
|
dom.dispose();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('closes the popover only when closeOnDateSelect is enabled', () => {
|
||||||
|
const { dom } = installSomaHarness();
|
||||||
|
const opts = createDatePickerOpts();
|
||||||
|
|
||||||
|
const { result: provider, cleanup } = withEffectRoot(() => DatePickerProvider.create(opts));
|
||||||
|
|
||||||
|
provider.handleDateSelect();
|
||||||
|
expect(opts.open.current).toBe(false);
|
||||||
|
|
||||||
|
opts.open.current = true;
|
||||||
|
opts.closeOnDateSelect.current = false;
|
||||||
|
provider.handleDateSelect();
|
||||||
|
expect(opts.open.current).toBe(true);
|
||||||
|
|
||||||
|
cleanup();
|
||||||
|
dom.dispose();
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -0,0 +1,146 @@
|
|||||||
|
// @vitest-environment jsdom
|
||||||
|
|
||||||
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||||
|
|
||||||
|
import { createActiveDom } from '$adom';
|
||||||
|
import { CalendarDate } from '$libs/days';
|
||||||
|
import type {
|
||||||
|
DateMatcher,
|
||||||
|
DateOnInvalid,
|
||||||
|
DateRange,
|
||||||
|
DateRangeValidator,
|
||||||
|
DateValue,
|
||||||
|
EditableSegmentPart,
|
||||||
|
Granularity,
|
||||||
|
HourCycle,
|
||||||
|
WeekStartsOn
|
||||||
|
} 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, OnChangeFn } from '../../types';
|
||||||
|
|
||||||
|
import { DateRangePickerProvider } from './date-range-picker-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,
|
||||||
|
langs: {
|
||||||
|
getLocale: () => 'en-US',
|
||||||
|
ts: (key: string) => key
|
||||||
|
},
|
||||||
|
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(DateRangePickerProvider.ctx, 'set').mockImplementation((value) => value);
|
||||||
|
|
||||||
|
return { dom };
|
||||||
|
}
|
||||||
|
|
||||||
|
function createDateRangePickerOpts() {
|
||||||
|
const placeholder = new CalendarDate(2026, 5, 15);
|
||||||
|
|
||||||
|
return {
|
||||||
|
id: state('date-range-picker-root'),
|
||||||
|
ref: state<HTMLElement | null>(null),
|
||||||
|
value: state<DateRange>({ start: undefined, end: undefined }),
|
||||||
|
placeholder: state<DateValue>(placeholder),
|
||||||
|
startValue: state<DateValue | undefined>(undefined),
|
||||||
|
endValue: state<DateValue | undefined>(undefined),
|
||||||
|
open: state(true),
|
||||||
|
validate: state<DateRangeValidator | undefined>(undefined),
|
||||||
|
onInvalid: state<DateOnInvalid | undefined>(undefined),
|
||||||
|
minValue: state<DateValue | undefined>(undefined),
|
||||||
|
maxValue: state<DateValue | undefined>(undefined),
|
||||||
|
minDays: state<number | undefined>(undefined),
|
||||||
|
maxDays: state<number | undefined>(undefined),
|
||||||
|
allowSingleDay: state(true),
|
||||||
|
isDateDisabled: state<DateMatcher | undefined>(undefined),
|
||||||
|
isDateUnavailable: state<DateMatcher | undefined>(undefined),
|
||||||
|
isDateHoliday: state<DateMatcher | undefined>(undefined),
|
||||||
|
disabled: state(false),
|
||||||
|
readonly: state(false),
|
||||||
|
startReadonlySegments: state<EditableSegmentPart[]>([]),
|
||||||
|
endReadonlySegments: state<EditableSegmentPart[]>([]),
|
||||||
|
required: state(false),
|
||||||
|
closeOnRangeSelect: state(true),
|
||||||
|
granularity: state<Granularity | undefined>(undefined),
|
||||||
|
hideTimeZone: state(false),
|
||||||
|
hourCycle: state<HourCycle | undefined>(undefined),
|
||||||
|
locale: state<string | undefined>('en-US'),
|
||||||
|
dir: state<Direction>('ltr'),
|
||||||
|
pagedNavigation: state(false),
|
||||||
|
weekStartsOn: state<WeekStartsOn | undefined>(undefined),
|
||||||
|
weekdayFormat: state<Intl.DateTimeFormatOptions['weekday']>('narrow'),
|
||||||
|
fixedWeeks: state(false),
|
||||||
|
numberOfMonths: state(1),
|
||||||
|
calendarLabel: state<string | undefined>(undefined),
|
||||||
|
disableDaysOutsideMonth: state(false),
|
||||||
|
preventDeselect: state(false),
|
||||||
|
initialFocus: state(false),
|
||||||
|
monthFormat: state<Intl.DateTimeFormatOptions['month'] | ((month: number) => string)>('long'),
|
||||||
|
yearFormat: state<Intl.DateTimeFormatOptions['year'] | ((year: number) => string)>('numeric'),
|
||||||
|
showWeekNumbers: state(false),
|
||||||
|
weekNumberFormat: state<'iso' | 'simple'>('iso'),
|
||||||
|
announceNavigation: state(true),
|
||||||
|
errorMessageId: state<string | undefined>(undefined),
|
||||||
|
onOpenChangeComplete: state<OnChangeFn<boolean>>(() => {})
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('DateRangePickerProvider', () => {
|
||||||
|
afterEach(() => {
|
||||||
|
vi.restoreAllMocks();
|
||||||
|
document.body.innerHTML = '';
|
||||||
|
});
|
||||||
|
|
||||||
|
it('registers the provider part through SomaRuntime', () => {
|
||||||
|
const { dom } = installSomaHarness();
|
||||||
|
const opts = createDateRangePickerOpts();
|
||||||
|
|
||||||
|
const { result: provider, cleanup } = withEffectRoot(() =>
|
||||||
|
DateRangePickerProvider.create(opts)
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(provider.runtimePart.props.id).toBe('date-range-picker-root');
|
||||||
|
|
||||||
|
cleanup();
|
||||||
|
dom.dispose();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('closes the popover only when closeOnRangeSelect is enabled', () => {
|
||||||
|
const { dom } = installSomaHarness();
|
||||||
|
const opts = createDateRangePickerOpts();
|
||||||
|
|
||||||
|
const { result: provider, cleanup } = withEffectRoot(() =>
|
||||||
|
DateRangePickerProvider.create(opts)
|
||||||
|
);
|
||||||
|
|
||||||
|
provider.handleRangeSelect();
|
||||||
|
expect(opts.open.current).toBe(false);
|
||||||
|
|
||||||
|
opts.open.current = true;
|
||||||
|
opts.closeOnRangeSelect.current = false;
|
||||||
|
provider.handleRangeSelect();
|
||||||
|
expect(opts.open.current).toBe(true);
|
||||||
|
|
||||||
|
cleanup();
|
||||||
|
dom.dispose();
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -0,0 +1,150 @@
|
|||||||
|
// @vitest-environment jsdom
|
||||||
|
|
||||||
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||||
|
|
||||||
|
import { createActiveDom } from '$adom';
|
||||||
|
import { Time } from '$libs/days';
|
||||||
|
import type {
|
||||||
|
EditableTimeSegmentPart,
|
||||||
|
HourCycle,
|
||||||
|
TimeGranularity,
|
||||||
|
TimeOnInvalid,
|
||||||
|
TimeValidator,
|
||||||
|
TimeValue
|
||||||
|
} 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, OnChangeFn } from '../../types';
|
||||||
|
|
||||||
|
import { TimePickerProvider } from './time-picker-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(hourCycle?: HourCycle) {
|
||||||
|
const dom = createActiveDom();
|
||||||
|
const soma = {
|
||||||
|
dom,
|
||||||
|
langs: {
|
||||||
|
getLocale: () => 'en-US',
|
||||||
|
ts: (key: string) => key
|
||||||
|
},
|
||||||
|
dates: {
|
||||||
|
getHourCycle: () => hourCycle
|
||||||
|
},
|
||||||
|
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(TimePickerProvider.ctx, 'set').mockImplementation((value) => value);
|
||||||
|
|
||||||
|
return { dom };
|
||||||
|
}
|
||||||
|
|
||||||
|
function createTimePickerOpts() {
|
||||||
|
return {
|
||||||
|
id: state('time-picker-root'),
|
||||||
|
ref: state<HTMLElement | null>(null),
|
||||||
|
value: state<TimeValue | undefined>(undefined),
|
||||||
|
placeholder: state<TimeValue>(new Time(13, 45, 30)),
|
||||||
|
open: state(true),
|
||||||
|
validate: state<TimeValidator<TimeValue> | undefined>(undefined),
|
||||||
|
onInvalid: state<TimeOnInvalid | undefined>(undefined),
|
||||||
|
minValue: state<TimeValue | undefined>(undefined),
|
||||||
|
maxValue: state<TimeValue | undefined>(undefined),
|
||||||
|
disabled: state(false),
|
||||||
|
readonly: state(false),
|
||||||
|
readonlySegments: state<EditableTimeSegmentPart[]>([]),
|
||||||
|
required: state(false),
|
||||||
|
granularity: state<TimeGranularity | undefined>(undefined),
|
||||||
|
hideTimeZone: state(false),
|
||||||
|
hourCycle: state<HourCycle | undefined>(undefined),
|
||||||
|
locale: state<string | undefined>('en-US'),
|
||||||
|
dir: state<Direction>('ltr'),
|
||||||
|
hourStep: state(1),
|
||||||
|
minuteStep: state(1),
|
||||||
|
secondStep: state(1),
|
||||||
|
errorMessageId: state<string | undefined>(undefined),
|
||||||
|
onOpenChangeComplete: state<OnChangeFn<boolean>>(() => {})
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('TimePickerProvider', () => {
|
||||||
|
afterEach(() => {
|
||||||
|
vi.restoreAllMocks();
|
||||||
|
document.body.innerHTML = '';
|
||||||
|
});
|
||||||
|
|
||||||
|
it('registers the provider part and exposes 24-hour placeholder values', () => {
|
||||||
|
const { dom } = installSomaHarness(24);
|
||||||
|
const opts = createTimePickerOpts();
|
||||||
|
|
||||||
|
const { result: provider, cleanup } = withEffectRoot(() => TimePickerProvider.create(opts));
|
||||||
|
|
||||||
|
expect(provider.runtimePart.props.id).toBe('time-picker-root');
|
||||||
|
expect(provider.inferredGranularity).toBe('minute');
|
||||||
|
expect(provider.hour).toBe(13);
|
||||||
|
expect(provider.minute).toBe(45);
|
||||||
|
expect(provider.second).toBe(30);
|
||||||
|
expect(provider.dayPeriod).toBe('PM');
|
||||||
|
expect(provider.hourMin).toBe(0);
|
||||||
|
expect(provider.hourMax).toBe(23);
|
||||||
|
|
||||||
|
cleanup();
|
||||||
|
dom.dispose();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('writes slider changes back to the shared value source', () => {
|
||||||
|
const { dom } = installSomaHarness(24);
|
||||||
|
const opts = createTimePickerOpts();
|
||||||
|
|
||||||
|
const { result: provider, cleanup } = withEffectRoot(() => TimePickerProvider.create(opts));
|
||||||
|
|
||||||
|
provider.setHour(9);
|
||||||
|
provider.setMinute(5);
|
||||||
|
provider.setSecond(10);
|
||||||
|
|
||||||
|
expect(opts.value.current?.hour).toBe(9);
|
||||||
|
expect(opts.value.current?.minute).toBe(5);
|
||||||
|
expect(opts.value.current?.second).toBe(10);
|
||||||
|
|
||||||
|
cleanup();
|
||||||
|
dom.dispose();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('maps 12-hour input through the active day period', () => {
|
||||||
|
const { dom } = installSomaHarness(12);
|
||||||
|
const opts = createTimePickerOpts();
|
||||||
|
|
||||||
|
const { result: provider, cleanup } = withEffectRoot(() => TimePickerProvider.create(opts));
|
||||||
|
|
||||||
|
expect(provider.hour).toBe(1);
|
||||||
|
expect(provider.dayPeriod).toBe('PM');
|
||||||
|
expect(provider.hourMin).toBe(1);
|
||||||
|
expect(provider.hourMax).toBe(12);
|
||||||
|
|
||||||
|
provider.setHour(3);
|
||||||
|
expect(opts.value.current?.hour).toBe(15);
|
||||||
|
|
||||||
|
provider.setDayPeriod('AM');
|
||||||
|
expect(opts.value.current?.hour).toBe(3);
|
||||||
|
|
||||||
|
provider.setDayPeriod('PM');
|
||||||
|
expect(opts.value.current?.hour).toBe(15);
|
||||||
|
|
||||||
|
cleanup();
|
||||||
|
dom.dispose();
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Reference in new issue