@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
package/tests/controls.test.tsx
DELETED
|
@@ -1,261 +0,0 @@
|
|
|
1
|
-
import { usePopOverActions, usePopOverDefinition, usePopOverEvent } from '@jsb188/react/states';
|
|
2
|
-
import { createStore, Provider } from 'jotai';
|
|
3
|
-
import { act, useState, type ReactNode } from 'react';
|
|
4
|
-
import type { Root } from 'react-dom/client';
|
|
5
|
-
import { JSDOM } from 'jsdom';
|
|
6
|
-
import { afterEach, beforeEach, expect, it, vi } from 'vitest';
|
|
7
|
-
import { composeFormInput } from '../src/modules/composeFormInput';
|
|
8
|
-
import { Input } from '../src/modules/Input';
|
|
9
|
-
import { PasswordInput } from '../src/modules/PasswordInput';
|
|
10
|
-
import { Select } from '../src/modules/Select';
|
|
11
|
-
import { Textarea } from '../src/modules/Textarea';
|
|
12
|
-
import { InputTimeFromDate } from '../src/modules/TimeInput';
|
|
13
|
-
|
|
14
|
-
let root: Root;
|
|
15
|
-
let dom: JSDOM;
|
|
16
|
-
let container: HTMLDivElement;
|
|
17
|
-
let resizeCallback: ResizeObserverCallback;
|
|
18
|
-
const disconnect = vi.fn();
|
|
19
|
-
|
|
20
|
-
beforeEach(async () => {
|
|
21
|
-
dom = new JSDOM('<!doctype html><html><body></body></html>');
|
|
22
|
-
vi.stubGlobal('window', dom.window);
|
|
23
|
-
vi.stubGlobal('document', dom.window.document);
|
|
24
|
-
vi.stubGlobal('HTMLInputElement', dom.window.HTMLInputElement);
|
|
25
|
-
vi.stubGlobal('Event', dom.window.Event);
|
|
26
|
-
const { createRoot } = await import('react-dom/client');
|
|
27
|
-
vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true);
|
|
28
|
-
vi.stubGlobal(
|
|
29
|
-
'ResizeObserver',
|
|
30
|
-
class {
|
|
31
|
-
/*
|
|
32
|
-
* Keep the resize notification available so textarea measurement can be tested without browser layout.
|
|
33
|
-
*/
|
|
34
|
-
constructor(callback: ResizeObserverCallback) {
|
|
35
|
-
resizeCallback = callback;
|
|
36
|
-
}
|
|
37
|
-
observe = vi.fn();
|
|
38
|
-
disconnect = disconnect;
|
|
39
|
-
},
|
|
40
|
-
);
|
|
41
|
-
container = document.createElement('div');
|
|
42
|
-
document.body.append(container);
|
|
43
|
-
root = createRoot(container);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
afterEach(() => {
|
|
47
|
-
act(() => root.unmount());
|
|
48
|
-
container.remove();
|
|
49
|
-
vi.clearAllMocks();
|
|
50
|
-
dom.window.close();
|
|
51
|
-
vi.unstubAllGlobals();
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
/*
|
|
55
|
-
* Mount controls in an isolated instance of the existing popover state store.
|
|
56
|
-
*/
|
|
57
|
-
function render(children: ReactNode) {
|
|
58
|
-
act(() => root.render(<Provider store={createStore()}>{children}</Provider>));
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
it('routes select events to their owning control and acknowledges handled selections', () => {
|
|
62
|
-
let state: {
|
|
63
|
-
actions: ReturnType<typeof usePopOverActions>;
|
|
64
|
-
definition: ReturnType<typeof usePopOverDefinition>;
|
|
65
|
-
event: ReturnType<typeof usePopOverEvent>;
|
|
66
|
-
};
|
|
67
|
-
const onFirstChange = vi.fn();
|
|
68
|
-
const onSecondChange = vi.fn();
|
|
69
|
-
/*
|
|
70
|
-
* Observe the real bundled popover store while both select modules are mounted.
|
|
71
|
-
*/
|
|
72
|
-
const Harness = () => {
|
|
73
|
-
state = { actions: usePopOverActions(), definition: usePopOverDefinition(), event: usePopOverEvent() };
|
|
74
|
-
return (
|
|
75
|
-
<>
|
|
76
|
-
<Select
|
|
77
|
-
name='first'
|
|
78
|
-
value='a'
|
|
79
|
-
options={[
|
|
80
|
-
{ value: 'a', text: 'First' },
|
|
81
|
-
{ value: 'b', text: 'Second' },
|
|
82
|
-
]}
|
|
83
|
-
onChange={onFirstChange}
|
|
84
|
-
/>
|
|
85
|
-
<Select
|
|
86
|
-
name='second'
|
|
87
|
-
options={[{ value: 'c', text: 'Other' }]}
|
|
88
|
-
onChange={onSecondChange}
|
|
89
|
-
/>
|
|
90
|
-
</>
|
|
91
|
-
);
|
|
92
|
-
};
|
|
93
|
-
render(<Harness />);
|
|
94
|
-
act(() => container.querySelector<HTMLButtonElement>('#first-form')!.click());
|
|
95
|
-
expect(state!.definition?.name).toBe('PO_LIST');
|
|
96
|
-
const variables = state!.definition!.variables as { options: { name: string }[] };
|
|
97
|
-
const optionName = variables.options[0].name;
|
|
98
|
-
act(() => state!.actions.publishPopOverEvent({ action: 'ITEM', name: 'unrelated', value: 'b' }));
|
|
99
|
-
expect(onFirstChange).not.toHaveBeenCalled();
|
|
100
|
-
act(() => state!.actions.publishPopOverEvent({ action: 'ITEM', name: optionName, value: 'b' }));
|
|
101
|
-
expect(onFirstChange).toHaveBeenCalledExactlyOnceWith('b', 'first', { value: 'b', text: 'Second' });
|
|
102
|
-
expect(onSecondChange).not.toHaveBeenCalled();
|
|
103
|
-
expect(state!.event).toBeNull();
|
|
104
|
-
expect(state!.definition?.closing || !state!.definition).toBe(true);
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it.each(['disabled', 'locked', 'readonly', 'removed', 'option-disabled'] as const)(
|
|
108
|
-
'ignores an already-open selection after the control becomes %s',
|
|
109
|
-
(restriction) => {
|
|
110
|
-
let actions!: ReturnType<typeof usePopOverActions>;
|
|
111
|
-
let definition!: ReturnType<typeof usePopOverDefinition>;
|
|
112
|
-
let restrict!: () => void;
|
|
113
|
-
const onChange = vi.fn();
|
|
114
|
-
/* Refresh control permissions or options while its original popover is still mounted. */
|
|
115
|
-
const Harness = () => {
|
|
116
|
-
const [restricted, setRestricted] = useState(false);
|
|
117
|
-
restrict = () => setRestricted(true);
|
|
118
|
-
actions = usePopOverActions();
|
|
119
|
-
definition = usePopOverDefinition();
|
|
120
|
-
return (
|
|
121
|
-
<Select
|
|
122
|
-
name='choice'
|
|
123
|
-
disabled={restricted && restriction === 'disabled'}
|
|
124
|
-
locked={restricted && restriction === 'locked'}
|
|
125
|
-
editable={!(restricted && restriction === 'readonly')}
|
|
126
|
-
options={
|
|
127
|
-
restricted && restriction === 'removed'
|
|
128
|
-
? []
|
|
129
|
-
: [{ value: 'a', text: 'First', disabled: restricted && restriction === 'option-disabled' }]
|
|
130
|
-
}
|
|
131
|
-
onChange={onChange}
|
|
132
|
-
/>
|
|
133
|
-
);
|
|
134
|
-
};
|
|
135
|
-
render(<Harness />);
|
|
136
|
-
act(() => container.querySelector<HTMLButtonElement>('#choice-form')!.click());
|
|
137
|
-
const variables = definition!.variables as { options: { name: string }[] };
|
|
138
|
-
act(restrict);
|
|
139
|
-
act(() => actions.publishPopOverEvent({ action: 'ITEM', name: variables.options[0].name, value: 'a' }));
|
|
140
|
-
expect(onChange).not.toHaveBeenCalled();
|
|
141
|
-
},
|
|
142
|
-
);
|
|
143
|
-
|
|
144
|
-
it('updates nested standalone form fields without mutating the saved value snapshot', () => {
|
|
145
|
-
const initial = Object.freeze({ contact: Object.freeze({ email: 'before@example.com' }), other: Object.freeze({ value: 1 }) });
|
|
146
|
-
let current = initial;
|
|
147
|
-
const BoundInput = composeFormInput(Input);
|
|
148
|
-
/*
|
|
149
|
-
* Bind the compatibility adapter to React state and retain both snapshots for comparison.
|
|
150
|
-
*/
|
|
151
|
-
const Harness = () => {
|
|
152
|
-
const [values, setValues] = useState(initial);
|
|
153
|
-
current = values;
|
|
154
|
-
return (
|
|
155
|
-
<BoundInput
|
|
156
|
-
name='contact.email'
|
|
157
|
-
formValues={values}
|
|
158
|
-
setFormValues={setValues}
|
|
159
|
-
/>
|
|
160
|
-
);
|
|
161
|
-
};
|
|
162
|
-
render(<Harness />);
|
|
163
|
-
const input = container.querySelector('input')!;
|
|
164
|
-
act(() => {
|
|
165
|
-
Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')!.set!.call(input, 'after@example.com');
|
|
166
|
-
input.dispatchEvent(new Event('input', { bubbles: true }));
|
|
167
|
-
});
|
|
168
|
-
expect(current.contact.email).toBe('after@example.com');
|
|
169
|
-
expect(initial.contact.email).toBe('before@example.com');
|
|
170
|
-
expect(current.contact).not.toBe(initial.contact);
|
|
171
|
-
expect(current.other).toBe(initial.other);
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
it('toggles controlled password visibility without changing its value or submitting its form', () => {
|
|
175
|
-
const onSubmit = vi.fn();
|
|
176
|
-
render(
|
|
177
|
-
<form onSubmit={onSubmit}>
|
|
178
|
-
<PasswordInput
|
|
179
|
-
name='password'
|
|
180
|
-
value='secret'
|
|
181
|
-
onChange={() => undefined}
|
|
182
|
-
/>
|
|
183
|
-
</form>,
|
|
184
|
-
);
|
|
185
|
-
const input = container.querySelector('input')!;
|
|
186
|
-
expect(input.type).toBe('password');
|
|
187
|
-
act(() => container.querySelector<HTMLButtonElement>('button')!.click());
|
|
188
|
-
expect(input.type).toBe('text');
|
|
189
|
-
expect(input.value).toBe('secret');
|
|
190
|
-
expect(onSubmit).not.toHaveBeenCalled();
|
|
191
|
-
act(() => container.querySelector<HTMLButtonElement>('button')!.click());
|
|
192
|
-
expect(input.type).toBe('password');
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
it('sizes textarea text with existing classes while keeping pasted HTML inert', () => {
|
|
196
|
-
const value = '<img src=x onerror=alert(1)>\n Indented second line';
|
|
197
|
-
render(
|
|
198
|
-
<Textarea
|
|
199
|
-
name='note'
|
|
200
|
-
value={value}
|
|
201
|
-
onChange={() => undefined}
|
|
202
|
-
/>,
|
|
203
|
-
);
|
|
204
|
-
const virtualizer = container.querySelector<HTMLDivElement>('.tx_virtualizer')!;
|
|
205
|
-
const textarea = container.querySelector('textarea')!;
|
|
206
|
-
expect(textarea.value).toBe(value);
|
|
207
|
-
expect(virtualizer.querySelector('img')).toBeNull();
|
|
208
|
-
expect(virtualizer.textContent).toBe(value);
|
|
209
|
-
expect(virtualizer.getAttribute('aria-hidden')).toBe('true');
|
|
210
|
-
Object.defineProperty(virtualizer, 'offsetHeight', { configurable: true, value: 230 });
|
|
211
|
-
act(() => resizeCallback([], {} as ResizeObserver));
|
|
212
|
-
expect(textarea.style.height).toBe('230px');
|
|
213
|
-
expect(textarea.style.minHeight).toBe('150px');
|
|
214
|
-
expect(textarea.className).toBe('form_input bl of rel w_f r_sm');
|
|
215
|
-
Object.defineProperty(virtualizer, 'offsetHeight', { configurable: true, value: 252 });
|
|
216
|
-
render(
|
|
217
|
-
<Textarea
|
|
218
|
-
name='note'
|
|
219
|
-
value={`${value}\n`}
|
|
220
|
-
onChange={() => undefined}
|
|
221
|
-
/>,
|
|
222
|
-
);
|
|
223
|
-
// Text changes must resize without waiting for a ResizeObserver notification.
|
|
224
|
-
expect(textarea.style.height).toBe('252px');
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
it('resets controlled time values from the owner and keeps invalid edits out of the saved date', () => {
|
|
228
|
-
const onChange = vi.fn();
|
|
229
|
-
const onBlur = vi.fn();
|
|
230
|
-
render(
|
|
231
|
-
<InputTimeFromDate
|
|
232
|
-
name='date'
|
|
233
|
-
value={new Date(2026, 0, 1, 3, 20)}
|
|
234
|
-
onChange={onChange}
|
|
235
|
-
onBlur={onBlur}
|
|
236
|
-
/>,
|
|
237
|
-
);
|
|
238
|
-
const hours = container.querySelector<HTMLInputElement>('[name="date_hours"]')!;
|
|
239
|
-
const minutes = container.querySelector<HTMLInputElement>('[name="date_minutes"]')!;
|
|
240
|
-
expect(hours.value).toBe('3');
|
|
241
|
-
expect(minutes.value).toBe('20');
|
|
242
|
-
act(() => {
|
|
243
|
-
Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')!.set!.call(hours, '13');
|
|
244
|
-
hours.dispatchEvent(new Event('input', { bubbles: true }));
|
|
245
|
-
});
|
|
246
|
-
expect(onChange).not.toHaveBeenCalled();
|
|
247
|
-
expect(hours.classList.contains('error')).toBe(true);
|
|
248
|
-
act(() => hours.dispatchEvent(new Event('focusout', { bubbles: true })));
|
|
249
|
-
expect(hours.value).toBe('3');
|
|
250
|
-
expect(onBlur).toHaveBeenCalledOnce();
|
|
251
|
-
render(
|
|
252
|
-
<InputTimeFromDate
|
|
253
|
-
name='date'
|
|
254
|
-
value={new Date(2026, 0, 1, 21, 9)}
|
|
255
|
-
onChange={onChange}
|
|
256
|
-
/>,
|
|
257
|
-
);
|
|
258
|
-
expect(hours.value).toBe('9');
|
|
259
|
-
expect(minutes.value).toBe('9');
|
|
260
|
-
expect(container.querySelector('[role="button"]')?.textContent).toBe('PM');
|
|
261
|
-
});
|
|
@@ -1,303 +0,0 @@
|
|
|
1
|
-
import i18n, { i18nTranslations } from '@jsb188/app/i18n/index.ts';
|
|
2
|
-
import { JSDOM } from 'jsdom';
|
|
3
|
-
import { act } from 'react';
|
|
4
|
-
import type { Root } from 'react-dom/client';
|
|
5
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
6
|
-
import { z } from 'zod';
|
|
7
|
-
import { defineForm } from '../src/libs/defineForm';
|
|
8
|
-
import SchemaForm from '../src/libs/SchemaForm';
|
|
9
|
-
import type { FormController, SubmitResult } from '../src/libs/SchemaForm.types';
|
|
10
|
-
import { useSchemaForm } from '../src/libs/useSchemaForm';
|
|
11
|
-
|
|
12
|
-
let dom: JSDOM;
|
|
13
|
-
let root: Root;
|
|
14
|
-
let container: HTMLDivElement;
|
|
15
|
-
let nativeSubmit: ReturnType<typeof vi.fn>;
|
|
16
|
-
|
|
17
|
-
beforeEach(async () => {
|
|
18
|
-
dom = new JSDOM('<!doctype html><html><body></body></html>', { url: 'https://example.com/auth/signin' });
|
|
19
|
-
vi.stubGlobal('window', dom.window);
|
|
20
|
-
vi.stubGlobal('document', dom.window.document);
|
|
21
|
-
vi.stubGlobal('HTMLInputElement', dom.window.HTMLInputElement);
|
|
22
|
-
vi.stubGlobal('HTMLFormElement', dom.window.HTMLFormElement);
|
|
23
|
-
vi.stubGlobal('Event', dom.window.Event);
|
|
24
|
-
vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true);
|
|
25
|
-
const { createRoot } = await import('react-dom/client');
|
|
26
|
-
container = document.createElement('div');
|
|
27
|
-
document.body.append(container);
|
|
28
|
-
root = createRoot(container);
|
|
29
|
-
nativeSubmit = vi.fn();
|
|
30
|
-
vi.spyOn(dom.window.HTMLFormElement.prototype, 'submit').mockImplementation(nativeSubmit);
|
|
31
|
-
i18n.extend(i18nTranslations);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
afterEach(() => {
|
|
35
|
-
act(() => root.unmount());
|
|
36
|
-
container.remove();
|
|
37
|
-
dom.window.close();
|
|
38
|
-
vi.restoreAllMocks();
|
|
39
|
-
vi.unstubAllGlobals();
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
/* Define the same identifier/password shape and conditional password presentation used by sign-in. */
|
|
43
|
-
function signInDefinition() {
|
|
44
|
-
return defineForm({
|
|
45
|
-
schema: z.object({ identifier: z.string().min(1, 'Enter an identifier'), password: z.string() }),
|
|
46
|
-
defaults: { identifier: '', password: '' },
|
|
47
|
-
fields: {
|
|
48
|
-
identifier: { kind: 'input', label: 'Identifier', suppressHydrationWarning: true },
|
|
49
|
-
password: {
|
|
50
|
-
kind: 'password',
|
|
51
|
-
label: 'Password',
|
|
52
|
-
visibleWhen: ({ context }) => !context?.hidePassword,
|
|
53
|
-
},
|
|
54
|
-
},
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/* Deliver a browser input event without React's controlled-input value tracker swallowing the change. */
|
|
59
|
-
function typeInto(input: HTMLInputElement, value: string) {
|
|
60
|
-
Object.getOwnPropertyDescriptor(dom.window.HTMLInputElement.prototype, 'value')!.set!.call(input, value);
|
|
61
|
-
input.dispatchEvent(new dom.window.Event('input', { bubbles: true }));
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
describe('schema form browser submission and presentation', () => {
|
|
65
|
-
it('clears an incomplete time draft when resetting to the same saved date', () => {
|
|
66
|
-
const definition = defineForm({
|
|
67
|
-
schema: z.object({ date: z.date() }),
|
|
68
|
-
defaults: { date: new Date('2026-01-01T17:00:00Z') },
|
|
69
|
-
fields: { date: { kind: 'time', timeZone: 'UTC' } },
|
|
70
|
-
});
|
|
71
|
-
let form!: FormController<{ date: Date }>;
|
|
72
|
-
/* Expose explicit controller reset while rendering its real time field. */
|
|
73
|
-
const Harness = () => {
|
|
74
|
-
form = useSchemaForm(definition);
|
|
75
|
-
return <SchemaForm form={form} />;
|
|
76
|
-
};
|
|
77
|
-
act(() => root.render(<Harness />));
|
|
78
|
-
const hours = container.querySelector<HTMLInputElement>('[name="date_hours"]')!;
|
|
79
|
-
expect(hours.value).toBe('5');
|
|
80
|
-
act(() => typeInto(hours, ''));
|
|
81
|
-
expect(hours.value).toBe('');
|
|
82
|
-
expect(form.isDirty).toBe(false);
|
|
83
|
-
act(() => form.reset());
|
|
84
|
-
expect(hours.value).toBe('5');
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
it('allows a reset form to submit while its previous request is still finishing', async () => {
|
|
88
|
-
const pending: ((value: SubmitResult<{ name: string }>) => void)[] = [];
|
|
89
|
-
const onSubmit = vi.fn(() => new Promise<SubmitResult<{ name: string }>>((resolve) => pending.push(resolve)));
|
|
90
|
-
const definition = defineForm({
|
|
91
|
-
schema: z.object({ name: z.string() }),
|
|
92
|
-
defaults: { name: 'First' },
|
|
93
|
-
fields: { name: { kind: 'input' } },
|
|
94
|
-
});
|
|
95
|
-
let form!: FormController<{ name: string }>;
|
|
96
|
-
/* Keep the renderer mounted across a deliberate controller reset. */
|
|
97
|
-
const Harness = () => {
|
|
98
|
-
form = useSchemaForm(definition, { onSubmit });
|
|
99
|
-
return <SchemaForm form={form} />;
|
|
100
|
-
};
|
|
101
|
-
act(() => root.render(<Harness />));
|
|
102
|
-
await act(async () => container.querySelector<HTMLButtonElement>('button')!.click());
|
|
103
|
-
expect(onSubmit).toHaveBeenCalledOnce();
|
|
104
|
-
act(() => form.reset({ name: 'Second' }));
|
|
105
|
-
await act(async () => container.querySelector<HTMLButtonElement>('button')!.click());
|
|
106
|
-
expect(onSubmit).toHaveBeenCalledTimes(2);
|
|
107
|
-
await act(async () => pending[0]({ status: 'success' }));
|
|
108
|
-
expect(form.isSubmitting).toBe(true);
|
|
109
|
-
expect(form.values.name).toBe('Second');
|
|
110
|
-
await act(async () => pending[1]({ status: 'success' }));
|
|
111
|
-
expect(form.isSubmitting).toBe(false);
|
|
112
|
-
expect(form.isDirty).toBe(false);
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
it('preserves child, field, footer, and submit-button order while toggling password visibility', async () => {
|
|
116
|
-
const definition = signInDefinition();
|
|
117
|
-
const onSubmit = vi.fn(() => ({ status: 'continue' as const }));
|
|
118
|
-
/* Update only discovery context without remounting the form or discarding the entered identifier. */
|
|
119
|
-
const render = (hidePassword: boolean) =>
|
|
120
|
-
act(() =>
|
|
121
|
-
root.render(
|
|
122
|
-
<SchemaForm
|
|
123
|
-
definition={definition}
|
|
124
|
-
context={{ hidePassword }}
|
|
125
|
-
onSubmit={onSubmit}
|
|
126
|
-
className="py_df"
|
|
127
|
-
FooterComponent={<p data-testid="footer">Footer</p>}
|
|
128
|
-
>
|
|
129
|
-
<p data-testid="message">Existing error message</p>
|
|
130
|
-
</SchemaForm>,
|
|
131
|
-
),
|
|
132
|
-
);
|
|
133
|
-
render(true);
|
|
134
|
-
const identifier = container.querySelector<HTMLInputElement>('input[name="identifier"]')!;
|
|
135
|
-
expect(container.querySelector('input[name="password"]')).toBeNull();
|
|
136
|
-
await act(async () => typeInto(identifier, 'member@example.com'));
|
|
137
|
-
render(false);
|
|
138
|
-
const password = container.querySelector<HTMLInputElement>('input[name="password"]')!;
|
|
139
|
-
const form = container.querySelector('form')!;
|
|
140
|
-
const message = form.querySelector('[data-testid="message"]')!;
|
|
141
|
-
const footer = form.querySelector('[data-testid="footer"]')!;
|
|
142
|
-
const submitWrapper = form.querySelector('.pt_df.fs.z2')!;
|
|
143
|
-
const follows = dom.window.Node.DOCUMENT_POSITION_FOLLOWING;
|
|
144
|
-
|
|
145
|
-
expect(form.className).toBe('schema_form py_df');
|
|
146
|
-
expect(identifier.value).toBe('member@example.com');
|
|
147
|
-
expect(message.compareDocumentPosition(identifier) & follows).toBeTruthy();
|
|
148
|
-
expect(identifier.compareDocumentPosition(password) & follows).toBeTruthy();
|
|
149
|
-
expect(password.compareDocumentPosition(footer) & follows).toBeTruthy();
|
|
150
|
-
expect(footer.compareDocumentPosition(submitWrapper) & follows).toBeTruthy();
|
|
151
|
-
expect(submitWrapper.querySelector('button')?.classList.contains('shadow')).toBe(true);
|
|
152
|
-
|
|
153
|
-
await act(async () => typeInto(password, 'ExistingPassword1!'));
|
|
154
|
-
const passwordToggle = Array.from(form.querySelectorAll('button')).find((button) =>
|
|
155
|
-
button.textContent?.includes(i18n.t('account.show_password')),
|
|
156
|
-
)!;
|
|
157
|
-
act(() => passwordToggle.click());
|
|
158
|
-
expect(password.type).toBe('text');
|
|
159
|
-
expect(password.value).toBe('ExistingPassword1!');
|
|
160
|
-
expect(onSubmit).not.toHaveBeenCalled();
|
|
161
|
-
expect(nativeSubmit).not.toHaveBeenCalled();
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
it('validates Enter submissions and keeps discovery and rejected requests from posting natively', async () => {
|
|
165
|
-
const onError = vi.fn();
|
|
166
|
-
const onSubmit = vi
|
|
167
|
-
.fn()
|
|
168
|
-
.mockReturnValueOnce({ status: 'continue' })
|
|
169
|
-
.mockReturnValueOnce({ status: 'error', error: { message: 'Server rejected' } });
|
|
170
|
-
act(() =>
|
|
171
|
-
root.render(
|
|
172
|
-
<SchemaForm
|
|
173
|
-
definition={signInDefinition()}
|
|
174
|
-
actionUrl="/auth/signin"
|
|
175
|
-
onSubmit={onSubmit}
|
|
176
|
-
onError={onError}
|
|
177
|
-
/>,
|
|
178
|
-
),
|
|
179
|
-
);
|
|
180
|
-
const form = container.querySelector('form')!;
|
|
181
|
-
|
|
182
|
-
await act(async () => {
|
|
183
|
-
form.dispatchEvent(new dom.window.Event('submit', { bubbles: true, cancelable: true }));
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
expect(onSubmit).not.toHaveBeenCalled();
|
|
187
|
-
expect(onError).toHaveBeenCalledWith(expect.objectContaining({ message: 'Enter an identifier', path: 'identifier' }));
|
|
188
|
-
expect(nativeSubmit).not.toHaveBeenCalled();
|
|
189
|
-
await act(async () => typeInto(form.querySelector<HTMLInputElement>('input[name="identifier"]')!, 'member@example.com'));
|
|
190
|
-
await act(async () => {
|
|
191
|
-
form.dispatchEvent(new dom.window.Event('submit', { bubbles: true, cancelable: true }));
|
|
192
|
-
});
|
|
193
|
-
expect(onSubmit).toHaveBeenCalledTimes(1);
|
|
194
|
-
expect(nativeSubmit).not.toHaveBeenCalled();
|
|
195
|
-
await act(async () => {
|
|
196
|
-
form.querySelector<HTMLButtonElement>('.pt_df.fs.z2 button')!.click();
|
|
197
|
-
});
|
|
198
|
-
expect(onSubmit).toHaveBeenCalledTimes(2);
|
|
199
|
-
expect(onError).toHaveBeenLastCalledWith({ message: 'Server rejected' });
|
|
200
|
-
expect(nativeSubmit).not.toHaveBeenCalled();
|
|
201
|
-
});
|
|
202
|
-
|
|
203
|
-
it('posts enabled credential fields and hidden metadata exactly once after async native authorization', async () => {
|
|
204
|
-
let resolve!: (result: SubmitResult<z.input<ReturnType<typeof signInDefinition>['schema']>>) => void;
|
|
205
|
-
const authorization = new Promise<SubmitResult<z.input<ReturnType<typeof signInDefinition>['schema']>>>((resolvePromise) => {
|
|
206
|
-
resolve = resolvePromise;
|
|
207
|
-
});
|
|
208
|
-
const onSubmit = vi.fn(() => authorization);
|
|
209
|
-
act(() =>
|
|
210
|
-
root.render(
|
|
211
|
-
<SchemaForm
|
|
212
|
-
definition={signInDefinition()}
|
|
213
|
-
initialValues={{ identifier: 'member@example.com', password: 'ExistingPassword1!' }}
|
|
214
|
-
actionUrl="/auth/signin?returnTo=%2Fapp"
|
|
215
|
-
method="post"
|
|
216
|
-
onSubmit={onSubmit}
|
|
217
|
-
>
|
|
218
|
-
<input
|
|
219
|
-
type="hidden"
|
|
220
|
-
name="managedSessionVersion"
|
|
221
|
-
value="1"
|
|
222
|
-
/>
|
|
223
|
-
</SchemaForm>,
|
|
224
|
-
),
|
|
225
|
-
);
|
|
226
|
-
const form = container.querySelector('form')!;
|
|
227
|
-
const submitButton = form.querySelector<HTMLButtonElement>('.pt_df.fs.z2 button')!;
|
|
228
|
-
act(() => {
|
|
229
|
-
submitButton.click();
|
|
230
|
-
form.dispatchEvent(new dom.window.Event('submit', { bubbles: true, cancelable: true }));
|
|
231
|
-
});
|
|
232
|
-
await act(async () => {});
|
|
233
|
-
|
|
234
|
-
expect(onSubmit).toHaveBeenCalledTimes(1);
|
|
235
|
-
expect(nativeSubmit).not.toHaveBeenCalled();
|
|
236
|
-
expect(form.querySelector<HTMLInputElement>('input[name="identifier"]')!.disabled).toBe(false);
|
|
237
|
-
expect(form.querySelector<HTMLInputElement>('input[name="password"]')!.disabled).toBe(false);
|
|
238
|
-
await act(async () => {
|
|
239
|
-
resolve({ status: 'native' });
|
|
240
|
-
await authorization;
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
expect(nativeSubmit).toHaveBeenCalledTimes(1);
|
|
244
|
-
expect(form.method).toBe('post');
|
|
245
|
-
expect(form.getAttribute('action')).toBe('/auth/signin?returnTo=%2Fapp');
|
|
246
|
-
expect(Object.fromEntries(new dom.window.FormData(form))).toEqual({
|
|
247
|
-
identifier: 'member@example.com',
|
|
248
|
-
password: 'ExistingPassword1!',
|
|
249
|
-
managedSessionVersion: '1',
|
|
250
|
-
});
|
|
251
|
-
});
|
|
252
|
-
it('retains the existing loading shadow and row label association', () => {
|
|
253
|
-
const definition = defineForm({
|
|
254
|
-
schema: z.object({ firstName: z.string(), lastName: z.string() }),
|
|
255
|
-
defaults: { firstName: 'First', lastName: 'Last' },
|
|
256
|
-
fields: { firstName: { kind: 'input' }, lastName: { kind: 'input' } },
|
|
257
|
-
layout: [{ kind: 'row', label: 'Name', fields: ['firstName', 'lastName'] }],
|
|
258
|
-
});
|
|
259
|
-
act(() =>
|
|
260
|
-
root.render(
|
|
261
|
-
<SchemaForm
|
|
262
|
-
definition={definition}
|
|
263
|
-
busy
|
|
264
|
-
/>,
|
|
265
|
-
),
|
|
266
|
-
);
|
|
267
|
-
const button = container.querySelector<HTMLButtonElement>('.pt_df.fs.z2 button')!;
|
|
268
|
-
const input = container.querySelector<HTMLInputElement>('input[name="firstName"]')!;
|
|
269
|
-
|
|
270
|
-
expect(button.classList.contains('loading')).toBe(true);
|
|
271
|
-
expect(button.classList.contains('shadow')).toBe(true);
|
|
272
|
-
expect(container.querySelector('label')!.htmlFor).toBe(input.id);
|
|
273
|
-
});
|
|
274
|
-
|
|
275
|
-
it('keeps credential actions clickable on readonly fields while respecting whole-form restrictions', () => {
|
|
276
|
-
const onClickButton = vi.fn();
|
|
277
|
-
const definition = defineForm({
|
|
278
|
-
schema: z.object({ email: z.string() }),
|
|
279
|
-
defaults: { email: 'member@example.com' },
|
|
280
|
-
fields: {
|
|
281
|
-
email: { kind: 'action', disabled: true, buttonText: 'Change', onClickButton },
|
|
282
|
-
},
|
|
283
|
-
});
|
|
284
|
-
act(() =>
|
|
285
|
-
root.render(
|
|
286
|
-
<SchemaForm
|
|
287
|
-
definition={definition}
|
|
288
|
-
notAllowed
|
|
289
|
-
/>,
|
|
290
|
-
),
|
|
291
|
-
);
|
|
292
|
-
expect(container.querySelector('form')!.classList.contains('form_not_allowed')).toBe(true);
|
|
293
|
-
expect(container.querySelector('input')!.disabled).toBe(true);
|
|
294
|
-
act(() => container.querySelector<HTMLDivElement>('.form_el_hv')!.click());
|
|
295
|
-
expect(onClickButton).not.toHaveBeenCalled();
|
|
296
|
-
|
|
297
|
-
act(() => root.render(<SchemaForm definition={definition} />));
|
|
298
|
-
expect(container.querySelector('input')!.disabled).toBe(true);
|
|
299
|
-
act(() => container.querySelector<HTMLDivElement>('.form_el_hv')!.click());
|
|
300
|
-
expect(onClickButton).toHaveBeenCalledTimes(1);
|
|
301
|
-
expect(nativeSubmit).not.toHaveBeenCalled();
|
|
302
|
-
});
|
|
303
|
-
});
|