|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#26794 — the page designer's add-block picker offers `list-view`. |
| 5 | + * |
| 6 | + * objectstack#26703 gave the `list-view` block an inspector panel (Object, View, |
| 7 | + * Row limit), but the picker is built from `BLOCK_TYPE_META` alone and had no |
| 8 | + * `list-view` row, so a designer could edit a list block a page already carried |
| 9 | + * and never add one. These cases drive the REAL picker in `PageBlockCanvas`: |
| 10 | + * the row sits under the Data heading, picking it writes a bare `list-view` |
| 11 | + * node and selects it, and that selection, handed to the REAL inspector with |
| 12 | + * the draft the canvas wrote, opens the three controls with nothing bound yet. |
| 13 | + */ |
| 14 | + |
| 15 | +import * as React from 'react'; |
| 16 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 17 | +import { render, screen, fireEvent, cleanup, within } from '@testing-library/react'; |
| 18 | +import { PageSchema } from '@objectstack/spec/ui'; |
| 19 | +import { PageBlockCanvas } from './PageBlockCanvas'; |
| 20 | +import { PageBlockInspector } from '../inspectors/PageBlockInspector'; |
| 21 | +import { CATEGORY_LABEL_KEY } from './block-types'; |
| 22 | +import { t } from '../i18n'; |
| 23 | +import type { MetadataSelection } from '../preview-registry'; |
| 24 | + |
| 25 | +vi.mock('./useObjectFields', () => ({ |
| 26 | + useObjectFields: () => ({ fields: [], loading: false, error: null }), |
| 27 | +})); |
| 28 | +// No object catalog: the Object picker falls back to its labelled text box. |
| 29 | +vi.mock('../useMetadata', () => ({ |
| 30 | + useMetadataClient: () => ({ get: vi.fn(async () => undefined), list: vi.fn(async () => [] as unknown[]) }), |
| 31 | +})); |
| 32 | + |
| 33 | +// The new type label and the inspector rows live in `@object-ui/i18n`'s |
| 34 | +// `studio` namespace, whose `zh` catalogue is fetched on demand |
| 35 | +// (objectstack#25922). Resident before any case, so zh reads synchronously. |
| 36 | +import { loadStudioCatalogue } from '@object-ui/i18n/namespaces/studio'; |
| 37 | +await loadStudioCatalogue('zh'); |
| 38 | + |
| 39 | +afterEach(cleanup); |
| 40 | + |
| 41 | +type Draft = Record<string, unknown>; |
| 42 | + |
| 43 | +const EMPTY_PAGE: Draft = { |
| 44 | + name: 'home', |
| 45 | + label: 'Home', |
| 46 | + type: 'home', |
| 47 | + template: 'default', |
| 48 | + regions: [{ name: 'main', components: [] }], |
| 49 | +}; |
| 50 | + |
| 51 | +/** Mounts the canvas over `EMPTY_PAGE` and records the drafts and selections it produces. */ |
| 52 | +function renderCanvas(locale: string) { |
| 53 | + const drafts: Draft[] = []; |
| 54 | + const selections: Array<MetadataSelection | null> = []; |
| 55 | + function Harness() { |
| 56 | + const [draft, setDraft] = React.useState<Draft>(EMPTY_PAGE); |
| 57 | + return ( |
| 58 | + <PageBlockCanvas |
| 59 | + draft={draft} |
| 60 | + locale={locale} |
| 61 | + onPatch={(patch) => { |
| 62 | + const next = { ...draft, ...patch }; |
| 63 | + drafts.push(next); |
| 64 | + setDraft(next); |
| 65 | + }} |
| 66 | + onSelectionChange={(next) => selections.push(next)} |
| 67 | + /> |
| 68 | + ); |
| 69 | + } |
| 70 | + render(<Harness />); |
| 71 | + return { drafts, selections }; |
| 72 | +} |
| 73 | + |
| 74 | +/** Opens the first region's picker and returns the `list-view` row and the heading of the group it sits in. */ |
| 75 | +function openPickerAtListView(locale: string): { row: HTMLButtonElement; heading: string } { |
| 76 | + const [add] = screen.getAllByRole('button', { name: t('engine.inspector.add.block', locale) }); |
| 77 | + fireEvent.click(add); |
| 78 | + const code = screen.getAllByText('list-view').find((el) => el.tagName === 'CODE'); |
| 79 | + expect(code, 'the picker offers a `list-view` row').toBeDefined(); |
| 80 | + const row = code!.closest('button') as HTMLButtonElement; |
| 81 | + const group = row.parentElement!; |
| 82 | + return { row, heading: group.firstElementChild!.textContent ?? '' }; |
| 83 | +} |
| 84 | + |
| 85 | +describe('the add-block picker offers list-view (objectstack#26794)', () => { |
| 86 | + it('lists it under the Data heading, named in the designer locale', () => { |
| 87 | + for (const locale of ['en-US', 'zh-CN']) { |
| 88 | + renderCanvas(locale); |
| 89 | + const { row, heading } = openPickerAtListView(locale); |
| 90 | + expect(heading).toBe(t(CATEGORY_LABEL_KEY.data, locale)); |
| 91 | + expect(within(row).getByText(t('engine.pageBlockCanvas.type.listView', locale))).toBeTruthy(); |
| 92 | + cleanup(); |
| 93 | + } |
| 94 | + // Control: the zh row is a translation, not the en name read through. |
| 95 | + expect(t('engine.pageBlockCanvas.type.listView', 'zh-CN')).not.toBe(t('engine.pageBlockCanvas.type.listView', 'en-US')); |
| 96 | + }); |
| 97 | + |
| 98 | + it('adding it writes a bare list-view node the spec accepts, and selects it', () => { |
| 99 | + const { drafts, selections } = renderCanvas('en-US'); |
| 100 | + fireEvent.click(openPickerAtListView('en-US').row); |
| 101 | + |
| 102 | + const written = drafts.at(-1)!; |
| 103 | + expect((written.regions as Array<{ components: unknown[] }>)[0].components).toEqual([{ type: 'list-view' }]); |
| 104 | + expect(PageSchema.safeParse(written).success).toBe(true); |
| 105 | + expect(selections.at(-1)).toMatchObject({ kind: 'block', id: 'regions[0].components[0]' }); |
| 106 | + }); |
| 107 | + |
| 108 | + it('selecting the added block opens the Object, View and Row limit panel, nothing bound yet', () => { |
| 109 | + const { drafts, selections } = renderCanvas('en-US'); |
| 110 | + fireEvent.click(openPickerAtListView('en-US').row); |
| 111 | + const draft = drafts.at(-1)!; |
| 112 | + const selection = selections.at(-1)!; |
| 113 | + cleanup(); |
| 114 | + |
| 115 | + const onPatch = vi.fn(); |
| 116 | + render( |
| 117 | + <PageBlockInspector |
| 118 | + type="page" |
| 119 | + name="home" |
| 120 | + draft={draft} |
| 121 | + selection={selection} |
| 122 | + onPatch={onPatch} |
| 123 | + onClearSelection={() => {}} |
| 124 | + readOnly={false} |
| 125 | + locale="en-US" |
| 126 | + />, |
| 127 | + ); |
| 128 | + const object = screen.getByLabelText('Object') as HTMLInputElement; |
| 129 | + const view = screen.getByLabelText('View') as HTMLInputElement; |
| 130 | + const limit = screen.getByLabelText('Row limit') as HTMLInputElement; |
| 131 | + expect([object.value, view.value, limit.value]).toEqual(['', '', '']); |
| 132 | + expect(limit.placeholder).toBe('5'); |
| 133 | + |
| 134 | + // The Object control is how the fresh block gets its object: it writes the |
| 135 | + // node-level `dataSource` binding the list reads. |
| 136 | + fireEvent.change(object, { target: { value: 'contact' } }); |
| 137 | + const block = (onPatch.mock.calls.at(-1)![0] as { regions: Array<{ components: Array<Record<string, unknown>> }> }) |
| 138 | + .regions[0].components[0]; |
| 139 | + expect(block).toEqual({ type: 'list-view', dataSource: { object: 'contact' } }); |
| 140 | + }); |
| 141 | +}); |
0 commit comments