|
| 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 | + * objectui#12625 — the console record page's controls have accessible names, |
| 11 | + * read on the REAL `RecordDetailView` mount (the synthesized default page: |
| 12 | + * `page:header`, `page:tabs`, `record:details`), by role and name. |
| 13 | + * |
| 14 | + * - The tabs are named by their labels (Details, Related). |
| 15 | + * - A declared `record_header` action is named by its label (Log a Call). |
| 16 | + * - A collapsible field group's header is a button named by its label |
| 17 | + * (Lead Information) that toggles `aria-expanded` — the half the card found |
| 18 | + * really broken (`DetailSection`'s header was a div with no role). |
| 19 | + * |
| 20 | + * The tabs and the header actions were already named on main when this was |
| 21 | + * written; they are regression pins, so a restyle that moves a label into an |
| 22 | + * `aria-hidden` span turns this file red. |
| 23 | + */ |
| 24 | + |
| 25 | +import * as React from 'react'; |
| 26 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 27 | +import { render, waitFor, screen, cleanup } from '@testing-library/react'; |
| 28 | +import userEvent from '@testing-library/user-event'; |
| 29 | +import '@testing-library/jest-dom'; |
| 30 | +import { MemoryRouter } from 'react-router-dom'; |
| 31 | + |
| 32 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 33 | + ...(await importOriginal<Record<string, unknown>>()), |
| 34 | + useAuth: () => ({ user: { id: 'u1', name: 'Ada', image: null }, activeOrganization: null }), |
| 35 | + createAuthenticatedFetch: () => vi.fn(), |
| 36 | +})); |
| 37 | + |
| 38 | +vi.mock('@object-ui/collaboration', async (importOriginal) => ({ |
| 39 | + ...(await importOriginal<Record<string, unknown>>()), |
| 40 | + useRecordPresence: () => [], |
| 41 | + PresenceAvatars: () => null, |
| 42 | +})); |
| 43 | + |
| 44 | +vi.mock('sonner', () => ({ |
| 45 | + toast: Object.assign(vi.fn(), { |
| 46 | + success: vi.fn(), |
| 47 | + error: vi.fn(), |
| 48 | + info: vi.fn(), |
| 49 | + warning: vi.fn(), |
| 50 | + loading: vi.fn(), |
| 51 | + dismiss: vi.fn(), |
| 52 | + }), |
| 53 | +})); |
| 54 | + |
| 55 | +// Orthogonal chrome — same posture as RecordDetailView.headerActionLookupPredicate.test. |
| 56 | +vi.mock('./ActionConfirmDialog', () => ({ ActionConfirmDialog: () => null })); |
| 57 | +vi.mock('./ActionParamDialog', () => ({ ActionParamDialog: () => null })); |
| 58 | +vi.mock('./ActionResultDialog', () => ({ ActionResultDialog: () => null })); |
| 59 | +vi.mock('./FlowRunner', () => ({ FlowRunner: () => null })); |
| 60 | +vi.mock('./MetadataInspector', () => ({ |
| 61 | + MetadataPanel: () => null, |
| 62 | + useMetadataInspector: () => ({ showDebug: false, toggle: () => {} }), |
| 63 | +})); |
| 64 | + |
| 65 | +import { MetadataCtx } from '@object-ui/react'; |
| 66 | +import { RecordDetailView } from './RecordDetailView'; |
| 67 | +import { ExpressionProvider } from '../providers/ExpressionProvider'; |
| 68 | + |
| 69 | +const headerAction = (name: string, label: string) => ({ |
| 70 | + name, |
| 71 | + label, |
| 72 | + type: 'api', |
| 73 | + target: '/api/v1/noop', |
| 74 | + locations: ['record_header'], |
| 75 | +}); |
| 76 | + |
| 77 | +const LEAD = { |
| 78 | + name: 'lead', |
| 79 | + label: 'Lead', |
| 80 | + managedBy: 'platform', |
| 81 | + // One declared, ungrouped highlight keeps the heuristic strip from claiming |
| 82 | + // the grouped fields (see defaultFieldGroupsPage.sectionHeadings.test). |
| 83 | + highlightFields: ['name'], |
| 84 | + fieldGroups: [ |
| 85 | + { key: 'info', label: 'Lead Information', collapsible: true }, |
| 86 | + { key: 'addr', label: 'Address' }, |
| 87 | + ], |
| 88 | + fields: { |
| 89 | + id: { type: 'text', label: 'Id' }, |
| 90 | + name: { type: 'text', label: 'Name' }, |
| 91 | + company: { type: 'text', label: 'Company', group: 'info' }, |
| 92 | + city: { type: 'text', label: 'City', group: 'addr' }, |
| 93 | + }, |
| 94 | + actions: [ |
| 95 | + headerAction('log_call', 'Log a Call'), |
| 96 | + headerAction('log_meeting', 'Log a Meeting'), |
| 97 | + headerAction('schedule_meeting', 'Schedule a Meeting'), |
| 98 | + ], |
| 99 | +}; |
| 100 | + |
| 101 | +/** A child with a lookup to the lead, so the page carries a Related tab. */ |
| 102 | +const TASK = { |
| 103 | + name: 'task', |
| 104 | + label: 'Task', |
| 105 | + fields: { |
| 106 | + id: { type: 'text' }, |
| 107 | + subject: { type: 'text', label: 'Subject' }, |
| 108 | + lead: { type: 'lookup', reference: 'lead', label: 'Lead' }, |
| 109 | + }, |
| 110 | +}; |
| 111 | + |
| 112 | +async function mountLeadPage() { |
| 113 | + const objects = [LEAD, TASK]; |
| 114 | + const findOne = vi.fn(async () => ({ id: 'L1', name: 'Ada', company: 'Acme', city: 'Paris' })); |
| 115 | + const dataSource = { |
| 116 | + find: vi.fn(async () => ({ data: [] })), |
| 117 | + findOne, |
| 118 | + create: vi.fn(async () => ({})), |
| 119 | + update: vi.fn(async () => ({})), |
| 120 | + delete: vi.fn(async () => ({})), |
| 121 | + } as never; |
| 122 | + const metadata = { |
| 123 | + objects, |
| 124 | + pages: [], |
| 125 | + loading: false, |
| 126 | + error: null, |
| 127 | + refresh: async () => {}, |
| 128 | + invalidate: () => {}, |
| 129 | + ensureType: async () => [], |
| 130 | + getItem: async () => null, |
| 131 | + getItemsByType: () => [], |
| 132 | + } as never; |
| 133 | + render( |
| 134 | + <MemoryRouter initialEntries={['/apps/crm/lead/L1']}> |
| 135 | + <MetadataCtx.Provider value={metadata}> |
| 136 | + <ExpressionProvider user={{ id: 'u1', name: 'Ada' }} app={{ name: 'crm' }} data={{}} features={{}}> |
| 137 | + <RecordDetailView |
| 138 | + dataSource={dataSource} |
| 139 | + objects={objects} |
| 140 | + onEdit={() => {}} |
| 141 | + objectNameOverride="lead" |
| 142 | + recordIdOverride="L1" |
| 143 | + /> |
| 144 | + </ExpressionProvider> |
| 145 | + </MetadataCtx.Provider> |
| 146 | + </MemoryRouter>, |
| 147 | + ); |
| 148 | + await waitFor(() => expect(findOne).toHaveBeenCalled()); |
| 149 | + await screen.findByRole('button', { name: 'Lead Information' }); |
| 150 | +} |
| 151 | + |
| 152 | +beforeEach(() => { |
| 153 | + cleanup(); |
| 154 | + vi.stubGlobal( |
| 155 | + 'fetch', |
| 156 | + vi.fn(async () => |
| 157 | + new Response(JSON.stringify({ data: [] }), { |
| 158 | + status: 200, |
| 159 | + headers: { 'content-type': 'application/json' }, |
| 160 | + }), |
| 161 | + ), |
| 162 | + ); |
| 163 | +}); |
| 164 | + |
| 165 | +afterEach(() => { |
| 166 | + vi.unstubAllGlobals(); |
| 167 | + vi.clearAllMocks(); |
| 168 | +}); |
| 169 | + |
| 170 | +describe('objectui#12625 — the record page names its tabs, header actions and section headers', () => { |
| 171 | + it('the tabs are named by their labels', async () => { |
| 172 | + await mountLeadPage(); |
| 173 | + expect(screen.getByRole('tab', { name: 'Details' })).toBeInTheDocument(); |
| 174 | + expect(screen.getByRole('tab', { name: 'Related' })).toBeInTheDocument(); |
| 175 | + }); |
| 176 | + |
| 177 | + it('the header actions are named by their labels', async () => { |
| 178 | + await mountLeadPage(); |
| 179 | + expect(screen.getByRole('button', { name: 'Log a Call' })).toBeInTheDocument(); |
| 180 | + expect(screen.getByRole('button', { name: 'Log a Meeting' })).toBeInTheDocument(); |
| 181 | + expect(screen.getByRole('button', { name: 'Schedule a Meeting' })).toBeInTheDocument(); |
| 182 | + }); |
| 183 | + |
| 184 | + it('the collapsible field group header is a button named by its label that toggles', async () => { |
| 185 | + const user = userEvent.setup(); |
| 186 | + await mountLeadPage(); |
| 187 | + const header = screen.getByRole('button', { name: 'Lead Information' }); |
| 188 | + expect(header).toHaveAttribute('aria-expanded', 'true'); |
| 189 | + await user.click(header); |
| 190 | + expect(header).toHaveAttribute('aria-expanded', 'false'); |
| 191 | + // CONTROL: the non-collapsible group shows its heading but is no button. |
| 192 | + expect(screen.getByText('Address')).toBeInTheDocument(); |
| 193 | + expect(screen.queryByRole('button', { name: 'Address' })).toBeNull(); |
| 194 | + }); |
| 195 | +}); |
0 commit comments