|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * objectstack#27483 — a date typed in a form the picker cannot read is a |
| 11 | + * validation error, not a silent drop. |
| 12 | + * |
| 13 | + * The picker never writes text that names no date (`13/45/2026`), so the form |
| 14 | + * value stayed what it was: the record saved with an empty Due Date and a |
| 15 | + * success toast. The picker now reports such text through |
| 16 | + * `onInvalidInputChange`, and the form renderer turns it into the field's |
| 17 | + * error, which refuses submit the way a missing required value does. |
| 18 | + * |
| 19 | + * Mounted through the real `form` renderer with the production registration of |
| 20 | + * the picker, because the defect lives in the hand-off between the two. |
| 21 | + */ |
| 22 | + |
| 23 | +import React from 'react'; |
| 24 | +import { describe, it, expect, beforeAll, beforeEach, afterEach, vi } from 'vitest'; |
| 25 | +import { render, screen, fireEvent, waitFor, cleanup } from '@testing-library/react'; |
| 26 | +import '@testing-library/jest-dom'; |
| 27 | +import { ComponentRegistry } from '@object-ui/core'; |
| 28 | +// Module scope: pulls in the form renderer's registration side effect. |
| 29 | +import '@object-ui/components'; |
| 30 | + |
| 31 | +import { withFieldCarrier } from '../withFieldCarrier'; |
| 32 | +import { DatePickerField, DateTimePickerField } from '../widgets/date-picker/DatePickerField'; |
| 33 | + |
| 34 | +beforeAll(() => { |
| 35 | + ComponentRegistry.register('date', withFieldCarrier(DatePickerField), { |
| 36 | + namespace: 'field', |
| 37 | + skipFallback: true, |
| 38 | + }); |
| 39 | + ComponentRegistry.register('datetime', withFieldCarrier(DateTimePickerField), { |
| 40 | + namespace: 'field', |
| 41 | + skipFallback: true, |
| 42 | + }); |
| 43 | +}, 60000); |
| 44 | + |
| 45 | +beforeEach(() => { |
| 46 | + if (!Element.prototype.scrollIntoView) { |
| 47 | + Element.prototype.scrollIntoView = () => {}; |
| 48 | + } |
| 49 | +}); |
| 50 | + |
| 51 | +afterEach(() => { |
| 52 | + cleanup(); |
| 53 | + vi.restoreAllMocks(); |
| 54 | +}); |
| 55 | + |
| 56 | +function renderForm(type: 'date' | 'datetime', onSubmit: (data: Record<string, unknown>) => void) { |
| 57 | + const Form = ComponentRegistry.get('form')!; |
| 58 | + return render( |
| 59 | + <Form |
| 60 | + schema={{ |
| 61 | + type: 'form', |
| 62 | + mode: 'create', |
| 63 | + showSubmit: true, |
| 64 | + showCancel: false, |
| 65 | + submitLabel: 'Create', |
| 66 | + defaultValues: { title: 'Walkthrough task', due: null }, |
| 67 | + fields: [ |
| 68 | + { name: 'title', label: 'Title', type: 'text' }, |
| 69 | + { name: 'due', label: 'Due Date', type: `field:${type}` }, |
| 70 | + ], |
| 71 | + onSubmit, |
| 72 | + }} |
| 73 | + />, |
| 74 | + ); |
| 75 | +} |
| 76 | + |
| 77 | +async function dueBox(): Promise<HTMLInputElement> { |
| 78 | + return waitFor(() => { |
| 79 | + const input = document.querySelector('[data-field="due"] [data-slot="date-picker"] input'); |
| 80 | + if (!input) throw new Error('date picker never rendered'); |
| 81 | + return input as HTMLInputElement; |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +const typeAndLeave = (input: HTMLInputElement, text: string) => { |
| 86 | + fireEvent.change(input, { target: { value: text } }); |
| 87 | + fireEvent.blur(input); |
| 88 | +}; |
| 89 | + |
| 90 | +const dueRow = () => document.querySelector('[data-field="due"]') as HTMLElement; |
| 91 | + |
| 92 | +/** Press Create and wait for the refusal to land: the form scrolls to its first invalid field. */ |
| 93 | +async function submitAndSeeRefusal() { |
| 94 | + const scroll = vi.spyOn(Element.prototype, 'scrollIntoView'); |
| 95 | + fireEvent.click(screen.getByRole('button', { name: /create/i })); |
| 96 | + await waitFor(() => expect(scroll).toHaveBeenCalled()); |
| 97 | +} |
| 98 | + |
| 99 | +describe('an unreadable typed date refuses submit (objectstack#27483)', () => { |
| 100 | + it('13/45/2026 marks the field invalid and sends nothing', async () => { |
| 101 | + const onSubmit = vi.fn(); |
| 102 | + renderForm('date', onSubmit); |
| 103 | + const input = await dueBox(); |
| 104 | + |
| 105 | + typeAndLeave(input, '13/45/2026'); |
| 106 | + // Shown on leaving the field, before any submit: the form's own message, |
| 107 | + // naming the format, and the picker's notice is not drawn beside it. |
| 108 | + await waitFor(() => expect(dueRow().textContent).toContain('MM/DD/YYYY')); |
| 109 | + expect(dueRow().querySelector('[data-testid="date-picker-notice"]')).toBeNull(); |
| 110 | + expect(input).toHaveAttribute('aria-invalid', 'true'); |
| 111 | + |
| 112 | + await submitAndSeeRefusal(); |
| 113 | + expect(onSubmit).not.toHaveBeenCalled(); |
| 114 | + expect(input.value).toBe('13/45/2026'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('correcting the text clears the error and the typed day is saved', async () => { |
| 118 | + const onSubmit = vi.fn(); |
| 119 | + renderForm('date', onSubmit); |
| 120 | + const input = await dueBox(); |
| 121 | + |
| 122 | + typeAndLeave(input, '13/45/2026'); |
| 123 | + await waitFor(() => expect(input).toHaveAttribute('aria-invalid', 'true')); |
| 124 | + |
| 125 | + typeAndLeave(input, '10/20/2026'); |
| 126 | + await waitFor(() => expect(dueRow().textContent).not.toContain('MM/DD/YYYY')); |
| 127 | + expect(input).toHaveAttribute('aria-invalid', 'false'); |
| 128 | + |
| 129 | + fireEvent.click(screen.getByRole('button', { name: /create/i })); |
| 130 | + await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1)); |
| 131 | + expect(onSubmit.mock.calls[0][0]).toMatchObject({ title: 'Walkthrough task', due: '2026-10-20' }); |
| 132 | + }); |
| 133 | + |
| 134 | + it('clearing the bad text withdraws the error and an empty optional date saves', async () => { |
| 135 | + const onSubmit = vi.fn(); |
| 136 | + renderForm('date', onSubmit); |
| 137 | + const input = await dueBox(); |
| 138 | + |
| 139 | + typeAndLeave(input, '13/45/2026'); |
| 140 | + await waitFor(() => expect(input).toHaveAttribute('aria-invalid', 'true')); |
| 141 | + |
| 142 | + fireEvent.click(screen.getByTestId('date-picker-clear')); |
| 143 | + await waitFor(() => expect(input).toHaveAttribute('aria-invalid', 'false')); |
| 144 | + |
| 145 | + fireEvent.click(screen.getByRole('button', { name: /create/i })); |
| 146 | + await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1)); |
| 147 | + }); |
| 148 | + |
| 149 | + it('a valid typed date saves untouched (control)', async () => { |
| 150 | + const onSubmit = vi.fn(); |
| 151 | + renderForm('date', onSubmit); |
| 152 | + typeAndLeave(await dueBox(), '10/20/2026'); |
| 153 | + |
| 154 | + fireEvent.click(screen.getByRole('button', { name: /create/i })); |
| 155 | + await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1)); |
| 156 | + expect(onSubmit.mock.calls[0][0]).toMatchObject({ due: '2026-10-20' }); |
| 157 | + }); |
| 158 | + |
| 159 | + it('datetime: an unreadable date and time refuses submit the same way', async () => { |
| 160 | + const onSubmit = vi.fn(); |
| 161 | + renderForm('datetime', onSubmit); |
| 162 | + const input = await dueBox(); |
| 163 | + |
| 164 | + typeAndLeave(input, '13/45/2026 25:99'); |
| 165 | + await waitFor(() => expect(input).toHaveAttribute('aria-invalid', 'true')); |
| 166 | + |
| 167 | + await submitAndSeeRefusal(); |
| 168 | + expect(onSubmit).not.toHaveBeenCalled(); |
| 169 | + }); |
| 170 | +}); |
0 commit comments