|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#30046 (Scope 8) — a form view's 视图类型 reads a real type. |
| 5 | + * |
| 6 | + * The view inspector's type picker offered the LIST-view types alone (grid, |
| 7 | + * kanban, calendar, …), read off the spec's list-variant schema, for every |
| 8 | + * view it was shown on. A form view's `type` is a form layout (`simple`, |
| 9 | + * `tabbed`, `wizard`, `FormViewSchema.type`), which that roster does not |
| 10 | + * carry, so every form view read 「simple(未找到)」: its own, valid type |
| 11 | + * flagged as not found, beside list types it can never take. |
| 12 | + * |
| 13 | + * The picker now reads the roster of the view's own family: a form view's |
| 14 | + * from the spec's form-variant schema, a list view's unchanged. Both sides |
| 15 | + * are pinned, through the rendered home panel, in both designer locales. |
| 16 | + */ |
| 17 | + |
| 18 | +import * as React from 'react'; |
| 19 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 20 | +import { render, screen, cleanup, fireEvent, waitFor } from '@testing-library/react'; |
| 21 | + |
| 22 | +const state = vi.hoisted(() => ({ |
| 23 | + metadataClient: { |
| 24 | + get: vi.fn(async () => undefined), |
| 25 | + list: vi.fn(async (type: string) => (type === 'object' ? [{ name: 'crm_lead', label: 'Lead' }] : ([] as unknown[]))), |
| 26 | + }, |
| 27 | +})); |
| 28 | +vi.mock('../useMetadata', async (importOriginal) => { |
| 29 | + const mod = await importOriginal<typeof import('../useMetadata')>(); |
| 30 | + return { ...mod, useMetadataClient: () => state.metadataClient }; |
| 31 | +}); |
| 32 | + |
| 33 | +import { ViewDefaultInspector } from './ViewInspector'; |
| 34 | +import { t } from '../i18n'; |
| 35 | + |
| 36 | +afterEach(cleanup); |
| 37 | + |
| 38 | +type Locale = 'en-US' | 'zh-CN'; |
| 39 | + |
| 40 | +/** A view item as the server serves it: `{ name, object, viewKind, config }` (ADR-0017). */ |
| 41 | +function renderView(locale: Locale, viewKind: 'form' | 'list', config: Record<string, unknown>) { |
| 42 | + render( |
| 43 | + <ViewDefaultInspector |
| 44 | + type="view" |
| 45 | + name={`crm_lead.${viewKind}`} |
| 46 | + draft={{ name: `crm_lead.${viewKind}`, object: 'crm_lead', viewKind, config }} |
| 47 | + onPatch={vi.fn()} |
| 48 | + onSelectionChange={vi.fn()} |
| 49 | + readOnly={false} |
| 50 | + locale={locale as never} |
| 51 | + />, |
| 52 | + ); |
| 53 | + return screen.getByRole('combobox', { name: t('engine.inspector.view.type', locale) }); |
| 54 | +} |
| 55 | + |
| 56 | +/** The options the open picker offers, by their text. */ |
| 57 | +async function optionsOf(trigger: HTMLElement): Promise<string[]> { |
| 58 | + fireEvent.keyDown(trigger, { key: 'ArrowDown' }); |
| 59 | + await waitFor(() => expect(screen.queryAllByRole('option').length).toBeGreaterThan(0)); |
| 60 | + return screen.getAllByRole('option').map((o) => (o.textContent ?? '').trim()); |
| 61 | +} |
| 62 | + |
| 63 | +const formType = (value: string, locale: Locale) => t(`engine.inspector.pageBlock.option.formType.${value}`, locale); |
| 64 | +const listType = (value: string, locale: Locale) => t(`engine.inspector.view.type.${value}`, locale); |
| 65 | + |
| 66 | +describe('a form view’s 视图类型 reads a real type (objectstack#30046)', () => { |
| 67 | + it.each(['en-US', 'zh-CN'] as const)('%s — a form view reads its layout, never flagged as not found', async (locale) => { |
| 68 | + const trigger = renderView(locale, 'form', { type: 'simple', sections: [] }); |
| 69 | + expect(trigger.textContent).toContain(formType('simple', locale)); |
| 70 | + expect(trigger.textContent).not.toContain(t('engine.form.notFound', locale)); |
| 71 | + // The form family's own roster, and none of the list types. |
| 72 | + expect(await optionsOf(trigger)).toEqual([formType('simple', locale), formType('tabbed', locale), formType('wizard', locale)]); |
| 73 | + }); |
| 74 | + |
| 75 | + it.each(['en-US', 'zh-CN'] as const)('%s — a form view with no type reads the spec’s default layout', (locale) => { |
| 76 | + const trigger = renderView(locale, 'form', { sections: [] }); |
| 77 | + expect(trigger.textContent).toContain(formType('simple', locale)); |
| 78 | + expect(trigger.textContent).not.toContain(listType('grid', locale)); |
| 79 | + }); |
| 80 | + |
| 81 | + // The list-view side, unchanged: the same inspector, the list roster. |
| 82 | + it.each(['en-US', 'zh-CN'] as const)('%s — CONTROL: a list view still offers the list types', async (locale) => { |
| 83 | + const trigger = renderView(locale, 'list', { type: 'kanban', columns: ['name'] }); |
| 84 | + expect(trigger.textContent).toContain(listType('kanban', locale)); |
| 85 | + const offered = await optionsOf(trigger); |
| 86 | + expect(offered).toContain(listType('grid', locale)); |
| 87 | + expect(offered).toContain(listType('kanban', locale)); |
| 88 | + expect(offered).not.toContain(formType('simple', locale)); |
| 89 | + }); |
| 90 | + |
| 91 | + it.each(['en-US', 'zh-CN'] as const)('%s — CONTROL: a list type the spec does not carry is still flagged', (locale) => { |
| 92 | + const trigger = renderView(locale, 'list', { type: 'simple', columns: ['name'] }); |
| 93 | + expect(trigger.textContent).toContain(t('engine.form.notFound', locale)); |
| 94 | + }); |
| 95 | +}); |
0 commit comments