|
| 1 | +/** |
| 2 | + * RecordDetailView — Edit recordId stripping tests |
| 3 | + * |
| 4 | + * Validates that when editing from the detail page, the objectName prefix |
| 5 | + * is stripped from the URL-based recordId before passing to the onEdit callback. |
| 6 | + * |
| 7 | + * Related: objectstack-ai/objectui — "Record not found" bug |
| 8 | + */ |
| 9 | + |
| 10 | +import { describe, it, expect, vi } from 'vitest'; |
| 11 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 12 | +import userEvent from '@testing-library/user-event'; |
| 13 | +import '@testing-library/jest-dom'; |
| 14 | +import { MemoryRouter, Routes, Route } from 'react-router-dom'; |
| 15 | +import { RecordDetailView } from '../components/RecordDetailView'; |
| 16 | +import type { DataSource } from '@object-ui/types'; |
| 17 | + |
| 18 | +// ─── Mocks ─────────────────────────────────────────────────────────────────── |
| 19 | + |
| 20 | +function createMockDataSource(): DataSource { |
| 21 | + return { |
| 22 | + async getObjectSchema() { |
| 23 | + return { |
| 24 | + name: 'contact', |
| 25 | + label: 'Contact', |
| 26 | + fields: { |
| 27 | + name: { name: 'name', label: 'Name', type: 'text' }, |
| 28 | + email: { name: 'email', label: 'Email', type: 'email' }, |
| 29 | + }, |
| 30 | + }; |
| 31 | + }, |
| 32 | + findOne: vi.fn().mockResolvedValue({ id: '1772350253615-4', name: 'Alice' }), |
| 33 | + find: vi.fn().mockResolvedValue({ data: [] }), |
| 34 | + create: vi.fn().mockResolvedValue({ id: '1' }), |
| 35 | + update: vi.fn().mockResolvedValue({ id: '1' }), |
| 36 | + delete: vi.fn().mockResolvedValue(true), |
| 37 | + } as any; |
| 38 | +} |
| 39 | + |
| 40 | +const mockObjects = [ |
| 41 | + { |
| 42 | + name: 'contact', |
| 43 | + label: 'Contact', |
| 44 | + fields: { |
| 45 | + name: { name: 'name', label: 'Name', type: 'text' }, |
| 46 | + email: { name: 'email', label: 'Email', type: 'email' }, |
| 47 | + }, |
| 48 | + }, |
| 49 | +]; |
| 50 | + |
| 51 | +function renderDetailView( |
| 52 | + urlRecordId: string, |
| 53 | + objectName: string, |
| 54 | + onEdit: (record: any) => void, |
| 55 | + ds?: DataSource, |
| 56 | +) { |
| 57 | + const dataSource = ds ?? createMockDataSource(); |
| 58 | + return render( |
| 59 | + <MemoryRouter initialEntries={[`/${objectName}/record/${urlRecordId}`]}> |
| 60 | + <Routes> |
| 61 | + <Route |
| 62 | + path="/:objectName/record/:recordId" |
| 63 | + element={ |
| 64 | + <RecordDetailView |
| 65 | + dataSource={dataSource} |
| 66 | + objects={mockObjects} |
| 67 | + onEdit={onEdit} |
| 68 | + /> |
| 69 | + } |
| 70 | + /> |
| 71 | + </Routes> |
| 72 | + </MemoryRouter>, |
| 73 | + ); |
| 74 | +} |
| 75 | + |
| 76 | +// ─── Tests ─────────────────────────────────────────────────────────────────── |
| 77 | + |
| 78 | +describe('RecordDetailView — onEdit recordId stripping', () => { |
| 79 | + it('strips objectName prefix from recordId when editing', async () => { |
| 80 | + const onEdit = vi.fn(); |
| 81 | + |
| 82 | + renderDetailView('contact-1772350253615-4', 'contact', onEdit); |
| 83 | + |
| 84 | + // Wait for the detail view to load |
| 85 | + await waitFor(() => { |
| 86 | + expect(screen.getByText('Contact')).toBeInTheDocument(); |
| 87 | + }); |
| 88 | + |
| 89 | + // Click the Edit button |
| 90 | + const editButton = await screen.findByRole('button', { name: /edit/i }); |
| 91 | + await userEvent.click(editButton); |
| 92 | + |
| 93 | + // The onEdit callback should receive the STRIPPED recordId (no objectName prefix) |
| 94 | + expect(onEdit).toHaveBeenCalledWith( |
| 95 | + expect.objectContaining({ |
| 96 | + id: '1772350253615-4', |
| 97 | + _id: '1772350253615-4', |
| 98 | + }), |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + it('passes recordId as-is when no objectName prefix', async () => { |
| 103 | + const onEdit = vi.fn(); |
| 104 | + |
| 105 | + renderDetailView('plain-id-12345', 'contact', onEdit); |
| 106 | + |
| 107 | + await waitFor(() => { |
| 108 | + expect(screen.getByText('Contact')).toBeInTheDocument(); |
| 109 | + }); |
| 110 | + |
| 111 | + const editButton = await screen.findByRole('button', { name: /edit/i }); |
| 112 | + await userEvent.click(editButton); |
| 113 | + |
| 114 | + // No prefix to strip — should pass the original recordId unchanged |
| 115 | + expect(onEdit).toHaveBeenCalledWith( |
| 116 | + expect.objectContaining({ |
| 117 | + id: 'plain-id-12345', |
| 118 | + _id: 'plain-id-12345', |
| 119 | + }), |
| 120 | + ); |
| 121 | + }); |
| 122 | +}); |
0 commit comments