|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The page designer's Design canvas draws an authored `record:details` body's |
| 5 | + * values the way the record page does. |
| 6 | + * |
| 7 | + * A `full` record page names its detail fields (the full-create seed writes |
| 8 | + * `sections[].fields` as bare names). The canvas mounts the runtime |
| 9 | + * `record:details` renderer under a record context that holds the bound |
| 10 | + * object's schema but no dataSource, as every Studio preview does. The field |
| 11 | + * types come from that schema: without it a `select` drew its stored code and a |
| 12 | + * `location` drew `[Object]`. |
| 13 | + * |
| 14 | + * Everything here is real: the canvas, the record context, `record:details`, |
| 15 | + * `DetailView` and the `@object-ui/fields` cell renderers. Only the two |
| 16 | + * endpoints `PagePreview` reads (the object schema and the sample records) are |
| 17 | + * answered by a double. |
| 18 | + */ |
| 19 | + |
| 20 | +import * as React from 'react'; |
| 21 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 22 | +import { render, waitFor, within, cleanup } from '@testing-library/react'; |
| 23 | +// Registers `record:details`, which the canvas resolves through the registry. |
| 24 | +import { buildDefaultPageSchema } from '@object-ui/plugin-detail'; |
| 25 | + |
| 26 | +/** Stable stub identity: `PagePreview` carries `perms` in an effect dep list. */ |
| 27 | +const { permsStub } = vi.hoisted(() => ({ |
| 28 | + permsStub: { |
| 29 | + isLoaded: true, |
| 30 | + checkField: () => true, |
| 31 | + check: () => ({ allowed: true }), |
| 32 | + getFieldPermissions: () => [], |
| 33 | + getRowFilter: () => undefined, |
| 34 | + getObjectApiOperations: () => undefined, |
| 35 | + roles: [], |
| 36 | + userId: null, |
| 37 | + systemPermissions: undefined, |
| 38 | + hasCapabilities: () => true, |
| 39 | + can: () => true, |
| 40 | + cannot: () => false, |
| 41 | + }, |
| 42 | +})); |
| 43 | + |
| 44 | +vi.mock('@object-ui/permissions', async (importOriginal) => { |
| 45 | + const actual = await importOriginal<typeof import('@object-ui/permissions')>(); |
| 46 | + return { ...actual, usePermissions: () => permsStub as any }; |
| 47 | +}); |
| 48 | + |
| 49 | +import { PagePreview } from './PagePreview'; |
| 50 | +import { |
| 51 | + deriveStudioDetailSections, |
| 52 | + toAuthoredDetailSections, |
| 53 | + MORE_DETAILS_DEFAULT_LABEL, |
| 54 | +} from '../../recordDetailSections'; |
| 55 | + |
| 56 | +const OBJECT = 'showcase_account'; |
| 57 | +const OBJECT_DEF = { |
| 58 | + name: OBJECT, |
| 59 | + label: 'Account', |
| 60 | + // The highlights strip takes this field, so the three below stay in the |
| 61 | + // details body: the strip reads the same record context and draws its own. |
| 62 | + highlightFields: ['website'], |
| 63 | + fields: { |
| 64 | + name: { type: 'text', label: 'Account Name' }, |
| 65 | + website: { type: 'text', label: 'Website' }, |
| 66 | + account_code: { type: 'text', label: 'Account Code' }, |
| 67 | + sales_region: { |
| 68 | + type: 'select', |
| 69 | + label: 'Sales Region', |
| 70 | + options: [ |
| 71 | + { value: 'amer', label: 'Americas' }, |
| 72 | + { value: 'emea', label: 'Europe' }, |
| 73 | + ], |
| 74 | + }, |
| 75 | + headquarters: { type: 'location', label: 'Headquarters' }, |
| 76 | + }, |
| 77 | +}; |
| 78 | +const RECORD = { |
| 79 | + id: 'acc_1', |
| 80 | + name: 'Northwind', |
| 81 | + website: 'northwind.example', |
| 82 | + account_code: 'NW-001', |
| 83 | + sales_region: 'amer', |
| 84 | + headquarters: { latitude: 37.7749, longitude: -122.4194 }, |
| 85 | +}; |
| 86 | + |
| 87 | +/** The page the full-create seed writes for this object: authored, named fields. */ |
| 88 | +function fullCreateSeed(): Record<string, unknown> { |
| 89 | + const sections = toAuthoredDetailSections( |
| 90 | + deriveStudioDetailSections(OBJECT_DEF as any, { moreDetails: MORE_DETAILS_DEFAULT_LABEL }), |
| 91 | + ); |
| 92 | + const synth = buildDefaultPageSchema(OBJECT_DEF as any, sections ? { sections: sections as any } : undefined) as { |
| 93 | + regions: unknown[]; |
| 94 | + template?: unknown; |
| 95 | + }; |
| 96 | + return { |
| 97 | + name: 'account_page', |
| 98 | + label: 'Account page', |
| 99 | + type: 'record', |
| 100 | + object: OBJECT, |
| 101 | + kind: 'full', |
| 102 | + regions: synth.regions, |
| 103 | + ...(synth.template !== undefined ? { template: synth.template } : {}), |
| 104 | + }; |
| 105 | +} |
| 106 | + |
| 107 | +function stubFetch() { |
| 108 | + vi.stubGlobal('fetch', vi.fn(async (url: string) => { |
| 109 | + if (url.startsWith('/api/v1/meta/object/')) return { ok: true, json: async () => ({ item: OBJECT_DEF }) } as any; |
| 110 | + if (url.startsWith(`/api/v1/data/${OBJECT}`)) return { ok: true, json: async () => ({ records: [RECORD] }) } as any; |
| 111 | + // The record-edit probe and anything else the real blocks ask for. |
| 112 | + return { ok: true, json: async () => ({}) } as any; |
| 113 | + })); |
| 114 | +} |
| 115 | + |
| 116 | +afterEach(() => { |
| 117 | + cleanup(); |
| 118 | + vi.unstubAllGlobals(); |
| 119 | +}); |
| 120 | + |
| 121 | +describe('PagePreview Design canvas: an authored record:details draws typed values', () => { |
| 122 | + it('names its detail fields as bare names (the premise this file pins)', () => { |
| 123 | + const seed = JSON.stringify(fullCreateSeed()); |
| 124 | + expect(seed).toContain('"record:details"'); |
| 125 | + expect(seed).toContain('"sales_region"'); |
| 126 | + expect(seed).not.toContain('"type":"select"'); |
| 127 | + }); |
| 128 | + |
| 129 | + it('draws a select by its option label and a location as coordinates, never the raw value', async () => { |
| 130 | + stubFetch(); |
| 131 | + render( |
| 132 | + <PagePreview |
| 133 | + type="page" |
| 134 | + name="account_page" |
| 135 | + draft={fullCreateSeed()} |
| 136 | + editing |
| 137 | + selection={null} |
| 138 | + onSelectionChange={() => {}} |
| 139 | + onPatch={vi.fn() as any} |
| 140 | + locale="en-US" |
| 141 | + />, |
| 142 | + ); |
| 143 | + |
| 144 | + // The block draws its "bind a record" placeholder until the sample record |
| 145 | + // is bound, so wait for the bound body. |
| 146 | + const details = await waitFor(() => { |
| 147 | + const el = document.querySelector('[data-obj-type="record:details"]:not([data-record-details-placeholder])'); |
| 148 | + expect(el).not.toBeNull(); |
| 149 | + // Control: a plain text field reads as stored, before and after the fix. |
| 150 | + within(el as HTMLElement).getByText('NW-001'); |
| 151 | + return el as HTMLElement; |
| 152 | + }); |
| 153 | + const body = within(details); |
| 154 | + expect(body.getByText('Americas')).toBeTruthy(); |
| 155 | + expect(body.getByText(/37\.7749, -122\.4194/)).toBeTruthy(); |
| 156 | + expect(body.queryByText('amer')).toBeNull(); |
| 157 | + expect(details.textContent).not.toContain('[Object]'); |
| 158 | + }); |
| 159 | +}); |
0 commit comments