@jsb188/form 1.1.2 → 1.1.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1 -1
- package/package.json +16 -7
- package/src/modules/Form-Line.tsx +8 -2
- package/src/modules/ListItemForm.tsx +9 -8
- package/src/modules/ListItemForm.types.ts +1 -0
- package/src/modules/PopOverInput.tsx +1 -0
- package/src/ui/FormUI.tsx +20 -9
- package/src/ui/Input.tsx +1 -0
- package/src/ui/Input.types.ts +1 -1
- package/src/ui/LineForm.tsx +5 -2
- package/tests/controller.test.tsx +0 -336
- package/tests/controls.test.tsx +0 -261
- package/tests/schema-form.test.tsx +0 -303
- package/tests/settings-definitions.test.ts +0 -129
- package/tests/sms-input.test.tsx +0 -131
- package/tests/time-input.test.tsx +0 -395
- package/tests/types.ts +0 -102
- package/tests/upload-input.test.tsx +0 -114
- package/tests/validation.test.ts +0 -65
- package/tsconfig.json +0 -13
- package/vitest.config.ts +0 -8
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
import type { AccountData } from '@jsb188/app/types/auth.d.ts';
|
|
2
|
-
import type { OrganizationGQL } from '@jsb188/mday/types/organization.d.ts';
|
|
3
|
-
import i18n, { i18nTranslations } from '@jsb188/app/i18n/index.ts';
|
|
4
|
-
import { makeEditAccountSchema } from '@jsb188/auth-web/schemas';
|
|
5
|
-
import { addressFormValues, makeAddressSchema } from '@jsb188/auth-web/schemas/address';
|
|
6
|
-
import { makeEditOrganizationSchema } from '@jsb188/mday-web/schemas/organization';
|
|
7
|
-
import { beforeAll, describe, expect, it, vi } from 'vitest';
|
|
8
|
-
|
|
9
|
-
/* Initialize the same translations used by the application for schema labels and errors. */
|
|
10
|
-
beforeAll(() => {
|
|
11
|
-
i18n.extend(Object.fromEntries(Object.entries(i18nTranslations).map(([namespace, translations]) => [namespace, translations.en])));
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
/* Build a complete saved address while retaining optional second-line behavior. */
|
|
15
|
-
function completeAddress() {
|
|
16
|
-
return addressFormValues({ line1: '1 Main St', postalCode: 'A1A 1A1', city: 'Toronto', state: 'ON', country: 'CA' });
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/* Supply the saved account data used to test read-only verification controls. */
|
|
20
|
-
function savedAccount(): AccountData {
|
|
21
|
-
return {
|
|
22
|
-
id: 'account',
|
|
23
|
-
deleted: false,
|
|
24
|
-
plus: null,
|
|
25
|
-
profile: { id: 'profile', firstName: 'Ada', lastName: 'Lovelace', photoId: '', photoUri: '' },
|
|
26
|
-
settings: { language: 'en', theme: null, lightMode: 'SYSTEM', timeZone: null },
|
|
27
|
-
email: { id: 'email', address: 'ada@example.com', verified: true },
|
|
28
|
-
phone: { id: 'phone', number: '14165550123', verified: true },
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
describe('migrated settings definitions', () => {
|
|
33
|
-
it('accepts an empty optional address and a country-only selection', () => {
|
|
34
|
-
const definition = makeAddressSchema(null);
|
|
35
|
-
expect(definition.schema.safeParse(definition.defaults).success).toBe(true);
|
|
36
|
-
expect(definition.schema.safeParse({ address: addressFormValues({ country: 'CA' }) }).success).toBe(true);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it('reports missing partial-address fields by nested path and accepts a complete address without line two', () => {
|
|
40
|
-
const definition = makeAddressSchema(null);
|
|
41
|
-
const result = definition.schema.safeParse({ address: addressFormValues({ line1: '1 Main St' }) });
|
|
42
|
-
expect(result.success).toBe(false);
|
|
43
|
-
if (!result.success) {
|
|
44
|
-
expect(result.error.issues.map((issue) => issue.path.join('.'))).toEqual([
|
|
45
|
-
'address.postalCode',
|
|
46
|
-
'address.city',
|
|
47
|
-
'address.state',
|
|
48
|
-
'address.country',
|
|
49
|
-
]);
|
|
50
|
-
expect(result.error.issues[0].message).toBe(i18n.t('error.msg_value_required', { value: i18n.t('c.postalCode') }));
|
|
51
|
-
}
|
|
52
|
-
expect(definition.schema.safeParse({ address: completeAddress() }).success).toBe(true);
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
it('preserves the grouped country popover and resolves selection from the current draft', () => {
|
|
56
|
-
const definition = makeAddressSchema(null, 'w_350');
|
|
57
|
-
const scope = { values: { address: completeAddress() }, context: undefined };
|
|
58
|
-
const popover = definition.fields['address.country'].resolve(scope).popOverProps;
|
|
59
|
-
expect(popover.iface.variables.designClassName).toBe('w_350');
|
|
60
|
-
expect(popover.iface.variables.options.slice(0, 4).map((option) => ('value' in option ? option.value : option.__type))).toEqual([
|
|
61
|
-
'US',
|
|
62
|
-
'MX',
|
|
63
|
-
'CA',
|
|
64
|
-
'BREAK',
|
|
65
|
-
]);
|
|
66
|
-
expect(popover.iface.variables.initialState).toEqual({ 'address.country': 'CA' });
|
|
67
|
-
expect(popover.iface.variables.options[2]).toMatchObject({ value: 'CA', selected: true });
|
|
68
|
-
const countryItems = popover.iface.variables.options.filter((option) => option.__type === 'LIST_ITEM');
|
|
69
|
-
expect(new Set(countryItems.map((option) => option.id)).size).toBe(countryItems.length);
|
|
70
|
-
expect(definition.defaults.address.country).toBe('');
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it('keeps verification actions explicit and resolves current account display values without altering editable defaults', () => {
|
|
74
|
-
const account = savedAccount();
|
|
75
|
-
const openModal = vi.fn();
|
|
76
|
-
const definition = makeEditAccountSchema(account, account.settings, openModal);
|
|
77
|
-
expect(definition.schema.safeParse(definition.defaults).success).toBe(true);
|
|
78
|
-
expect(definition.fields.change_emailAddress.getter()).toBe('ada@example.com');
|
|
79
|
-
expect(definition.fields.change_password.getter()).toBe('....');
|
|
80
|
-
expect(openModal).not.toHaveBeenCalled();
|
|
81
|
-
definition.fields.change_emailAddress.onClickButton();
|
|
82
|
-
expect(openModal).toHaveBeenCalledWith({ preset: 'CHANGE_EMAIL', name: 'edit_account_email' });
|
|
83
|
-
const refreshed = makeEditAccountSchema(
|
|
84
|
-
{ ...account, email: { ...account.email, address: 'new@example.com' } },
|
|
85
|
-
account.settings,
|
|
86
|
-
openModal,
|
|
87
|
-
);
|
|
88
|
-
expect(refreshed.defaults).toEqual(definition.defaults);
|
|
89
|
-
expect(refreshed.fields.change_emailAddress.getter()).toBe('new@example.com');
|
|
90
|
-
const draft = { ...definition.defaults, settings: { language: 'es' } };
|
|
91
|
-
const options = definition.fields['settings.language'].resolve({ values: draft, context: undefined }).popOverProps.iface.variables
|
|
92
|
-
.options;
|
|
93
|
-
expect(options.find((option) => option.value === 'es')?.selected).toBe(true);
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
it('preserves organization section order, nullable language, and display preference', () => {
|
|
97
|
-
const organization = { name: 'Test', operation: 'ARABLE', address: completeAddress() } as OrganizationGQL;
|
|
98
|
-
const definition = makeEditOrganizationSchema(organization, {
|
|
99
|
-
translateLanguage: null,
|
|
100
|
-
displayTranslatedLogs: false,
|
|
101
|
-
});
|
|
102
|
-
expect(definition.schema.safeParse(definition.defaults).success).toBe(true);
|
|
103
|
-
expect(definition.layout?.slice(0, 7).map((item) => (typeof item === 'string' ? item : item.id))).toEqual([
|
|
104
|
-
'information',
|
|
105
|
-
'name',
|
|
106
|
-
'operation',
|
|
107
|
-
'language',
|
|
108
|
-
'translateLanguage',
|
|
109
|
-
'displayTranslatedLogs',
|
|
110
|
-
'address',
|
|
111
|
-
]);
|
|
112
|
-
const scope = { values: definition.defaults, context: undefined };
|
|
113
|
-
const language = definition.fields.translateLanguage.resolve(scope);
|
|
114
|
-
expect(language.inputClassName).toBe('cl_md');
|
|
115
|
-
expect(language.popOverProps.iface.variables.options[0]).toMatchObject({ value: null, selected: true });
|
|
116
|
-
expect(language.popOverProps.iface.variables.options.slice(1).map((option) => [option.value, option.text])).toEqual([
|
|
117
|
-
['zh-Hans', 'Chinese (Simplified)'],
|
|
118
|
-
['en', 'English'],
|
|
119
|
-
['fr', 'French'],
|
|
120
|
-
['de', 'German'],
|
|
121
|
-
['it', 'Italian'],
|
|
122
|
-
['pt', 'Portuguese'],
|
|
123
|
-
['es', 'Spanish'],
|
|
124
|
-
['vi', 'Vietnamese'],
|
|
125
|
-
]);
|
|
126
|
-
const preference = definition.fields.displayTranslatedLogs.resolve(scope);
|
|
127
|
-
expect(preference.popOverProps.iface.variables.options[1]).toMatchObject({ value: false, selected: true });
|
|
128
|
-
});
|
|
129
|
-
});
|
package/tests/sms-input.test.tsx
DELETED
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import { JSDOM } from 'jsdom';
|
|
2
|
-
import { act, StrictMode } from 'react';
|
|
3
|
-
import type { Root } from 'react-dom/client';
|
|
4
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
5
|
-
import { SMSCodeInput } from '../src/modules/SMSCodeInput';
|
|
6
|
-
|
|
7
|
-
let dom: JSDOM;
|
|
8
|
-
let container: HTMLDivElement;
|
|
9
|
-
let root: Root;
|
|
10
|
-
|
|
11
|
-
beforeEach(async () => {
|
|
12
|
-
dom = new JSDOM('<!doctype html><html><body></body></html>');
|
|
13
|
-
vi.stubGlobal('window', dom.window);
|
|
14
|
-
vi.stubGlobal('document', dom.window.document);
|
|
15
|
-
vi.stubGlobal('HTMLInputElement', dom.window.HTMLInputElement);
|
|
16
|
-
vi.stubGlobal('Event', dom.window.Event);
|
|
17
|
-
vi.stubGlobal('KeyboardEvent', dom.window.KeyboardEvent);
|
|
18
|
-
vi.stubGlobal('addEventListener', dom.window.addEventListener.bind(dom.window));
|
|
19
|
-
vi.stubGlobal('removeEventListener', dom.window.removeEventListener.bind(dom.window));
|
|
20
|
-
vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true);
|
|
21
|
-
const { createRoot } = await import('react-dom/client');
|
|
22
|
-
container = document.createElement('div');
|
|
23
|
-
document.body.append(container);
|
|
24
|
-
root = createRoot(container);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
afterEach(() => {
|
|
28
|
-
act(() => root.unmount());
|
|
29
|
-
dom.window.close();
|
|
30
|
-
vi.unstubAllGlobals();
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
/* Keep Strict Mode enabled so verification never relies on a state updater executing only once. */
|
|
34
|
-
function render(props: React.ComponentProps<typeof SMSCodeInput>) {
|
|
35
|
-
act(() =>
|
|
36
|
-
root.render(
|
|
37
|
-
<StrictMode>
|
|
38
|
-
<SMSCodeInput {...props} />
|
|
39
|
-
</StrictMode>,
|
|
40
|
-
),
|
|
41
|
-
);
|
|
42
|
-
return container.querySelector('input')!;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/* Simulate native typing or autofill without changing React's controlled-input tracker. */
|
|
46
|
-
function enterCode(input: HTMLInputElement, value: string) {
|
|
47
|
-
act(() => {
|
|
48
|
-
Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')!.set!.call(input, value);
|
|
49
|
-
input.dispatchEvent(new Event('input', { bubbles: true }));
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/* Paste the full clipboard value, including spaces that browsers would otherwise truncate at maxLength. */
|
|
54
|
-
function pasteCode(input: HTMLInputElement, value: string) {
|
|
55
|
-
const event = new Event('paste', { bubbles: true, cancelable: true });
|
|
56
|
-
Object.defineProperty(event, 'clipboardData', { value: { getData: () => value } });
|
|
57
|
-
act(() => input.dispatchEvent(event));
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
describe('SMS verification input', () => {
|
|
61
|
-
it('accepts formatted paste and leading zeroes, submitting a complete code only once', () => {
|
|
62
|
-
const onSubmit = vi.fn();
|
|
63
|
-
const input = render({ onSubmit });
|
|
64
|
-
expect(input.type).toBe('text');
|
|
65
|
-
expect(input.inputMode).toBe('numeric');
|
|
66
|
-
expect(input.autocomplete).toBe('one-time-code');
|
|
67
|
-
pasteCode(input, '01 23456');
|
|
68
|
-
expect(input.value).toBe('01234');
|
|
69
|
-
expect(onSubmit).toHaveBeenCalledExactlyOnceWith('01234');
|
|
70
|
-
pasteCode(input, '01234');
|
|
71
|
-
act(() => input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true, cancelable: true })));
|
|
72
|
-
expect(onSubmit).toHaveBeenCalledOnce();
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
it('submits a global digit once in Strict Mode and ignores non-digit shortcuts', () => {
|
|
76
|
-
const onSubmit = vi.fn();
|
|
77
|
-
const input = render({ codeLength: 1, onSubmit });
|
|
78
|
-
act(() => window.dispatchEvent(new KeyboardEvent('keydown', { key: ' ', bubbles: true, cancelable: true })));
|
|
79
|
-
expect(input.value).toBe('');
|
|
80
|
-
act(() => window.dispatchEvent(new KeyboardEvent('keydown', { key: '7', bubbles: true, cancelable: true })));
|
|
81
|
-
expect(input.value).toBe('7');
|
|
82
|
-
expect(onSubmit).toHaveBeenCalledExactlyOnceWith('7');
|
|
83
|
-
const pasteShortcut = new KeyboardEvent('keydown', { key: 'v', ctrlKey: true, bubbles: true, cancelable: true });
|
|
84
|
-
act(() => input.dispatchEvent(pasteShortcut));
|
|
85
|
-
expect(pasteShortcut.defaultPrevented).toBe(false);
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it('keeps partial input local, blocks busy verification, and allows the same code after an error', () => {
|
|
89
|
-
const onSubmit = vi.fn();
|
|
90
|
-
const onChangeCode = vi.fn();
|
|
91
|
-
const input = render({ onSubmit, onChangeCode });
|
|
92
|
-
enterCode(input, '00x2');
|
|
93
|
-
expect(input.value).toBe('002');
|
|
94
|
-
expect(onSubmit).not.toHaveBeenCalled();
|
|
95
|
-
expect(onChangeCode).toHaveBeenLastCalledWith('002');
|
|
96
|
-
enterCode(input, '00234');
|
|
97
|
-
expect(onSubmit).toHaveBeenCalledExactlyOnceWith('00234');
|
|
98
|
-
render({ onSubmit, saving: true });
|
|
99
|
-
expect(input.disabled).toBe(true);
|
|
100
|
-
pasteCode(input, '98765');
|
|
101
|
-
expect(onSubmit).toHaveBeenCalledOnce();
|
|
102
|
-
render({ onSubmit, error: { message: 'Try again' } });
|
|
103
|
-
expect(input.value).toBe('');
|
|
104
|
-
enterCode(input, '00234');
|
|
105
|
-
expect(onSubmit).toHaveBeenCalledTimes(2);
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
it('does not consume digits while another text field has focus', () => {
|
|
109
|
-
const onSubmit = vi.fn();
|
|
110
|
-
const input = render({ onSubmit });
|
|
111
|
-
const other = document.createElement('input');
|
|
112
|
-
document.body.append(other);
|
|
113
|
-
other.focus();
|
|
114
|
-
const event = new KeyboardEvent('keydown', { key: '3', bubbles: true, cancelable: true });
|
|
115
|
-
act(() => other.dispatchEvent(event));
|
|
116
|
-
expect(event.defaultPrevented).toBe(false);
|
|
117
|
-
expect(input.value).toBe('');
|
|
118
|
-
expect(onSubmit).not.toHaveBeenCalled();
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
it('allows an explicit retry after a request stops saving without clearing the entered code', () => {
|
|
122
|
-
const onSubmit = vi.fn();
|
|
123
|
-
const input = render({ onSubmit });
|
|
124
|
-
enterCode(input, '01234');
|
|
125
|
-
render({ onSubmit, saving: true });
|
|
126
|
-
render({ onSubmit, saving: false });
|
|
127
|
-
expect(input.value).toBe('01234');
|
|
128
|
-
act(() => input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true, cancelable: true })));
|
|
129
|
-
expect(onSubmit).toHaveBeenCalledTimes(2);
|
|
130
|
-
});
|
|
131
|
-
});
|
|
@@ -1,395 +0,0 @@
|
|
|
1
|
-
import { usePopOverActions, usePopOverDefinition, usePopOverEvent } from '@jsb188/react/states';
|
|
2
|
-
import { createStore, Provider } from 'jotai';
|
|
3
|
-
import { JSDOM } from 'jsdom';
|
|
4
|
-
import { act, useState, type ReactNode } from 'react';
|
|
5
|
-
import type { Root } from 'react-dom/client';
|
|
6
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
7
|
-
import { InputTimeFromDate } from '../src/modules/TimeInput';
|
|
8
|
-
import { checkTimeError, getTimeFormValues, resolveTimeInput } from '../src/modules/TimeInput.helpers';
|
|
9
|
-
import type { TimeFormValues, TimeInputValue } from '../src/modules/TimeInput.types';
|
|
10
|
-
|
|
11
|
-
/* Resolve clock parts with a fixed fallback date so calendar and DST assertions are deterministic. */
|
|
12
|
-
function resolve(value: TimeInputValue, values: TimeFormValues, timeZone = 'America/Toronto') {
|
|
13
|
-
return resolveTimeInput({ value, values, timeZone, fallbackDate: new Date('2026-09-20T01:02:03.456Z') });
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/* Read a successfully resolved instant without allowing an invalid branch to hide a failed assertion. */
|
|
17
|
-
function resolvedISO(result: ReturnType<typeof resolve>) {
|
|
18
|
-
expect(result.status).toBe('valid');
|
|
19
|
-
return result.status === 'valid' ? result.value.toISOString() : '';
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
describe('timezone and twelve-hour math', () => {
|
|
23
|
-
it('formats midnight, noon and a different local calendar date in the requested zone', () => {
|
|
24
|
-
expect(getTimeFormValues('2026-01-02T05:00:00Z', 'America/Toronto')).toEqual({ hours: '12', minutes: '0', AMPM: 'AM' });
|
|
25
|
-
expect(getTimeFormValues('2026-01-02T17:05:00Z', 'America/Toronto')).toEqual({ hours: '12', minutes: '5', AMPM: 'PM' });
|
|
26
|
-
expect(getTimeFormValues('2026-01-02T01:20:30.456Z', 'America/Toronto')).toEqual({ hours: '8', minutes: '20', AMPM: 'PM' });
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it('keeps afternoon edits in PM and preserves the local date, seconds and milliseconds', () => {
|
|
30
|
-
expect(resolvedISO(resolve('2026-01-02T01:20:30.456Z', { hours: '4', minutes: '20', AMPM: 'PM' }))).toBe(
|
|
31
|
-
'2026-01-01T21:20:30.456Z',
|
|
32
|
-
);
|
|
33
|
-
expect(resolvedISO(resolve('2026-01-02T01:20:30.456Z', { hours: '12', minutes: '5', AMPM: 'AM' }))).toBe(
|
|
34
|
-
'2026-01-01T05:05:30.456Z',
|
|
35
|
-
);
|
|
36
|
-
expect(resolvedISO(resolve('2026-01-02T01:20:30.456Z', { hours: '12', minutes: '5', AMPM: 'PM' }))).toBe(
|
|
37
|
-
'2026-01-01T17:05:30.456Z',
|
|
38
|
-
);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('uses the runtime local zone when none is supplied', () => {
|
|
42
|
-
expect(getTimeFormValues(new Date(2026, 0, 1, 9, 15))).toEqual({ hours: '9', minutes: '15', AMPM: 'AM' });
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it.each([
|
|
46
|
-
['2026-03-08T06:30:00Z', 'America/Toronto', '2', '30'],
|
|
47
|
-
['2026-03-29T00:30:00Z', 'Europe/Berlin', '2', '30'],
|
|
48
|
-
['2026-10-03T15:00:00Z', 'Australia/Lord_Howe', '2', '15'],
|
|
49
|
-
])('rejects nonexistent DST clock time for %s in %s', (value, zone, hours, minutes) => {
|
|
50
|
-
expect(resolve(value, { hours, minutes, AMPM: 'AM' }, zone)).toMatchObject({ status: 'invalid', reason: 'gap' });
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it.each([
|
|
54
|
-
['2026-11-01T05:30:00Z', '2026-11-01T05:15:00.000Z'],
|
|
55
|
-
['2026-11-01T06:30:00Z', '2026-11-01T06:15:00.000Z'],
|
|
56
|
-
])('preserves the original offset of repeated time %s', (value, expected) => {
|
|
57
|
-
expect(resolvedISO(resolve(value, { hours: '1', minutes: '15', AMPM: 'AM' }))).toBe(expected);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it('chooses the earlier repeated instant when an empty value has no stored offset to preserve', () => {
|
|
61
|
-
const result = resolveTimeInput({
|
|
62
|
-
value: '',
|
|
63
|
-
values: { hours: '1', minutes: '15', AMPM: 'AM' },
|
|
64
|
-
timeZone: 'America/Toronto',
|
|
65
|
-
fallbackDate: new Date('2026-11-01T12:00:00Z'),
|
|
66
|
-
});
|
|
67
|
-
expect(resolvedISO(result)).toBe('2026-11-01T05:15:00.000Z');
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it('initializes blank values on today in the requested zone without inheriting seconds', () => {
|
|
71
|
-
expect(resolvedISO(resolve('', { hours: '4', minutes: '30', AMPM: 'PM' }, 'America/Los_Angeles'))).toBe(
|
|
72
|
-
'2026-09-19T23:30:00.000Z',
|
|
73
|
-
);
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
it('rejects invalid dates and zones without creating a fallback date', () => {
|
|
77
|
-
expect(resolve('not-a-date', { hours: '4', minutes: '30', AMPM: 'PM' })).toMatchObject({ status: 'invalid', reason: 'date' });
|
|
78
|
-
expect(resolve('2026-01-01T00:00:00Z', { hours: '4', minutes: '30', AMPM: 'PM' }, 'Not/A_Timezone')).toMatchObject({
|
|
79
|
-
status: 'invalid',
|
|
80
|
-
reason: 'zone',
|
|
81
|
-
});
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it('keeps incomplete values separate from malformed or out-of-range clock parts', () => {
|
|
85
|
-
expect(resolve('2026-01-01T00:00:00Z', { hours: '', minutes: '30', AMPM: 'PM' })).toEqual({ status: 'incomplete' });
|
|
86
|
-
expect(resolve('2026-01-01T00:00:00Z', { hours: '4', minutes: '', AMPM: 'PM' })).toEqual({ status: 'incomplete' });
|
|
87
|
-
for (const value of ['0', '13', '-1', '1.5', '001', '1e1']) expect(checkTimeError(value, 'hours')).toBe(true);
|
|
88
|
-
for (const value of ['60', '-1', '1.5', '001', '1e1']) expect(checkTimeError(value, 'minutes')).toBe(true);
|
|
89
|
-
expect(checkTimeError('00', 'minutes')).toBe(false);
|
|
90
|
-
expect(checkTimeError('12', 'hours')).toBe(false);
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
describe('time control interactions', () => {
|
|
95
|
-
let dom: JSDOM;
|
|
96
|
-
let root: Root;
|
|
97
|
-
let container: HTMLDivElement;
|
|
98
|
-
let store: ReturnType<typeof createStore>;
|
|
99
|
-
|
|
100
|
-
beforeEach(async () => {
|
|
101
|
-
dom = new JSDOM('<!doctype html><html><body></body></html>');
|
|
102
|
-
vi.stubGlobal('window', dom.window);
|
|
103
|
-
vi.stubGlobal('document', dom.window.document);
|
|
104
|
-
vi.stubGlobal('HTMLInputElement', dom.window.HTMLInputElement);
|
|
105
|
-
vi.stubGlobal('Event', dom.window.Event);
|
|
106
|
-
vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true);
|
|
107
|
-
const { createRoot } = await import('react-dom/client');
|
|
108
|
-
container = document.createElement('div');
|
|
109
|
-
document.body.append(container);
|
|
110
|
-
root = createRoot(container);
|
|
111
|
-
store = createStore();
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
afterEach(() => {
|
|
115
|
-
act(() => root.unmount());
|
|
116
|
-
container.remove();
|
|
117
|
-
dom.window.close();
|
|
118
|
-
vi.useRealTimers();
|
|
119
|
-
vi.unstubAllGlobals();
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
/* Mount or rerender a control without replacing its local draft state or popover store. */
|
|
123
|
-
const render = (children: ReactNode) => act(() => root.render(<Provider store={store}>{children}</Provider>));
|
|
124
|
-
|
|
125
|
-
/* Type through React's native input event integration, bypassing its value tracker as a browser edit would. */
|
|
126
|
-
const type = (name: 'hours' | 'minutes', value: string) => {
|
|
127
|
-
const input = container.querySelector<HTMLInputElement>(`[name="date_${name}"]`)!;
|
|
128
|
-
act(() => {
|
|
129
|
-
Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')!.set!.call(input, value);
|
|
130
|
-
input.dispatchEvent(new Event('input', { bubbles: true }));
|
|
131
|
-
});
|
|
132
|
-
return input;
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
it('commits PM edits with the configured timezone and retains partial values until complete', () => {
|
|
136
|
-
const onChange = vi.fn();
|
|
137
|
-
render(
|
|
138
|
-
<InputTimeFromDate
|
|
139
|
-
name="date"
|
|
140
|
-
value="2026-01-01T20:30:12.123Z"
|
|
141
|
-
timeZone="America/Toronto"
|
|
142
|
-
onChange={onChange}
|
|
143
|
-
/>,
|
|
144
|
-
);
|
|
145
|
-
type('hours', '');
|
|
146
|
-
type('minutes', '10');
|
|
147
|
-
expect(onChange).not.toHaveBeenCalled();
|
|
148
|
-
type('hours', '4');
|
|
149
|
-
expect(onChange).toHaveBeenCalledOnce();
|
|
150
|
-
expect(onChange.mock.calls[0][2].toISOString()).toBe('2026-01-01T21:10:12.123Z');
|
|
151
|
-
expect(container.querySelector('[role="button"]')?.textContent).toBe('PM');
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
it('initializes an empty standalone control on today in its zone', () => {
|
|
155
|
-
vi.useFakeTimers({ toFake: ['Date'] });
|
|
156
|
-
vi.setSystemTime(new Date('2026-09-20T01:02:03.456Z'));
|
|
157
|
-
const onChange = vi.fn();
|
|
158
|
-
render(
|
|
159
|
-
<InputTimeFromDate
|
|
160
|
-
name="date"
|
|
161
|
-
value=""
|
|
162
|
-
timeZone="America/Los_Angeles"
|
|
163
|
-
onChange={onChange}
|
|
164
|
-
/>,
|
|
165
|
-
);
|
|
166
|
-
type('hours', '12');
|
|
167
|
-
expect(onChange).not.toHaveBeenCalled();
|
|
168
|
-
type('minutes', '5');
|
|
169
|
-
expect(onChange.mock.calls[0][2].toISOString()).toBe('2026-09-19T07:05:00.000Z');
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
it('preserves blank hours while real focus moves to minutes and completes the time', () => {
|
|
173
|
-
vi.useFakeTimers({ toFake: ['Date'] });
|
|
174
|
-
vi.setSystemTime(new Date('2026-09-20T01:02:03.456Z'));
|
|
175
|
-
const onChange = vi.fn();
|
|
176
|
-
const onBlur = vi.fn();
|
|
177
|
-
render(
|
|
178
|
-
<InputTimeFromDate
|
|
179
|
-
name="date"
|
|
180
|
-
value=""
|
|
181
|
-
timeZone="America/Los_Angeles"
|
|
182
|
-
onChange={onChange}
|
|
183
|
-
onBlur={onBlur}
|
|
184
|
-
/>,
|
|
185
|
-
);
|
|
186
|
-
const hours = container.querySelector<HTMLInputElement>('[name="date_hours"]')!;
|
|
187
|
-
const minutes = container.querySelector<HTMLInputElement>('[name="date_minutes"]')!;
|
|
188
|
-
act(() => hours.focus());
|
|
189
|
-
type('hours', '12');
|
|
190
|
-
act(() => minutes.focus());
|
|
191
|
-
expect(hours.value).toBe('12');
|
|
192
|
-
expect(onBlur).not.toHaveBeenCalled();
|
|
193
|
-
type('minutes', '5');
|
|
194
|
-
expect(onChange).toHaveBeenCalledOnce();
|
|
195
|
-
expect(onChange.mock.calls[0][2].toISOString()).toBe('2026-09-19T07:05:00.000Z');
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
it('preserves partial drafts through the period trigger and active popup before returning to minutes', () => {
|
|
199
|
-
vi.useFakeTimers({ toFake: ['Date'] });
|
|
200
|
-
vi.setSystemTime(new Date('2026-09-20T01:02:03.456Z'));
|
|
201
|
-
const onChange = vi.fn();
|
|
202
|
-
let actions: ReturnType<typeof usePopOverActions>;
|
|
203
|
-
/* Subscribe to the real bundled period events while its portal receives keyboard focus. */
|
|
204
|
-
const Harness = () => {
|
|
205
|
-
actions = usePopOverActions();
|
|
206
|
-
return (
|
|
207
|
-
<InputTimeFromDate
|
|
208
|
-
name="date"
|
|
209
|
-
value=""
|
|
210
|
-
timeZone="America/Los_Angeles"
|
|
211
|
-
onChange={onChange}
|
|
212
|
-
/>
|
|
213
|
-
);
|
|
214
|
-
};
|
|
215
|
-
render(<Harness />);
|
|
216
|
-
const hours = container.querySelector<HTMLInputElement>('[name="date_hours"]')!;
|
|
217
|
-
const minutes = container.querySelector<HTMLInputElement>('[name="date_minutes"]')!;
|
|
218
|
-
const period = container.querySelector<HTMLElement>('[role="button"]')!;
|
|
219
|
-
act(() => hours.focus());
|
|
220
|
-
type('hours', '12');
|
|
221
|
-
act(() => period.focus());
|
|
222
|
-
expect(hours.value).toBe('12');
|
|
223
|
-
act(() => period.click());
|
|
224
|
-
const popup = document.createElement('div');
|
|
225
|
-
popup.className = 'popover';
|
|
226
|
-
const option = document.createElement('button');
|
|
227
|
-
popup.append(option);
|
|
228
|
-
document.body.append(popup);
|
|
229
|
-
act(() => option.focus());
|
|
230
|
-
expect(hours.value).toBe('12');
|
|
231
|
-
act(() => actions!.publishPopOverEvent({ action: 'ITEM', value: 'PM' }));
|
|
232
|
-
expect(onChange).not.toHaveBeenCalled();
|
|
233
|
-
act(() => minutes.focus());
|
|
234
|
-
type('minutes', '5');
|
|
235
|
-
expect(onChange.mock.calls[0][2].toISOString()).toBe('2026-09-19T19:05:00.000Z');
|
|
236
|
-
popup.remove();
|
|
237
|
-
});
|
|
238
|
-
|
|
239
|
-
it('restores incomplete drafts when focus leaves through the period trigger', () => {
|
|
240
|
-
render(
|
|
241
|
-
<InputTimeFromDate
|
|
242
|
-
name="date"
|
|
243
|
-
value="2026-01-01T20:30:00Z"
|
|
244
|
-
timeZone="America/Toronto"
|
|
245
|
-
/>,
|
|
246
|
-
);
|
|
247
|
-
const hours = container.querySelector<HTMLInputElement>('[name="date_hours"]')!;
|
|
248
|
-
const period = container.querySelector<HTMLElement>('[role="button"]')!;
|
|
249
|
-
const outside = document.createElement('button');
|
|
250
|
-
document.body.append(outside);
|
|
251
|
-
act(() => hours.focus());
|
|
252
|
-
type('hours', '');
|
|
253
|
-
act(() => period.focus());
|
|
254
|
-
expect(hours.value).toBe('');
|
|
255
|
-
act(() => outside.focus());
|
|
256
|
-
expect(hours.value).toBe('3');
|
|
257
|
-
outside.remove();
|
|
258
|
-
});
|
|
259
|
-
|
|
260
|
-
it('rejects gap edits and restores committed twelve-hour text on blur', () => {
|
|
261
|
-
const onChange = vi.fn();
|
|
262
|
-
render(
|
|
263
|
-
<InputTimeFromDate
|
|
264
|
-
name="date"
|
|
265
|
-
value="2026-03-08T06:30:00Z"
|
|
266
|
-
timeZone="America/Toronto"
|
|
267
|
-
onChange={onChange}
|
|
268
|
-
/>,
|
|
269
|
-
);
|
|
270
|
-
const hours = type('hours', '2');
|
|
271
|
-
expect(onChange).not.toHaveBeenCalled();
|
|
272
|
-
expect(hours.getAttribute('aria-invalid')).toBe('true');
|
|
273
|
-
act(() => hours.dispatchEvent(new Event('focusout', { bubbles: true })));
|
|
274
|
-
expect(hours.value).toBe('1');
|
|
275
|
-
expect(hours.hasAttribute('aria-invalid')).toBe(false);
|
|
276
|
-
});
|
|
277
|
-
|
|
278
|
-
it('resets partial drafts when the value, timezone or explicit reset key changes', () => {
|
|
279
|
-
const props = { name: 'date', value: '2026-01-01T20:30:00Z', timeZone: 'America/Toronto', onChange: vi.fn() };
|
|
280
|
-
render(
|
|
281
|
-
<InputTimeFromDate
|
|
282
|
-
{...props}
|
|
283
|
-
resetKey={0}
|
|
284
|
-
/>,
|
|
285
|
-
);
|
|
286
|
-
const hours = type('hours', '');
|
|
287
|
-
render(
|
|
288
|
-
<InputTimeFromDate
|
|
289
|
-
{...props}
|
|
290
|
-
resetKey={0}
|
|
291
|
-
/>,
|
|
292
|
-
);
|
|
293
|
-
expect(hours.value).toBe('');
|
|
294
|
-
render(
|
|
295
|
-
<InputTimeFromDate
|
|
296
|
-
{...props}
|
|
297
|
-
resetKey={1}
|
|
298
|
-
/>,
|
|
299
|
-
);
|
|
300
|
-
expect(hours.value).toBe('3');
|
|
301
|
-
type('hours', '');
|
|
302
|
-
render(
|
|
303
|
-
<InputTimeFromDate
|
|
304
|
-
{...props}
|
|
305
|
-
timeZone="America/Los_Angeles"
|
|
306
|
-
resetKey={1}
|
|
307
|
-
/>,
|
|
308
|
-
);
|
|
309
|
-
expect(hours.value).toBe('12');
|
|
310
|
-
type('hours', '');
|
|
311
|
-
render(
|
|
312
|
-
<InputTimeFromDate
|
|
313
|
-
{...props}
|
|
314
|
-
value="2026-01-02T22:45:00Z"
|
|
315
|
-
resetKey={1}
|
|
316
|
-
/>,
|
|
317
|
-
);
|
|
318
|
-
expect(hours.value).toBe('5');
|
|
319
|
-
});
|
|
320
|
-
|
|
321
|
-
it.each(['locked', 'disabled'] as const)('ignores and acknowledges an already-open period event when %s', (flag) => {
|
|
322
|
-
const onChange = vi.fn();
|
|
323
|
-
let setGuard: (value: boolean) => void;
|
|
324
|
-
let state: {
|
|
325
|
-
actions: ReturnType<typeof usePopOverActions>;
|
|
326
|
-
definition: ReturnType<typeof usePopOverDefinition>;
|
|
327
|
-
event: ReturnType<typeof usePopOverEvent>;
|
|
328
|
-
};
|
|
329
|
-
/* Observe the real event store while the owner changes edit permissions. */
|
|
330
|
-
const Harness = () => {
|
|
331
|
-
const [guard, setState] = useState(false);
|
|
332
|
-
setGuard = setState;
|
|
333
|
-
state = { actions: usePopOverActions(), definition: usePopOverDefinition(), event: usePopOverEvent() };
|
|
334
|
-
return (
|
|
335
|
-
<InputTimeFromDate
|
|
336
|
-
name="date"
|
|
337
|
-
value="2026-01-01T20:30:00Z"
|
|
338
|
-
timeZone="America/Toronto"
|
|
339
|
-
onChange={onChange}
|
|
340
|
-
{...{ [flag]: guard }}
|
|
341
|
-
/>
|
|
342
|
-
);
|
|
343
|
-
};
|
|
344
|
-
render(<Harness />);
|
|
345
|
-
act(() => container.querySelector<HTMLElement>('[role="button"]')!.click());
|
|
346
|
-
expect(state!.definition).not.toBeNull();
|
|
347
|
-
act(() => {
|
|
348
|
-
setGuard!(true);
|
|
349
|
-
state!.actions.publishPopOverEvent({ action: 'ITEM', value: 'AM' });
|
|
350
|
-
});
|
|
351
|
-
expect(onChange).not.toHaveBeenCalled();
|
|
352
|
-
expect(state!.event).toBeNull();
|
|
353
|
-
expect(container.querySelector<HTMLInputElement>('input')!.disabled).toBe(true);
|
|
354
|
-
expect(state!.definition === null || state!.definition.closing).toBe(true);
|
|
355
|
-
});
|
|
356
|
-
|
|
357
|
-
it('isolates same-name time controls and acknowledges invalid period events without changes', () => {
|
|
358
|
-
const first = vi.fn();
|
|
359
|
-
const second = vi.fn();
|
|
360
|
-
let actions: ReturnType<typeof usePopOverActions>;
|
|
361
|
-
let event: ReturnType<typeof usePopOverEvent>;
|
|
362
|
-
/* Give both controls the same field name while retaining independently scoped popover IDs. */
|
|
363
|
-
const Harness = () => {
|
|
364
|
-
actions = usePopOverActions();
|
|
365
|
-
event = usePopOverEvent();
|
|
366
|
-
return (
|
|
367
|
-
<>
|
|
368
|
-
<InputTimeFromDate
|
|
369
|
-
name="date"
|
|
370
|
-
value="2026-01-01T20:30:00Z"
|
|
371
|
-
timeZone="America/Toronto"
|
|
372
|
-
onChange={first}
|
|
373
|
-
/>
|
|
374
|
-
<InputTimeFromDate
|
|
375
|
-
name="date"
|
|
376
|
-
value="2026-01-01T20:30:00Z"
|
|
377
|
-
timeZone="America/Toronto"
|
|
378
|
-
onChange={second}
|
|
379
|
-
/>
|
|
380
|
-
</>
|
|
381
|
-
);
|
|
382
|
-
};
|
|
383
|
-
render(<Harness />);
|
|
384
|
-
const trigger = container.querySelector<HTMLElement>('[role="button"]')!;
|
|
385
|
-
act(() => trigger.click());
|
|
386
|
-
act(() => actions!.publishPopOverEvent({ action: 'ITEM', value: 'AM' }));
|
|
387
|
-
expect(first.mock.calls[0][2].toISOString()).toBe('2026-01-01T08:30:00.000Z');
|
|
388
|
-
expect(second).not.toHaveBeenCalled();
|
|
389
|
-
expect(event!).toBeNull();
|
|
390
|
-
act(() => trigger.click());
|
|
391
|
-
act(() => actions!.publishPopOverEvent({ action: 'ITEM', value: 'not-a-period' }));
|
|
392
|
-
expect(first).toHaveBeenCalledOnce();
|
|
393
|
-
expect(event!).toBeNull();
|
|
394
|
-
});
|
|
395
|
-
});
|