|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#36459 — a view's conditional formatting editor hands the lint the |
| 5 | + * object's field types and `multiple`, so `record.<list field> == 'x'` — a list |
| 6 | + * compared with a scalar, one constant answer on every row, so the rule never |
| 7 | + * styles anything — is refused (`list-equality`, with the membership rewrite) |
| 8 | + * while the author types. A membership test on the same field is accepted. |
| 9 | + * |
| 10 | + * Pinned against the REAL `@objectstack/formula` engine: |
| 11 | + * - the editor itself: `tags` (a `select` declared `multiple: true`) is the |
| 12 | + * list subject; `stage` (a plain `select`) is the scalar CONTROL; `'vip' in |
| 13 | + * record.tags` is the shape that does match; |
| 14 | + * - both mount sites hand the object's field definitions through: the view |
| 15 | + * variant inspector (metadata admin) and the Studio list-view panel's |
| 16 | + * "more" section. |
| 17 | + * |
| 18 | + * Direction: without `fieldDefs` reaching the editor no field is judged a list, |
| 19 | + * `record.tags == 'vip'` lints clean and the refusing cases are red; the |
| 20 | + * controls are green either way. |
| 21 | + */ |
| 22 | + |
| 23 | +import '@testing-library/jest-dom/vitest'; |
| 24 | +// The CEL authoring helpers load these through dynamic imports; paying the first |
| 25 | +// load at module top keeps it out of every clocked window (`check:test-source-alias`). |
| 26 | +import '@objectstack/formula'; |
| 27 | +import '@objectstack/lint'; |
| 28 | +import * as React from 'react'; |
| 29 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 30 | +import { render, screen, fireEvent, cleanup, waitFor, within } from '@testing-library/react'; |
| 31 | + |
| 32 | +const TICKET_FIELDS = { |
| 33 | + name: { name: 'name', label: 'Name', type: 'text' }, |
| 34 | + tags: { |
| 35 | + name: 'tags', label: 'Tags', type: 'select', multiple: true, |
| 36 | + options: [{ value: 'vip', label: 'VIP' }, { value: 'new', label: 'New' }], |
| 37 | + }, |
| 38 | + stage: { |
| 39 | + name: 'stage', label: 'Stage', type: 'select', |
| 40 | + options: [{ value: 'open', label: 'Open' }, { value: 'done', label: 'Done' }], |
| 41 | + }, |
| 42 | +} as const; |
| 43 | +const TICKET = { name: 'ticket', label: 'Ticket', fields: TICKET_FIELDS }; |
| 44 | + |
| 45 | +const clients = vi.hoisted(() => ({ |
| 46 | + fake: { |
| 47 | + get: async () => undefined, |
| 48 | + list: async () => [] as unknown[], |
| 49 | + listDrafts: async () => [] as unknown[], |
| 50 | + withPreviewDrafts() { |
| 51 | + return this; |
| 52 | + }, |
| 53 | + }, |
| 54 | +})); |
| 55 | +vi.mock('./useMetadata', async (importOriginal) => { |
| 56 | + const mod = await importOriginal<typeof import('./useMetadata')>(); |
| 57 | + return { ...mod, useMetadataClient: () => clients.fake }; |
| 58 | +}); |
| 59 | + |
| 60 | +import { ConditionalFormattingEditor, type ConditionalFormattingRuleDraft } from './ConditionalFormattingEditor'; |
| 61 | +import { ViewVariantInspector } from './inspectors/ViewVariantInspector'; |
| 62 | +import { ListViewMoreSection } from '../studio-design/ListViewMoreSection'; |
| 63 | +import { mapObjectFields } from '../ViewConfigPanel'; |
| 64 | +import { loaded } from './loadState'; |
| 65 | + |
| 66 | +afterEach(cleanup); |
| 67 | + |
| 68 | +const t = (k: string) => k; |
| 69 | +const FIELD_DEFS = Object.values(TICKET_FIELDS).map((f) => ({ |
| 70 | + name: f.name, |
| 71 | + type: f.type, |
| 72 | + multiple: 'multiple' in f ? f.multiple : undefined, |
| 73 | +})); |
| 74 | +const LIST_FINDING = /`record\.tags` holds a list/; |
| 75 | + |
| 76 | +function EditorHarness({ |
| 77 | + initial, |
| 78 | + report, |
| 79 | +}: { |
| 80 | + initial: string; |
| 81 | + report: (count: number) => void; |
| 82 | +}) { |
| 83 | + const [rules, setRules] = React.useState<ConditionalFormattingRuleDraft[]>([{ condition: initial, style: {} }]); |
| 84 | + return ( |
| 85 | + <ConditionalFormattingEditor |
| 86 | + rules={rules} |
| 87 | + onChange={setRules} |
| 88 | + objectName="ticket" |
| 89 | + fieldNames={Object.keys(TICKET_FIELDS)} |
| 90 | + fieldDefs={FIELD_DEFS} |
| 91 | + t={t} |
| 92 | + onBlockingIssuesChange={report} |
| 93 | + /> |
| 94 | + ); |
| 95 | +} |
| 96 | + |
| 97 | +/** Type `source` into the first rule's condition inside `scope`. */ |
| 98 | +function typeCondition(source: string, scope: HTMLElement = document.body) { |
| 99 | + const box = within(scope).getByTestId('cf-rule-0').querySelector('textarea') as HTMLElement; |
| 100 | + fireEvent.change(box, { target: { value: source } }); |
| 101 | +} |
| 102 | + |
| 103 | +describe('ConditionalFormattingEditor — list-equality while typing (#36459)', () => { |
| 104 | + it('refuses == on a select declared multiple: true, with the membership rewrite, and reports it as blocking', async () => { |
| 105 | + const report = vi.fn(); |
| 106 | + render(<EditorHarness initial="" report={report} />); |
| 107 | + typeCondition("record.tags == 'vip'"); |
| 108 | + await waitFor(() => expect(report.mock.calls.at(-1)?.[0]).toBe(1), { timeout: 5000 }); |
| 109 | + const finding = await screen.findByText(LIST_FINDING); |
| 110 | + expect(finding.textContent).toMatch(/\bin\b/); |
| 111 | + }); |
| 112 | + |
| 113 | + it('refuses != on the same list field', async () => { |
| 114 | + const report = vi.fn(); |
| 115 | + render(<EditorHarness initial="" report={report} />); |
| 116 | + typeCondition("record.tags != 'vip'"); |
| 117 | + await waitFor(() => expect(report.mock.calls.at(-1)?.[0]).toBe(1), { timeout: 5000 }); |
| 118 | + expect(await screen.findByText(LIST_FINDING)).toBeInTheDocument(); |
| 119 | + }); |
| 120 | + |
| 121 | + it('accepts the membership test that does match a list', async () => { |
| 122 | + const report = vi.fn(); |
| 123 | + render(<EditorHarness initial="" report={report} />); |
| 124 | + typeCondition("'vip' in record.tags"); |
| 125 | + await waitFor(() => expect(report.mock.calls.at(-1)?.[0]).toBe(0), { timeout: 5000 }); |
| 126 | + expect(screen.queryByText(/holds a list/)).toBeNull(); |
| 127 | + }); |
| 128 | + |
| 129 | + it('CONTROL — == on a single-value select is clean', async () => { |
| 130 | + const report = vi.fn(); |
| 131 | + render(<EditorHarness initial="" report={report} />); |
| 132 | + typeCondition("record.stage == 'open'"); |
| 133 | + await waitFor(() => expect(report.mock.calls.at(-1)?.[0]).toBe(0), { timeout: 5000 }); |
| 134 | + expect(screen.queryByText(/holds a list/)).toBeNull(); |
| 135 | + }); |
| 136 | +}); |
| 137 | + |
| 138 | +describe('both mount sites hand the editor the object’s field definitions (#36459)', () => { |
| 139 | + it('the view variant inspector refuses == on a list field in a formatting rule', async () => { |
| 140 | + function Harness() { |
| 141 | + const [draft, setDraft] = React.useState<Record<string, unknown>>({ |
| 142 | + name: 'tickets', |
| 143 | + object: 'ticket', |
| 144 | + list: { |
| 145 | + type: 'grid', |
| 146 | + data: { provider: 'object', object: 'ticket' }, |
| 147 | + columns: ['name'], |
| 148 | + conditionalFormatting: [{ condition: '', style: {} }], |
| 149 | + }, |
| 150 | + }); |
| 151 | + return ( |
| 152 | + <ViewVariantInspector |
| 153 | + type="view" |
| 154 | + name="tickets" |
| 155 | + draft={draft} |
| 156 | + onPatch={(patch: Record<string, unknown>) => setDraft((d) => ({ ...d, ...patch }))} |
| 157 | + onSelectionChange={() => {}} |
| 158 | + onClearSelection={() => {}} |
| 159 | + readOnly={false} |
| 160 | + locale={'en-US'} |
| 161 | + variantKey="list" |
| 162 | + familyKey="list" |
| 163 | + isHome={false} |
| 164 | + objectFieldsOverride={mapObjectFields(TICKET as never)} |
| 165 | + /> |
| 166 | + ); |
| 167 | + } |
| 168 | + render(<Harness />); |
| 169 | + const editor = await screen.findByTestId('cf-editor'); |
| 170 | + typeCondition("record.tags == 'vip'", editor); |
| 171 | + expect(await within(editor).findByText(LIST_FINDING, {}, { timeout: 5000 })).toBeInTheDocument(); |
| 172 | + }); |
| 173 | + |
| 174 | + it('the Studio list-view panel refuses == on a list field, and holds the autosave', async () => { |
| 175 | + const report = vi.fn(); |
| 176 | + function Harness() { |
| 177 | + const [body, setBody] = React.useState<Record<string, unknown>>({ |
| 178 | + type: 'grid', |
| 179 | + columns: ['name'], |
| 180 | + conditionalFormatting: [{ condition: '', style: {} }], |
| 181 | + }); |
| 182 | + return ( |
| 183 | + <ListViewMoreSection |
| 184 | + body={body} |
| 185 | + writeBody={setBody} |
| 186 | + objectName="ticket" |
| 187 | + fieldNames={Object.keys(TICKET_FIELDS)} |
| 188 | + fieldCatalog={loaded(FIELD_DEFS.map((f) => ({ ...f, multiple: f.multiple === true || undefined })))} |
| 189 | + readOnly={false} |
| 190 | + locale={'en-US'} |
| 191 | + onBlockingIssuesChange={report} |
| 192 | + /> |
| 193 | + ); |
| 194 | + } |
| 195 | + render(<Harness />); |
| 196 | + const editor = screen.getByTestId('cf-editor'); |
| 197 | + typeCondition("record.tags == 'vip'", editor); |
| 198 | + await waitFor(() => expect(report.mock.calls.at(-1)?.[0]).toBe(1), { timeout: 5000 }); |
| 199 | + expect(within(editor).getByText(LIST_FINDING)).toBeInTheDocument(); |
| 200 | + }); |
| 201 | +}); |
0 commit comments