|
| 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#27069 / objectstack#27241 — the record page's default related |
| 11 | + * order reads the object's declared `relatedLists`: a reader who never |
| 12 | + * arranged the related column sees the cards in that order, the rest after |
| 13 | + * them in the derived order (owned `master_detail` children first, then |
| 14 | + * lookups). Measured from a real `RecordDetailView` mount at three columns |
| 15 | + * (the `recordPageTiers-26678` harness: the root's measured width is the one |
| 16 | + * input varied). |
| 17 | + */ |
| 18 | + |
| 19 | +import * as React from 'react'; |
| 20 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 21 | +import { render, cleanup, waitFor } from '@testing-library/react'; |
| 22 | +import { MemoryRouter } from 'react-router-dom'; |
| 23 | +import { MetadataCtx } from '@object-ui/react'; |
| 24 | +import { RelatedCountStore } from '@object-ui/components'; |
| 25 | + |
| 26 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 27 | + ...(await importOriginal<Record<string, unknown>>()), |
| 28 | + useAuth: () => ({ user: { id: 'u1', name: 'Ada', image: null }, activeOrganization: null }), |
| 29 | + createAuthenticatedFetch: () => vi.fn(), |
| 30 | +})); |
| 31 | + |
| 32 | +vi.mock('@object-ui/collaboration', async (importOriginal) => ({ |
| 33 | + ...(await importOriginal<Record<string, unknown>>()), |
| 34 | + useRecordPresence: () => [], |
| 35 | + PresenceAvatars: () => null, |
| 36 | +})); |
| 37 | + |
| 38 | +vi.mock('sonner', () => ({ |
| 39 | + toast: Object.assign(vi.fn(), { |
| 40 | + success: vi.fn(), |
| 41 | + error: vi.fn(), |
| 42 | + info: vi.fn(), |
| 43 | + warning: vi.fn(), |
| 44 | + loading: vi.fn(), |
| 45 | + dismiss: vi.fn(), |
| 46 | + }), |
| 47 | +})); |
| 48 | + |
| 49 | +// Orthogonal chrome — stubbed so the only asynchrony here is the page's own. |
| 50 | +vi.mock('./ActionConfirmDialog', () => ({ ActionConfirmDialog: () => null })); |
| 51 | +vi.mock('./ActionParamDialog', () => ({ ActionParamDialog: () => null })); |
| 52 | +vi.mock('./ActionResultDialog', () => ({ ActionResultDialog: () => null })); |
| 53 | +vi.mock('./FlowRunner', () => ({ FlowRunner: () => null })); |
| 54 | +vi.mock('./MetadataInspector', () => ({ |
| 55 | + MetadataPanel: () => null, |
| 56 | + useMetadataInspector: () => ({ showDebug: false, toggle: () => {} }), |
| 57 | +})); |
| 58 | + |
| 59 | +import { RecordDetailView } from './RecordDetailView'; |
| 60 | + |
| 61 | +const PARENT = 'showcase_project'; |
| 62 | +const RECORD_ID = 'p-1'; |
| 63 | +const BASE_FIELDS = { id: { type: 'text', label: 'Id' }, name: { type: 'text', label: 'Name' } }; |
| 64 | + |
| 65 | +const objectsWith = (relatedLists?: string[]) => [ |
| 66 | + { |
| 67 | + name: PARENT, |
| 68 | + label: 'Project', |
| 69 | + pluralLabel: 'Projects', |
| 70 | + managedBy: 'platform', |
| 71 | + ...(relatedLists ? { relatedLists } : {}), |
| 72 | + fields: BASE_FIELDS, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: 'showcase_task', |
| 76 | + label: 'Task', |
| 77 | + pluralLabel: 'Tasks', |
| 78 | + managedBy: 'platform', |
| 79 | + fields: { ...BASE_FIELDS, project: { type: 'master_detail', reference: PARENT, label: 'Project' } }, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: 'showcase_note', |
| 83 | + label: 'Note', |
| 84 | + pluralLabel: 'Notes', |
| 85 | + managedBy: 'platform', |
| 86 | + fields: { ...BASE_FIELDS, project: { type: 'lookup', reference: PARENT, label: 'Project' } }, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: 'showcase_call', |
| 90 | + label: 'Call', |
| 91 | + pluralLabel: 'Calls', |
| 92 | + managedBy: 'platform', |
| 93 | + fields: { ...BASE_FIELDS, project: { type: 'lookup', reference: PARENT, label: 'Project' } }, |
| 94 | + }, |
| 95 | +]; |
| 96 | + |
| 97 | +function renderPage(objects: ReturnType<typeof objectsWith>) { |
| 98 | + const metadata = { |
| 99 | + objects, |
| 100 | + pages: [], |
| 101 | + loading: false, |
| 102 | + error: null, |
| 103 | + refresh: async () => {}, |
| 104 | + invalidate: () => {}, |
| 105 | + ensureType: async () => [], |
| 106 | + getItem: async () => null, |
| 107 | + getItemsByType: () => [], |
| 108 | + } as never; |
| 109 | + const dataSource = { |
| 110 | + find: vi.fn(async () => ({ data: [], total: 0 })), |
| 111 | + create: vi.fn(async (_o: string, row: unknown) => row), |
| 112 | + findOne: vi.fn(async (_o: string, recordId: string) => ({ id: recordId, name: `Project ${recordId}` })), |
| 113 | + update: vi.fn(async () => ({})), |
| 114 | + delete: vi.fn(async () => ({})), |
| 115 | + }; |
| 116 | + return render( |
| 117 | + <MemoryRouter initialEntries={[`/app/demo/${PARENT}/${RECORD_ID}`]}> |
| 118 | + <MetadataCtx.Provider value={metadata}> |
| 119 | + <RecordDetailView |
| 120 | + dataSource={dataSource as never} |
| 121 | + objects={objects} |
| 122 | + onEdit={() => {}} |
| 123 | + objectNameOverride={PARENT} |
| 124 | + recordIdOverride={RECORD_ID} |
| 125 | + /> |
| 126 | + </MetadataCtx.Provider> |
| 127 | + </MemoryRouter>, |
| 128 | + ); |
| 129 | +} |
| 130 | + |
| 131 | +const cardOrder = (container: HTMLElement) => |
| 132 | + Array.from(container.querySelectorAll('[data-record-column="related"] [data-related-card-id]')).map((el) => |
| 133 | + el.getAttribute('data-related-card-id'), |
| 134 | + ); |
| 135 | + |
| 136 | +beforeEach(() => { |
| 137 | + cleanup(); |
| 138 | + window.localStorage.clear(); |
| 139 | + RelatedCountStore._reset(); |
| 140 | + vi.stubGlobal( |
| 141 | + 'fetch', |
| 142 | + vi.fn(async () => |
| 143 | + new Response(JSON.stringify({ data: [] }), { status: 200, headers: { 'content-type': 'application/json' } }), |
| 144 | + ), |
| 145 | + ); |
| 146 | + const measure = HTMLElement.prototype.getBoundingClientRect; |
| 147 | + vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(function (this: HTMLElement) { |
| 148 | + if (!this.hasAttribute('data-record-page-root')) return measure.call(this); |
| 149 | + return { x: 0, y: 0, top: 0, left: 0, right: 1440, bottom: 800, width: 1440, height: 800, toJSON: () => ({}) } as DOMRect; |
| 150 | + }); |
| 151 | +}); |
| 152 | + |
| 153 | +afterEach(() => { |
| 154 | + vi.unstubAllGlobals(); |
| 155 | + vi.restoreAllMocks(); |
| 156 | +}); |
| 157 | + |
| 158 | +describe('RecordDetailView — the related cards follow the object\'s relatedLists (objectstack#27069)', () => { |
| 159 | + it('without relatedLists: the derived order — the owned child first, then the lookups in declaration order', async () => { |
| 160 | + const { container } = renderPage(objectsWith()); |
| 161 | + await waitFor(() => expect(cardOrder(container)).toHaveLength(3), { timeout: 5000 }); |
| 162 | + expect(cardOrder(container)).toEqual(['showcase_task.project', 'showcase_note.project', 'showcase_call.project']); |
| 163 | + }); |
| 164 | + |
| 165 | + it('with relatedLists: the declared lists first in that order, the rest after them in the derived order', async () => { |
| 166 | + const { container } = renderPage(objectsWith(['showcase_call', 'showcase_note.project'])); |
| 167 | + await waitFor(() => expect(cardOrder(container)).toHaveLength(3), { timeout: 5000 }); |
| 168 | + expect(cardOrder(container)).toEqual(['showcase_call.project', 'showcase_note.project', 'showcase_task.project']); |
| 169 | + }); |
| 170 | +}); |
0 commit comments