|
| 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 | + * An untyped `repeater` field shows and edits its stored rows in the record |
| 11 | + * Edit dialog. |
| 12 | + * |
| 13 | + * `@objectstack/spec` declares `repeater` an untyped array of embedded JSON |
| 14 | + * objects: `FieldSchema` has no sub-field or column member, so nothing a widget |
| 15 | + * could read names the keys of a row. The alias table used to send it to the |
| 16 | + * line-items grid (`field:grid`), which draws only declared columns, so a |
| 17 | + * stored `[{label:'one',qty:1},…]` drew numbered rows with Duplicate and |
| 18 | + * Remove and no cell: the content could be neither read nor changed. It now |
| 19 | + * takes the JSON editor `composite` and `record` beside it use. |
| 20 | + * |
| 21 | + * Driven through the dialog the console mounts for Edit — the global |
| 22 | + * `ModalForm`, whose body is a real `SchemaRenderer` form — with the real |
| 23 | + * registered widgets and a click on the real submit button. |
| 24 | + * |
| 25 | + * The control: a line-items grid that DECLARES its columns still draws one |
| 26 | + * input per declared column and writes typed cells. |
| 27 | + */ |
| 28 | +import { describe, it, expect, vi } from 'vitest'; |
| 29 | +import { render, waitFor, fireEvent, screen, act } from '@testing-library/react'; |
| 30 | +import React from 'react'; |
| 31 | + |
| 32 | +import { registerAllFields, GridField, resolveFormWidgetType } from '@object-ui/fields'; |
| 33 | +import { ModalForm } from './ModalForm'; |
| 34 | + |
| 35 | +registerAllFields(); |
| 36 | + |
| 37 | +const ROWS = [ |
| 38 | + { label: 'one', qty: 1 }, |
| 39 | + { label: 'two', qty: 2 }, |
| 40 | +]; |
| 41 | + |
| 42 | +const STORED = { |
| 43 | + id: 'z1', |
| 44 | + name: 'Specimen — Full', |
| 45 | + f_repeater: ROWS, |
| 46 | +}; |
| 47 | + |
| 48 | +function makeDS() { |
| 49 | + return { |
| 50 | + getObjectSchema: vi.fn().mockResolvedValue({ |
| 51 | + name: 'field_zoo', |
| 52 | + fields: { |
| 53 | + name: { type: 'text', label: 'Name' }, |
| 54 | + f_repeater: { type: 'repeater', label: 'Repeater (embedded array)' }, |
| 55 | + }, |
| 56 | + }), |
| 57 | + findOne: vi.fn().mockResolvedValue(structuredClone(STORED)), |
| 58 | + find: vi.fn().mockResolvedValue({ data: [] }), |
| 59 | + create: vi.fn(async (_o: string, d: any) => ({ id: 'n1', ...d })), |
| 60 | + update: vi.fn(async (_o: string, _id: string, d: any) => ({ ...STORED, ...d })), |
| 61 | + }; |
| 62 | +} |
| 63 | + |
| 64 | +function openEditDialog() { |
| 65 | + const ds = makeDS(); |
| 66 | + render( |
| 67 | + <ModalForm |
| 68 | + schema={{ |
| 69 | + type: 'object-form', |
| 70 | + formType: 'modal', |
| 71 | + objectName: 'field_zoo', |
| 72 | + mode: 'edit', |
| 73 | + recordId: 'z1', |
| 74 | + open: true, |
| 75 | + } as any} |
| 76 | + dataSource={ds as any} |
| 77 | + />, |
| 78 | + ); |
| 79 | + return ds; |
| 80 | +} |
| 81 | + |
| 82 | +const repeaterBox = () => |
| 83 | + waitFor(() => { |
| 84 | + const el = document.querySelector('textarea[name="f_repeater"]') as HTMLTextAreaElement | null; |
| 85 | + if (!el) throw new Error('f_repeater not ready'); |
| 86 | + return el; |
| 87 | + }); |
| 88 | + |
| 89 | +const type = async (el: HTMLElement, text: string) => { |
| 90 | + await act(async () => { |
| 91 | + fireEvent.change(el, { target: { value: text } }); |
| 92 | + }); |
| 93 | +}; |
| 94 | + |
| 95 | +const clickUpdate = async () => { |
| 96 | + await act(async () => { |
| 97 | + fireEvent.click(screen.getByRole('button', { name: 'Update' })); |
| 98 | + }); |
| 99 | + await act(async () => { |
| 100 | + await new Promise((resolve) => setTimeout(resolve, 30)); |
| 101 | + }); |
| 102 | +}; |
| 103 | + |
| 104 | +const row = (name: string) => document.querySelector(`[data-field="${name}"]`) as HTMLElement | null; |
| 105 | + |
| 106 | +const payloads = (ds: ReturnType<typeof makeDS>) => ds.update.mock.calls.map((call) => call[2]); |
| 107 | + |
| 108 | +describe('an untyped repeater field in the record Edit dialog', () => { |
| 109 | + it('shows the stored rows: "one", "two", 1 and 2 are on screen', async () => { |
| 110 | + openEditDialog(); |
| 111 | + const box = await repeaterBox(); |
| 112 | + await waitFor(() => expect(box.value).toBe(JSON.stringify(ROWS, null, 2))); |
| 113 | + for (const shown of ['"one"', '"two"', '"qty": 1', '"qty": 2']) { |
| 114 | + expect(box.value).toContain(shown); |
| 115 | + } |
| 116 | + // Not the bare line-items grid it used to draw. |
| 117 | + expect(row('f_repeater')?.querySelector('[data-testid="line-items"]')).toBeNull(); |
| 118 | + }); |
| 119 | + |
| 120 | + it('saves an edit as an array of objects, never a string', async () => { |
| 121 | + const ds = openEditDialog(); |
| 122 | + const box = await repeaterBox(); |
| 123 | + await waitFor(() => expect(box.value).toContain('"one"')); |
| 124 | + const next = [ |
| 125 | + { label: 'one', qty: 5 }, |
| 126 | + { label: 'three', qty: 3 }, |
| 127 | + ]; |
| 128 | + await type(box, JSON.stringify(next)); |
| 129 | + await clickUpdate(); |
| 130 | + await waitFor(() => expect(ds.update).toHaveBeenCalledTimes(1)); |
| 131 | + const sent = payloads(ds)[0].f_repeater; |
| 132 | + expect(Array.isArray(sent)).toBe(true); |
| 133 | + expect(sent).toEqual(next); |
| 134 | + expect(typeof sent[0].qty).toBe('number'); |
| 135 | + }); |
| 136 | + |
| 137 | + it('an untouched save never writes the field as text', async () => { |
| 138 | + const ds = openEditDialog(); |
| 139 | + await repeaterBox(); |
| 140 | + await clickUpdate(); |
| 141 | + await waitFor(() => expect(ds.update).toHaveBeenCalledTimes(1)); |
| 142 | + const sent = payloads(ds)[0]; |
| 143 | + // Present or absent are both safe; a string in this slot is the defect. |
| 144 | + if ('f_repeater' in sent) expect(sent.f_repeater).toEqual(ROWS); |
| 145 | + expect(typeof sent.f_repeater === 'string').toBe(false); |
| 146 | + }); |
| 147 | + |
| 148 | + it('CONTROL — a grid that declares columns still draws the line-items grid', async () => { |
| 149 | + // `grid` is objectui's own line-items key (`GridFieldMetadata`), not a spec |
| 150 | + // field type, so it is driven at the widget the form resolves for it. |
| 151 | + expect(resolveFormWidgetType('grid')).toBe('grid'); |
| 152 | + const onChange = vi.fn(); |
| 153 | + render( |
| 154 | + <GridField |
| 155 | + field={{ |
| 156 | + name: 'f_lines', |
| 157 | + columns: [ |
| 158 | + { name: 'item', label: 'Item', type: 'text' }, |
| 159 | + { name: 'qty', label: 'Qty', type: 'number' }, |
| 160 | + ], |
| 161 | + } as any} |
| 162 | + value={[{ item: 'bolt', qty: 3 }]} |
| 163 | + onChange={onChange} |
| 164 | + />, |
| 165 | + ); |
| 166 | + const grid = screen.getByTestId('line-items'); |
| 167 | + expect((grid.querySelector('input[aria-label="Item"]') as HTMLInputElement).value).toBe('bolt'); |
| 168 | + const qty = grid.querySelector('input[aria-label="Qty"]') as HTMLInputElement; |
| 169 | + expect(qty.value).toBe('3'); |
| 170 | + await act(async () => { |
| 171 | + fireEvent.change(qty, { target: { value: '4' } }); |
| 172 | + }); |
| 173 | + expect(onChange).toHaveBeenLastCalledWith([{ item: 'bolt', qty: 4 }]); |
| 174 | + }); |
| 175 | +}); |
0 commit comments