|
| 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#29197 — an event with no stored end is a point in time, so no |
| 11 | + * view draws a resize handle on it. |
| 12 | + * |
| 13 | + * ── The rule ──────────────────────────────────────────────────────────────── |
| 14 | + * `CalendarView`'s `endEditable={false}` says the events have no stored end. |
| 15 | + * The week and day views then draw neither the bottom "Resize end" handle nor |
| 16 | + * the top "Resize start" one: against an unstored end, dragging the top edge |
| 17 | + * only rewrote the start and redrew the event an hour long, so it read as a |
| 18 | + * move. Moving the event stays a drag of its body. `ObjectCalendar` passes |
| 19 | + * `false` when the view declares no `endDateField`. |
| 20 | + * |
| 21 | + * The `calendar-view` renderer forwards a host's boolean `endEditable` the way |
| 22 | + * it forwards `slotMinutes`. It does not derive one from `endDateField`: that |
| 23 | + * key defaults to `end`, so every node names an end field. |
| 24 | + * |
| 25 | + * A view whose events have a stored end keeps both handles. |
| 26 | + */ |
| 27 | + |
| 28 | +import React from 'react'; |
| 29 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 30 | +import { cleanup, render, screen, within } from '@testing-library/react'; |
| 31 | +import { LocalizationProvider } from '@object-ui/i18n'; |
| 32 | +import { SchemaRenderer } from '@object-ui/react'; |
| 33 | +import { ObjectCalendar } from '../ObjectCalendar'; |
| 34 | +import { CalendarView, type CalendarViewEvent } from '../CalendarView'; |
| 35 | +// Module scope: the `calendar-view` registration the renderer cases render |
| 36 | +// through (ui/AGENTS.md, test discipline: never inside a hook). |
| 37 | +import '../index'; |
| 38 | + |
| 39 | +const TITLE = 'Build homepage'; |
| 40 | + |
| 41 | +const MONTH_HANDLE = 'Resize event end'; |
| 42 | +const GRID_HANDLE = { top: 'Resize start', bottom: 'Resize end' } as const; |
| 43 | + |
| 44 | +/** |
| 45 | + * Thursday January 15th, 2026: the day the day view opens on, inside the week |
| 46 | + * of the 11th to the 17th. The `ObjectCalendar` records sit on it. |
| 47 | + */ |
| 48 | +const CLOCK = '2026-01-15T12:00:00.000Z'; |
| 49 | + |
| 50 | +const OBJECT_SCHEMA = { |
| 51 | + name: 'task', |
| 52 | + fields: { |
| 53 | + name: { type: 'text' }, |
| 54 | + starts_at: { type: 'datetime' }, |
| 55 | + ends_at: { type: 'datetime' }, |
| 56 | + all_day: { type: 'boolean' }, |
| 57 | + }, |
| 58 | +}; |
| 59 | + |
| 60 | +/** |
| 61 | + * With no end field a record is all-day, drawn in the all-day lane with no |
| 62 | + * handle, unless the view declares `allDayField`: then a record flagged not |
| 63 | + * all-day is drawn timed, an hour long, in the time grid. |
| 64 | + */ |
| 65 | +const TIMED_NO_END = { startDateField: 'starts_at', allDayField: 'all_day' }; |
| 66 | + |
| 67 | +const timed: CalendarViewEvent = { |
| 68 | + id: 'e1', |
| 69 | + title: TITLE, |
| 70 | + start: new Date(2026, 0, 12, 9), |
| 71 | + end: new Date(2026, 0, 12, 11), |
| 72 | +}; |
| 73 | + |
| 74 | +afterEach(() => { |
| 75 | + cleanup(); |
| 76 | + vi.useRealTimers(); |
| 77 | +}); |
| 78 | + |
| 79 | +function handlesOn(piece: HTMLElement) { |
| 80 | + return { |
| 81 | + top: within(piece).queryByLabelText(GRID_HANDLE.top), |
| 82 | + bottom: within(piece).queryByLabelText(GRID_HANDLE.bottom), |
| 83 | + }; |
| 84 | +} |
| 85 | + |
| 86 | +function mountObjectCalendar( |
| 87 | + calendar: Record<string, unknown>, |
| 88 | + row: Record<string, unknown>, |
| 89 | + defaultView: 'week' | 'day', |
| 90 | +) { |
| 91 | + vi.useFakeTimers({ toFake: ['Date'] }); |
| 92 | + vi.setSystemTime(new Date(CLOCK)); |
| 93 | + const ds = { |
| 94 | + getObjectSchema: vi.fn(async () => OBJECT_SCHEMA), |
| 95 | + find: vi.fn(async () => ({ value: [{ id: 't1', name: TITLE, ...row }] })), |
| 96 | + update: vi.fn(async () => ({})), |
| 97 | + create: vi.fn(async () => ({ id: 't2' })), |
| 98 | + }; |
| 99 | + const schema = { |
| 100 | + type: 'object-calendar', |
| 101 | + objectName: 'task', |
| 102 | + defaultView, |
| 103 | + calendar: { titleField: 'name', ...calendar }, |
| 104 | + } as never; |
| 105 | + render( |
| 106 | + <LocalizationProvider value={{ locale: 'en-US' }}> |
| 107 | + <ObjectCalendar schema={schema} dataSource={ds as never} /> |
| 108 | + </LocalizationProvider>, |
| 109 | + ); |
| 110 | + return ds; |
| 111 | +} |
| 112 | + |
| 113 | +describe('CalendarView endEditable={false}: no resize handle in the time grid (objectstack#29197)', () => { |
| 114 | + for (const view of ['week', 'day'] as const) { |
| 115 | + it(`${view} view: neither the top nor the bottom handle is drawn`, () => { |
| 116 | + render( |
| 117 | + <CalendarView |
| 118 | + events={[timed]} |
| 119 | + view={view} |
| 120 | + currentDate={new Date(2026, 0, 12)} |
| 121 | + onEventDrop={vi.fn()} |
| 122 | + endEditable={false} |
| 123 | + />, |
| 124 | + ); |
| 125 | + const { top, bottom } = handlesOn(screen.getByTitle(TITLE)); |
| 126 | + expect(top).toBeNull(); |
| 127 | + expect(bottom).toBeNull(); |
| 128 | + }); |
| 129 | + |
| 130 | + it(`control: ${view} view, endEditable left at its default, draws both handles`, () => { |
| 131 | + render( |
| 132 | + <CalendarView events={[timed]} view={view} currentDate={new Date(2026, 0, 12)} onEventDrop={vi.fn()} />, |
| 133 | + ); |
| 134 | + const { top, bottom } = handlesOn(screen.getByTitle(TITLE)); |
| 135 | + expect(top).not.toBeNull(); |
| 136 | + expect(bottom).not.toBeNull(); |
| 137 | + }); |
| 138 | + } |
| 139 | +}); |
| 140 | + |
| 141 | +describe('ObjectCalendar with no endDateField: no resize handle in the time grid (objectstack#29197)', () => { |
| 142 | + for (const view of ['week', 'day'] as const) { |
| 143 | + it(`${view} view: a view with no end field draws no top handle`, async () => { |
| 144 | + mountObjectCalendar(TIMED_NO_END, { starts_at: '2026-01-15T09:00:00.000Z', all_day: false }, view); |
| 145 | + const { top, bottom } = handlesOn(await screen.findByTitle(TITLE)); |
| 146 | + expect(top).toBeNull(); |
| 147 | + expect(bottom).toBeNull(); |
| 148 | + }); |
| 149 | + |
| 150 | + it(`control: ${view} view, a view with an end field draws both handles`, async () => { |
| 151 | + mountObjectCalendar( |
| 152 | + { startDateField: 'starts_at', endDateField: 'ends_at' }, |
| 153 | + { starts_at: '2026-01-15T09:00:00.000Z', ends_at: '2026-01-15T11:00:00.000Z' }, |
| 154 | + view, |
| 155 | + ); |
| 156 | + const { top, bottom } = handlesOn(await screen.findByTitle(TITLE)); |
| 157 | + expect(top).not.toBeNull(); |
| 158 | + expect(bottom).not.toBeNull(); |
| 159 | + }); |
| 160 | + } |
| 161 | +}); |
| 162 | + |
| 163 | +describe('calendar-view renderer forwards a host endEditable (objectstack#29197)', () => { |
| 164 | + /** A timed record with an end, on Monday January 12th, 2026. */ |
| 165 | + const data = [ |
| 166 | + { |
| 167 | + id: 'r1', |
| 168 | + title: TITLE, |
| 169 | + start: new Date(2026, 0, 12, 9).toISOString(), |
| 170 | + end: new Date(2026, 0, 12, 11).toISOString(), |
| 171 | + allDay: false, |
| 172 | + }, |
| 173 | + ]; |
| 174 | + |
| 175 | + function mountNode(view: 'month' | 'week', host: Record<string, unknown>) { |
| 176 | + render( |
| 177 | + <SchemaRenderer |
| 178 | + schema={{ type: 'plugin-calendar:calendar-view', id: 'n', view, currentDate: '2026-01-12', data } as never} |
| 179 | + // A React host's own props: `SchemaRenderer` hands them to the widget. |
| 180 | + {...host} |
| 181 | + />, |
| 182 | + ); |
| 183 | + } |
| 184 | + |
| 185 | + it('week view: a host endEditable false draws neither time-grid handle', async () => { |
| 186 | + mountNode('week', { onEventDrop: vi.fn(), endEditable: false }); |
| 187 | + const { top, bottom } = handlesOn(await screen.findByTitle(TITLE)); |
| 188 | + expect(top).toBeNull(); |
| 189 | + expect(bottom).toBeNull(); |
| 190 | + }); |
| 191 | + |
| 192 | + it('month view: a host endEditable false draws no resize-end handle', async () => { |
| 193 | + mountNode('month', { onEventDrop: vi.fn(), endEditable: false }); |
| 194 | + await screen.findAllByTitle(TITLE); |
| 195 | + expect(screen.queryAllByLabelText(MONTH_HANDLE)).toHaveLength(0); |
| 196 | + }); |
| 197 | + |
| 198 | + it('control: with no endEditable from the host, the week view draws both handles', async () => { |
| 199 | + mountNode('week', { onEventDrop: vi.fn() }); |
| 200 | + const { top, bottom } = handlesOn(await screen.findByTitle(TITLE)); |
| 201 | + expect(top).not.toBeNull(); |
| 202 | + expect(bottom).not.toBeNull(); |
| 203 | + }); |
| 204 | + |
| 205 | + it('control: a non-boolean endEditable gets the absent-key answer, both handles drawn', async () => { |
| 206 | + mountNode('week', { onEventDrop: vi.fn(), endEditable: 'false' }); |
| 207 | + const { top, bottom } = handlesOn(await screen.findByTitle(TITLE)); |
| 208 | + expect(top).not.toBeNull(); |
| 209 | + expect(bottom).not.toBeNull(); |
| 210 | + }); |
| 211 | +}); |
0 commit comments