|
6 | 6 | * LICENSE file in the root directory of this source tree. |
7 | 7 | */ |
8 | 8 |
|
| 9 | +import * as React from 'react'; |
9 | 10 | import { describe, it, expect, vi } from 'vitest'; |
10 | 11 | import { render, screen, fireEvent, waitFor } from '@testing-library/react'; |
11 | 12 | import { InlineEditProvider, useInlineEdit } from '@object-ui/react'; |
| 13 | +import { NavigationOverlay, Popover, PopoverTrigger, PopoverContent } from '@object-ui/components'; |
12 | 14 | import { InlineEditSaveBar } from '../InlineEditSaveBar'; |
13 | 15 |
|
14 | 16 | /** |
@@ -290,3 +292,181 @@ describe('InlineEditSaveBar — the record changed elsewhere during the edit (ob |
290 | 292 | expect(notice()).toBeNull(); |
291 | 293 | }); |
292 | 294 | }); |
| 295 | + |
| 296 | +/** |
| 297 | + * objectstack#36472 — the record's side preview (the drawer `NavigationOverlay` |
| 298 | + * draws) holds an edit session the same way the full page does: the first Esc |
| 299 | + * ends the edit and the preview stays open; the next Esc closes the preview. A |
| 300 | + * layer over the record (a lookup or select popover) still closes first. Driven |
| 301 | + * through the real drawer and its Radix sheet: what is measured is who answers |
| 302 | + * the key. |
| 303 | + */ |
| 304 | +describe('InlineEditSaveBar — Esc inside the record\'s side preview (objectstack#36472)', () => { |
| 305 | + /** The preview, opened on one record; ✕/Esc closes it for real. */ |
| 306 | + function Preview({ |
| 307 | + update = vi.fn(), |
| 308 | + onBack, |
| 309 | + overList = true, |
| 310 | + children, |
| 311 | + }: { |
| 312 | + update?: ReturnType<typeof vi.fn>; |
| 313 | + onBack?: () => void; |
| 314 | + overList?: boolean; |
| 315 | + children?: React.ReactNode; |
| 316 | + }) { |
| 317 | + const [open, setOpen] = React.useState(true); |
| 318 | + return ( |
| 319 | + <NavigationOverlay |
| 320 | + isOpen={open} |
| 321 | + isOverlay |
| 322 | + selectedRecord={{ id: 'p1' }} |
| 323 | + mode="drawer" |
| 324 | + close={() => setOpen(false)} |
| 325 | + setIsOpen={setOpen} |
| 326 | + onBack={onBack} |
| 327 | + title="Project" |
| 328 | + width="460px" |
| 329 | + mainContent={overList ? <div>list</div> : undefined} |
| 330 | + > |
| 331 | + {() => ( |
| 332 | + <InlineEditProvider canEdit> |
| 333 | + <Harness /> |
| 334 | + <input aria-label="status-field" /> |
| 335 | + {children} |
| 336 | + <InlineEditSaveBar dataSource={{ update }} objectName="proj" recordId="p1" data={{ updated_at: 'v1' }} refresh={vi.fn()} /> |
| 337 | + </InlineEditProvider> |
| 338 | + )} |
| 339 | + </NavigationOverlay> |
| 340 | + ); |
| 341 | + } |
| 342 | + |
| 343 | + const pane = () => document.querySelector('[data-record-drawer-card]'); |
| 344 | + const field = () => screen.getByLabelText('status-field'); |
| 345 | + const editing = () => screen.queryByRole('button', { name: 'Save' }) !== null; |
| 346 | + const escape = (target: Element) => fireEvent.keyDown(target, { key: 'Escape', code: 'Escape', bubbles: true }); |
| 347 | + |
| 348 | + it('no change: the first Esc ends the edit and the preview stays; the next Esc closes the preview', () => { |
| 349 | + const update = vi.fn(); |
| 350 | + render(<Preview update={update} />); |
| 351 | + fireEvent.click(screen.getByText('edit-enter')); |
| 352 | + expect(editing()).toBe(true); |
| 353 | + expect(pane()?.contains(screen.getByRole('button', { name: 'Save' }))).toBe(true); |
| 354 | + |
| 355 | + escape(field()); |
| 356 | + expect(editing()).toBe(false); |
| 357 | + expect(pane()).not.toBeNull(); |
| 358 | + |
| 359 | + escape(field()); |
| 360 | + expect(pane()).toBeNull(); |
| 361 | + expect(update).not.toHaveBeenCalled(); |
| 362 | + }); |
| 363 | + |
| 364 | + it('a changed field: Esc discards the draft with no write, as on the full page, and the preview stays', () => { |
| 365 | + const update = vi.fn(); |
| 366 | + render(<Preview update={update} />); |
| 367 | + stageTwoFields(); |
| 368 | + // Pressed on the drawer itself, not in a field: the session still owns it. |
| 369 | + escape(pane()!); |
| 370 | + expect(editing()).toBe(false); |
| 371 | + expect(update).not.toHaveBeenCalled(); |
| 372 | + expect(pane()).not.toBeNull(); |
| 373 | + // The draft is gone: entering again starts clean, and Save writes nothing changed. |
| 374 | + fireEvent.click(screen.getByText('edit-enter')); |
| 375 | + fireEvent.click(screen.getByRole('button', { name: 'Save' })); |
| 376 | + expect(update).not.toHaveBeenCalledWith('proj', 'p1', expect.objectContaining({ status: 'active' }), expect.anything()); |
| 377 | + }); |
| 378 | + |
| 379 | + it('the modal preview with no list behind it behaves the same', () => { |
| 380 | + render(<Preview overList={false} />); |
| 381 | + fireEvent.click(screen.getByText('edit-enter')); |
| 382 | + escape(field()); |
| 383 | + expect(editing()).toBe(false); |
| 384 | + expect(pane()).not.toBeNull(); |
| 385 | + escape(field()); |
| 386 | + expect(pane()).toBeNull(); |
| 387 | + }); |
| 388 | + |
| 389 | + it('a level below the preview\'s root: Esc ends the edit and does not go back; the next Esc goes back', () => { |
| 390 | + const onBack = vi.fn(); |
| 391 | + render(<Preview onBack={onBack} />); |
| 392 | + fireEvent.click(screen.getByText('edit-enter')); |
| 393 | + escape(pane()!); |
| 394 | + expect(editing()).toBe(false); |
| 395 | + expect(onBack).not.toHaveBeenCalled(); |
| 396 | + escape(pane()!); |
| 397 | + expect(onBack).toHaveBeenCalledTimes(1); |
| 398 | + }); |
| 399 | + |
| 400 | + it('a popover open over the record closes on Esc alone: the edit and the preview stay', () => { |
| 401 | + render( |
| 402 | + <Preview> |
| 403 | + <Popover defaultOpen> |
| 404 | + <PopoverTrigger>lookup</PopoverTrigger> |
| 405 | + <PopoverContent> |
| 406 | + <input aria-label="lookup-search" /> |
| 407 | + </PopoverContent> |
| 408 | + </Popover> |
| 409 | + </Preview>, |
| 410 | + ); |
| 411 | + fireEvent.click(screen.getByText('edit-enter')); |
| 412 | + const search = screen.getByLabelText('lookup-search'); |
| 413 | + expect(pane()?.contains(search)).toBe(false); // portaled over the preview |
| 414 | + |
| 415 | + escape(search); |
| 416 | + expect(screen.queryByLabelText('lookup-search')).toBeNull(); |
| 417 | + expect(editing()).toBe(true); |
| 418 | + expect(pane()).not.toBeNull(); |
| 419 | + |
| 420 | + escape(field()); |
| 421 | + expect(editing()).toBe(false); |
| 422 | + expect(pane()).not.toBeNull(); |
| 423 | + }); |
| 424 | + |
| 425 | + it('a layer open when Esc was pressed owns it, even when it is gone by the time the session hears the key', () => { |
| 426 | + render(<Preview />); |
| 427 | + fireEvent.click(screen.getByText('edit-enter')); |
| 428 | + // A floating layer that closes on the key before it bubbles: its node is |
| 429 | + // removed in the capture phase, as a layer whose close the browser flushes |
| 430 | + // between listeners is. |
| 431 | + const popper = document.createElement('div'); |
| 432 | + popper.setAttribute('data-radix-popper-content-wrapper', ''); |
| 433 | + document.body.appendChild(popper); |
| 434 | + const closeOnEsc = (e: KeyboardEvent) => { |
| 435 | + if (e.key !== 'Escape') return; |
| 436 | + e.preventDefault(); |
| 437 | + popper.remove(); |
| 438 | + }; |
| 439 | + document.addEventListener('keydown', closeOnEsc, true); |
| 440 | + try { |
| 441 | + escape(field()); |
| 442 | + expect(document.body.contains(popper)).toBe(false); |
| 443 | + expect(editing()).toBe(true); |
| 444 | + expect(pane()).not.toBeNull(); |
| 445 | + } finally { |
| 446 | + document.removeEventListener('keydown', closeOnEsc, true); |
| 447 | + } |
| 448 | + escape(field()); |
| 449 | + expect(editing()).toBe(false); |
| 450 | + }); |
| 451 | + |
| 452 | + it('CONTROL: not editing, Esc closes the preview as before', () => { |
| 453 | + render(<Preview />); |
| 454 | + escape(field()); |
| 455 | + expect(pane()).toBeNull(); |
| 456 | + }); |
| 457 | + |
| 458 | + it('the full page is unchanged: a key a field already handled is not the session\'s', () => { |
| 459 | + render( |
| 460 | + <InlineEditProvider canEdit> |
| 461 | + <Harness /> |
| 462 | + <input aria-label="own-esc" onKeyDown={(e) => e.key === 'Escape' && e.preventDefault()} /> |
| 463 | + <InlineEditSaveBar dataSource={{ update: vi.fn() }} objectName="proj" recordId="p1" data={{}} refresh={vi.fn()} /> |
| 464 | + </InlineEditProvider>, |
| 465 | + ); |
| 466 | + fireEvent.click(screen.getByText('edit-enter')); |
| 467 | + escape(screen.getByLabelText('own-esc')); |
| 468 | + expect(editing()).toBe(true); |
| 469 | + escape(document.body); |
| 470 | + expect(editing()).toBe(false); |
| 471 | + }); |
| 472 | +}); |
0 commit comments