|
46 | 46 |
|
47 | 47 | import * as React from 'react'; |
48 | 48 | import { describe, it, expect, vi, beforeEach, afterEach, beforeAll, afterAll } from 'vitest'; |
49 | | -import { render, waitFor, cleanup, screen, act, fireEvent } from '@testing-library/react'; |
| 49 | +import { render, waitFor, cleanup, screen, act, fireEvent, within } from '@testing-library/react'; |
50 | 50 | import { MemoryRouter, Route, Routes, BrowserRouter } from 'react-router-dom'; |
51 | 51 | import { toast } from 'sonner'; |
52 | 52 | import { RelatedCountStore } from '@object-ui/components'; |
@@ -3430,3 +3430,163 @@ describe('RecordDetailView.approvalSubjectTitle-35415', () => { |
3430 | 3430 | }); |
3431 | 3431 | }); |
3432 | 3432 | }); |
| 3433 | + |
| 3434 | +// ── RecordDetailView.embeddedStageReread-36309 ────────────────────────────── |
| 3435 | + |
| 3436 | +/** |
| 3437 | + * objectstack#36309 — the header's stage menu draws in a drawer too (the list |
| 3438 | + * drawer, and the drawer a record page opens over itself, both mount this view |
| 3439 | + * `embedded`). A 409 on a Move there re-reads the record, so the next Move |
| 3440 | + * carries the version that is stored. The drawer still draws no Refresh entry: |
| 3441 | + * showing the entry and re-reading the record are two context members. |
| 3442 | + */ |
| 3443 | +describe('RecordDetailView.embeddedStageReread-36309', () => { |
| 3444 | + restoreAfterBlock(); |
| 3445 | + |
| 3446 | + const OBJECT = 'crm_deal_36309'; |
| 3447 | + const ID = 'd1'; |
| 3448 | + const DEF = { |
| 3449 | + name: OBJECT, |
| 3450 | + label: 'Deal', |
| 3451 | + nameField: 'name', |
| 3452 | + fields: { |
| 3453 | + id: { label: 'Id', type: 'text' }, |
| 3454 | + name: { label: 'Name', type: 'text' }, |
| 3455 | + stage: { |
| 3456 | + label: 'Stage', |
| 3457 | + type: 'select', |
| 3458 | + options: [ |
| 3459 | + { value: 'prospecting', label: 'Prospecting' }, |
| 3460 | + { value: 'negotiation', label: 'Negotiation' }, |
| 3461 | + ], |
| 3462 | + }, |
| 3463 | + }, |
| 3464 | + }; |
| 3465 | + const versionAt = (read: number) => `2026-11-22T0${read}:00:00.000Z`; |
| 3466 | + |
| 3467 | + function makeMetadata() { |
| 3468 | + const pages: any[] = []; |
| 3469 | + return { |
| 3470 | + objects: [DEF], pages, loading: false, error: null, |
| 3471 | + refresh: async () => {}, invalidate: () => {}, |
| 3472 | + ensureType: async () => pages, getItem: async () => null, |
| 3473 | + getItemsByType: () => pages, |
| 3474 | + } as any; |
| 3475 | + } |
| 3476 | + |
| 3477 | + /** Every read answers a newer version; the first Move is refused with a 409. */ |
| 3478 | + function renderDeal(embedded: boolean) { |
| 3479 | + let reads = 0; |
| 3480 | + const conflict = Object.assign(new Error('CONCURRENT_UPDATE'), { code: 'CONCURRENT_UPDATE', status: 409 }); |
| 3481 | + let writes = 0; |
| 3482 | + const ds = { |
| 3483 | + find: vi.fn(async () => ({ data: [], total: 0, hasMore: false, pageSize: 50 })), |
| 3484 | + findOne: vi.fn(async () => { |
| 3485 | + reads += 1; |
| 3486 | + return { id: ID, name: 'Wayne deal', stage: 'prospecting', updated_at: versionAt(reads) }; |
| 3487 | + }), |
| 3488 | + create: vi.fn(async (_o: string, r: any) => r), |
| 3489 | + update: vi.fn(async () => { |
| 3490 | + writes += 1; |
| 3491 | + if (writes === 1) throw conflict; |
| 3492 | + return {}; |
| 3493 | + }), |
| 3494 | + delete: vi.fn(async () => ({})), |
| 3495 | + getObjectSchema: async () => DEF, |
| 3496 | + } as Record<string, any>; |
| 3497 | + render( |
| 3498 | + <MemoryRouter initialEntries={[`/apps/demo/${OBJECT}/record/${ID}`]}> |
| 3499 | + <MetadataCtx.Provider value={makeMetadata()}> |
| 3500 | + <NavigationProvider> |
| 3501 | + <RecordDetailView |
| 3502 | + dataSource={ds as never} |
| 3503 | + objects={[DEF] as never} |
| 3504 | + onEdit={() => {}} |
| 3505 | + objectNameOverride={OBJECT} |
| 3506 | + recordIdOverride={ID} |
| 3507 | + embedded={embedded} |
| 3508 | + /> |
| 3509 | + </NavigationProvider> |
| 3510 | + </MetadataCtx.Provider> |
| 3511 | + </MemoryRouter>, |
| 3512 | + ); |
| 3513 | + return { ds }; |
| 3514 | + } |
| 3515 | + |
| 3516 | + const stageMenu = () => document.querySelector('[data-record-stage-menu]') as HTMLElement | null; |
| 3517 | + const moreTrigger = () => |
| 3518 | + document.querySelector('header [aria-label="More actions"], [data-record-top-bar-actions] [aria-label="More actions"]') as HTMLElement | null; |
| 3519 | + |
| 3520 | + async function pickNegotiation() { |
| 3521 | + await waitFor(() => expect(stageMenu()).toBeTruthy()); |
| 3522 | + fireEvent.pointerDown(stageMenu()!, { button: 0, ctrlKey: false, pointerType: 'mouse' }); |
| 3523 | + const option = await waitFor(() => { |
| 3524 | + const el = document.querySelector('[data-record-stage-menu-list] [data-stage-value="negotiation"]') as HTMLElement | null; |
| 3525 | + expect(el).toBeTruthy(); |
| 3526 | + return el!; |
| 3527 | + }); |
| 3528 | + fireEvent.click(option); |
| 3529 | + return screen.findByRole('dialog'); |
| 3530 | + } |
| 3531 | + |
| 3532 | + beforeAll(() => { |
| 3533 | + Object.defineProperty(window, 'innerWidth', { configurable: true, value: 1280 }); |
| 3534 | + }); |
| 3535 | + |
| 3536 | + beforeEach(() => { |
| 3537 | + vi.stubGlobal('fetch', vi.fn(async () => ({ |
| 3538 | + ok: true, |
| 3539 | + status: 200, |
| 3540 | + json: async () => ({ allowed: true, data: [] }), |
| 3541 | + text: async () => '{}', |
| 3542 | + })) as never); |
| 3543 | + }); |
| 3544 | + |
| 3545 | + afterEach(() => { |
| 3546 | + cleanup(); |
| 3547 | + vi.unstubAllGlobals(); |
| 3548 | + }); |
| 3549 | + |
| 3550 | + it('in a drawer, a 409 on Move re-reads the record and the second Move carries the new version', async () => { |
| 3551 | + const { ds } = renderDeal(true); |
| 3552 | + const dialog = await pickNegotiation(); |
| 3553 | + expect(ds.findOne).toHaveBeenCalledTimes(1); |
| 3554 | + |
| 3555 | + fireEvent.click(within(dialog).getByRole('button', { name: 'Move' })); |
| 3556 | + await waitFor(() => expect(ds.update).toHaveBeenCalledTimes(1)); |
| 3557 | + expect(ds.update).toHaveBeenLastCalledWith(OBJECT, ID, { stage: 'negotiation' }, { ifMatch: versionAt(1) }); |
| 3558 | + // The refusal re-reads the record in place. |
| 3559 | + await waitFor(() => expect(ds.findOne).toHaveBeenCalledTimes(2)); |
| 3560 | + |
| 3561 | + fireEvent.click(within(await screen.findByRole('dialog')).getByRole('button', { name: 'Move' })); |
| 3562 | + await waitFor(() => expect(ds.update).toHaveBeenCalledTimes(2)); |
| 3563 | + expect(ds.update).toHaveBeenLastCalledWith(OBJECT, ID, { stage: 'negotiation' }, { ifMatch: versionAt(2) }); |
| 3564 | + }); |
| 3565 | + |
| 3566 | + it('CONTROL — the drawer still draws no Refresh entry in the header', async () => { |
| 3567 | + const { ds } = renderDeal(true); |
| 3568 | + await waitFor(() => expect(ds.findOne).toHaveBeenCalled()); |
| 3569 | + await waitFor(() => expect(stageMenu()).toBeTruthy()); |
| 3570 | + const trigger = moreTrigger(); |
| 3571 | + if (trigger) { |
| 3572 | + fireEvent.pointerDown(trigger, { button: 0, ctrlKey: false, pointerType: 'mouse' }); |
| 3573 | + await waitFor(() => expect(document.querySelector('[role="menu"]')).toBeTruthy()); |
| 3574 | + } |
| 3575 | + expect(document.querySelector('[data-page-refresh]')).toBeNull(); |
| 3576 | + }); |
| 3577 | + |
| 3578 | + it('CONTROL — on the record page the same 409 re-reads too, and the page keeps its Refresh entry', async () => { |
| 3579 | + const { ds } = renderDeal(false); |
| 3580 | + const dialog = await pickNegotiation(); |
| 3581 | + fireEvent.click(within(dialog).getByRole('button', { name: 'Move' })); |
| 3582 | + await waitFor(() => expect(ds.update).toHaveBeenCalledTimes(1)); |
| 3583 | + await waitFor(() => expect(ds.findOne).toHaveBeenCalledTimes(2)); |
| 3584 | + fireEvent.click(within(await screen.findByRole('dialog')).getByRole('button', { name: 'Move' })); |
| 3585 | + await waitFor(() => expect(ds.update).toHaveBeenLastCalledWith(OBJECT, ID, { stage: 'negotiation' }, { ifMatch: versionAt(2) })); |
| 3586 | + await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull()); |
| 3587 | + |
| 3588 | + await waitFor(() => expect(moreTrigger()).toBeTruthy()); |
| 3589 | + fireEvent.pointerDown(moreTrigger()!, { button: 0, ctrlKey: false, pointerType: 'mouse' }); |
| 3590 | + await waitFor(() => expect(document.querySelector('[data-page-refresh]')).toBeTruthy()); |
| 3591 | + }); |
| 3592 | +}); |
0 commit comments