|
| 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#25890 — the pending-changes drill-in lists what the author |
| 11 | + * changed; what the platform wrote folds into one collapsed line. |
| 12 | + * |
| 13 | + * Measured in a console walkthrough: Studio → new object 「报修工单」 (OWD |
| 14 | + * private) → Publish expanded the one object to six field rows (the five |
| 15 | + * injected system columns beside the author's `name`) and four "other |
| 16 | + * changes" (`label`, `name`, `nameField`, `sharingModel`). The `/meta` reads |
| 17 | + * serve an object with the platform's columns stamped on and its `nameField` |
| 18 | + * derived, and a NEW object has no published body to cancel them against. |
| 19 | + * |
| 20 | + * The fixture is that object as the read serves it. Its system columns are the |
| 21 | + * spec's own definitions (`injectedSystemColumnDefs`, the table the injection |
| 22 | + * spreads), relabelled the way the read localises them, so the test proves the |
| 23 | + * fold reads the `system` mark and not a label or a byte-identity. |
| 24 | + * |
| 25 | + * Controls, each a way to get the fold wrong while the headline stays green: |
| 26 | + * |
| 27 | + * - a system field CHANGED between two bodies is the author's edit: listed; |
| 28 | + * - a `nameField` naming a field the derivation would not pick is the author's |
| 29 | + * choice: listed; |
| 30 | + * - a system field an UPDATE adds is the platform's: folded; |
| 31 | + * - an author field that merely looks like an audit column (no `system`): listed. |
| 32 | + */ |
| 33 | + |
| 34 | +import '@testing-library/jest-dom/vitest'; |
| 35 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 36 | +import { cleanup, fireEvent, render, screen, within } from '@testing-library/react'; |
| 37 | +import { injectedSystemColumnDefs } from '@objectstack/spec/data'; |
| 38 | + |
| 39 | +vi.mock('@object-ui/i18n', async (importOriginal) => ({ |
| 40 | + ...(await importOriginal<Record<string, unknown>>()), |
| 41 | + useObjectTranslation: () => ({ |
| 42 | + t: (_k: string, o?: Record<string, unknown> & { defaultValue?: string }) => |
| 43 | + (o?.defaultValue ?? _k).replace(/\{\{(\w+)\}\}/g, (_m, key: string) => String(o?.[key] ?? '')), |
| 44 | + language: 'en', |
| 45 | + }), |
| 46 | +})); |
| 47 | + |
| 48 | +import { DraftChangesPanel, computeChangeDetail } from '../DraftChangesPanel'; |
| 49 | + |
| 50 | +/** The read's zh-CN labels for the injected columns (`SYSTEM_FIELD_LABELS`). */ |
| 51 | +const ZH_LABELS: Record<string, string> = { |
| 52 | + created_at: '创建时间', |
| 53 | + created_by: '创建人', |
| 54 | + owner_id: '所有者', |
| 55 | + updated_at: '更新时间', |
| 56 | + updated_by: '更新人', |
| 57 | +}; |
| 58 | + |
| 59 | +const AUTHORED = { name: 'com_test_browser_repair_ticket', label: '报修工单', sharingModel: 'private' }; |
| 60 | + |
| 61 | +/** The injected columns as the read serves them: the spec's definitions, labels localised. */ |
| 62 | +function servedSystemColumns(): Record<string, Record<string, unknown>> { |
| 63 | + const injected = injectedSystemColumnDefs({ ...AUTHORED, fields: { name: { type: 'text' } } }); |
| 64 | + const out: Record<string, Record<string, unknown>> = {}; |
| 65 | + for (const [name, def] of Object.entries(injected)) { |
| 66 | + out[name] = { ...def, ...(ZH_LABELS[name] ? { label: ZH_LABELS[name] } : {}) }; |
| 67 | + } |
| 68 | + return out; |
| 69 | +} |
| 70 | + |
| 71 | +/** The card's repro: one authored field, the platform's columns, the derived pointer. */ |
| 72 | +const REPAIR_TICKET_DRAFT = { |
| 73 | + ...AUTHORED, |
| 74 | + nameField: 'name', |
| 75 | + fields: { |
| 76 | + name: { type: 'text', label: '名称' }, |
| 77 | + ...servedSystemColumns(), |
| 78 | + // The search companion: injected AND hidden, dropped outright (objectui#13125). |
| 79 | + __search: { type: 'text', label: 'Search Index', hidden: true, readonly: true, system: true }, |
| 80 | + }, |
| 81 | +}; |
| 82 | + |
| 83 | +const SYSTEM_COLUMNS = ['created_at', 'created_by', 'owner_id', 'updated_at', 'updated_by']; |
| 84 | + |
| 85 | +afterEach(() => { |
| 86 | + cleanup(); |
| 87 | + vi.unstubAllGlobals(); |
| 88 | +}); |
| 89 | + |
| 90 | +describe('computeChangeDetail — what the platform wrote is not an author row (objectstack#25890)', () => { |
| 91 | + it('the fixture carries the five visible system columns the card measured', () => { |
| 92 | + // Guards the fixture: if the injection table moves, this test says so here. |
| 93 | + const visible = Object.entries(REPAIR_TICKET_DRAFT.fields) |
| 94 | + .filter(([, def]) => (def as { system?: unknown }).system === true && !(def as { hidden?: unknown }).hidden) |
| 95 | + .map(([name]) => name) |
| 96 | + .sort(); |
| 97 | + expect(visible).toEqual(SYSTEM_COLUMNS); |
| 98 | + }); |
| 99 | + |
| 100 | + it("a NEW object's author rows are its authored field and keys; the system columns and nameField are generated", () => { |
| 101 | + const d = computeChangeDetail(null, REPAIR_TICKET_DRAFT); |
| 102 | + expect(d.isNew).toBe(true); |
| 103 | + expect(d.fields?.added).toEqual(['name']); |
| 104 | + expect(d.fields?.changed).toEqual([]); |
| 105 | + expect(d.fields?.removed).toEqual([]); |
| 106 | + expect(d.keys).toEqual({ added: ['label', 'name', 'sharingModel'], changed: [], removed: [] }); |
| 107 | + expect(d.generated).toEqual([ |
| 108 | + ...SYSTEM_COLUMNS.map((name) => ({ name, kind: 'added', scope: 'field' })), |
| 109 | + { name: 'nameField', kind: 'added', scope: 'key' }, |
| 110 | + ]); |
| 111 | + // A generated field keeps its label, for the row the group shows when opened. |
| 112 | + expect(d.fields?.labels.get('owner_id')).toBe('所有者'); |
| 113 | + }); |
| 114 | + |
| 115 | + it('CONTROL: a field shaped like an audit column without the system mark is the author’s', () => { |
| 116 | + const draft = { name: 'x', fields: { created_on: { type: 'datetime', label: 'Created On', readonly: true } } }; |
| 117 | + const d = computeChangeDetail(null, draft); |
| 118 | + expect(d.fields?.added).toEqual(['created_on']); |
| 119 | + expect(d.generated).toEqual([]); |
| 120 | + }); |
| 121 | + |
| 122 | + it('an UPDATE folds a system column it adds, and lists a system column the author changed', () => { |
| 123 | + const cols = servedSystemColumns(); |
| 124 | + const published = { |
| 125 | + name: 'ticket', |
| 126 | + fields: { subject: { type: 'text' }, created_at: cols.created_at }, |
| 127 | + }; |
| 128 | + const draft = { |
| 129 | + name: 'ticket', |
| 130 | + fields: { |
| 131 | + subject: { type: 'text' }, |
| 132 | + // Relabelled by the author: the same column on both reads, so the difference is an edit. |
| 133 | + created_at: { ...cols.created_at, label: 'Opened At' }, |
| 134 | + // Newly provisioned (e.g. the OWD now asks for an owner): the platform's. |
| 135 | + owner_id: cols.owner_id, |
| 136 | + }, |
| 137 | + }; |
| 138 | + const d = computeChangeDetail(published, draft); |
| 139 | + expect(d.isNew).toBe(false); |
| 140 | + expect(d.fields?.added).toEqual([]); |
| 141 | + expect(d.fields?.changed).toEqual([{ name: 'created_at', keys: ['label'] }]); |
| 142 | + expect(d.generated).toEqual([{ name: 'owner_id', kind: 'added', scope: 'field' }]); |
| 143 | + }); |
| 144 | + |
| 145 | + it('CONTROL: a nameField the derivation would not pick is the author’s choice and is listed', () => { |
| 146 | + const fields = { name: { type: 'text' }, subject: { type: 'text' } }; |
| 147 | + const published = { name: 'ticket', nameField: 'name', fields }; |
| 148 | + const draft = { name: 'ticket', nameField: 'subject', fields }; |
| 149 | + const d = computeChangeDetail(published, draft); |
| 150 | + expect(d.keys.changed).toEqual(['nameField']); |
| 151 | + expect(d.generated).toEqual([]); |
| 152 | + }); |
| 153 | + |
| 154 | + it('a nameField moving between two derived answers is the platform’s', () => { |
| 155 | + const published = { name: 'ticket', nameField: 'name', fields: { name: { type: 'text' } } }; |
| 156 | + const draft = { name: 'ticket', nameField: 'subject', fields: { subject: { type: 'text' } } }; |
| 157 | + const d = computeChangeDetail(published, draft); |
| 158 | + expect(d.keys.changed).toEqual([]); |
| 159 | + expect(d.generated).toEqual([{ name: 'nameField', kind: 'changed', scope: 'key' }]); |
| 160 | + }); |
| 161 | +}); |
| 162 | + |
| 163 | +/* ─────────────── the panel ─────────────── */ |
| 164 | + |
| 165 | +function stubServer() { |
| 166 | + const ok = (body: unknown) => ({ ok: true, status: 200, json: async () => body }); |
| 167 | + vi.stubGlobal( |
| 168 | + 'fetch', |
| 169 | + vi.fn(async (input: RequestInfo | URL) => { |
| 170 | + const url = String(input); |
| 171 | + if (url.includes('/_drafts')) { |
| 172 | + return ok({ drafts: [{ type: 'object', name: AUTHORED.name, packageId: 'com.x', label: AUTHORED.label }] }); |
| 173 | + } |
| 174 | + if (/\/meta\/object(\?|$)/.test(url)) return ok([]); |
| 175 | + if (url.includes(`/meta/object/${AUTHORED.name}`) && url.includes('state=draft')) { |
| 176 | + return ok({ item: REPAIR_TICKET_DRAFT }); |
| 177 | + } |
| 178 | + return { ok: false, status: 404, json: async () => ({}) }; |
| 179 | + }) as unknown as typeof fetch, |
| 180 | + ); |
| 181 | +} |
| 182 | + |
| 183 | +async function expandRepairTicket(): Promise<HTMLElement> { |
| 184 | + stubServer(); |
| 185 | + render(<DraftChangesPanel open onOpenChange={() => {}} packageId="com.x" />); |
| 186 | + const toggle = (await screen.findByText(AUTHORED.label)).closest('[data-testid="draft-entry-toggle"]') as HTMLElement; |
| 187 | + fireEvent.click(toggle); |
| 188 | + return screen.findByTestId('draft-entry-detail'); |
| 189 | +} |
| 190 | + |
| 191 | +describe("the card's repro in the sheet (objectstack#25890)", () => { |
| 192 | + it('shows the one authored field and one collapsed line for what the platform wrote', async () => { |
| 193 | + const detail = await expandRepairTicket(); |
| 194 | + const rows = within(detail).getAllByTestId('draft-field-row'); |
| 195 | + expect(rows.map((r) => r.textContent)).toEqual(['+ 名称 name']); |
| 196 | + const toggle = within(detail).getByTestId('draft-generated-toggle'); |
| 197 | + expect(toggle).toHaveTextContent('Generated by the platform (6)'); |
| 198 | + expect(toggle).toHaveAttribute('aria-expanded', 'false'); |
| 199 | + const text = detail.textContent ?? ''; |
| 200 | + for (const name of [...SYSTEM_COLUMNS, '__search', 'nameField']) expect(text).not.toContain(name); |
| 201 | + }); |
| 202 | + |
| 203 | + it('heads a NEW item’s keys as what it includes, not as other changes', async () => { |
| 204 | + const detail = await expandRepairTicket(); |
| 205 | + expect(within(detail).getByText('Includes:')).toBeInTheDocument(); |
| 206 | + expect(within(detail).queryByText('Also changed:')).toBeNull(); |
| 207 | + expect(within(detail).getByText('+ sharingModel')).toBeInTheDocument(); |
| 208 | + }); |
| 209 | + |
| 210 | + it('opens the group to the five system columns, label first, and the derived nameField', async () => { |
| 211 | + const detail = await expandRepairTicket(); |
| 212 | + const toggle = within(detail).getByTestId('draft-generated-toggle'); |
| 213 | + fireEvent.click(toggle); |
| 214 | + expect(toggle).toHaveAttribute('aria-expanded', 'true'); |
| 215 | + const group = within(detail).getByTestId('draft-generated-group'); |
| 216 | + const fieldRows = within(group).getAllByTestId('draft-field-row').map((r) => r.textContent); |
| 217 | + expect(fieldRows).toEqual(SYSTEM_COLUMNS.map((name) => `+ ${ZH_LABELS[name]} ${name}`)); |
| 218 | + expect(within(group).getByText('+ nameField')).toBeInTheDocument(); |
| 219 | + expect(group.textContent ?? '').not.toContain('__search'); |
| 220 | + }); |
| 221 | +}); |
0 commit comments