|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { render, screen, waitFor, fireEvent } from '@testing-library/react'; |
| 3 | +import React from 'react'; |
| 4 | +import { registerAllFields } from '@object-ui/fields'; |
| 5 | +import { MasterDetailForm } from './MasterDetailForm'; |
| 6 | + |
| 7 | +// objectstack#35889 — a master-detail collection a field group of the parent |
| 8 | +// places (`fieldGroups[].relatedList`) is drawn at that group in the entry |
| 9 | +// form, as the record page draws it: after the groups declared before it, |
| 10 | +// before the groups declared after it. A collection no group places stays |
| 11 | +// below the record's fields. |
| 12 | + |
| 13 | +registerAllFields(); |
| 14 | + |
| 15 | +const LINES_GROUP = { key: 'lines', label: 'Lines', relatedList: 'po_line' }; |
| 16 | +function parentWith(fieldGroups: unknown[]) { |
| 17 | + return { |
| 18 | + name: 'po', |
| 19 | + fieldGroups, |
| 20 | + fields: { |
| 21 | + ref: { type: 'text', label: 'Ref', group: 'header' }, |
| 22 | + memo: { type: 'text', label: 'Memo', group: 'totals' }, |
| 23 | + }, |
| 24 | + }; |
| 25 | +} |
| 26 | +const HEADER_GROUP = { key: 'header', label: 'Header', fields: ['ref'] }; |
| 27 | +const TOTALS_GROUP = { key: 'totals', label: 'Totals', fields: ['memo'] }; |
| 28 | + |
| 29 | +function makeDataSource(parent: unknown, overrides: Record<string, unknown> = {}) { |
| 30 | + return { |
| 31 | + getObjectSchema: vi.fn().mockResolvedValue(parent), |
| 32 | + find: vi.fn().mockResolvedValue({ data: [] }), |
| 33 | + create: vi.fn(), |
| 34 | + update: vi.fn(), |
| 35 | + delete: vi.fn(), |
| 36 | + bulk: vi.fn(), |
| 37 | + ...overrides, |
| 38 | + } as any; |
| 39 | +} |
| 40 | + |
| 41 | +const details = [ |
| 42 | + { childObject: 'po_line', relationshipField: 'po', title: 'Order lines', columns: [{ key: 'qty', label: 'Qty', type: 'number' } as any] }, |
| 43 | +]; |
| 44 | + |
| 45 | +/** Whether `a` comes before `b` in document order. */ |
| 46 | +function before(a: Node, b: Node): boolean { |
| 47 | + return (a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING) !== 0; |
| 48 | +} |
| 49 | + |
| 50 | +async function collection(container: HTMLElement): Promise<HTMLElement> { |
| 51 | + return waitFor(() => { |
| 52 | + const el = container.querySelector('[data-testid="md-collection"]') as HTMLElement | null; |
| 53 | + if (!el) throw new Error('collection not drawn'); |
| 54 | + return el; |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +describe('MasterDetailForm — a group-placed collection sits at its group (objectstack#35889)', () => { |
| 59 | + it('simple form: between the sections of the groups around its group, inside the header form', async () => { |
| 60 | + const ds = makeDataSource(parentWith([HEADER_GROUP, LINES_GROUP, TOTALS_GROUP])); |
| 61 | + const { container } = render( |
| 62 | + <MasterDetailForm schema={{ objectName: 'po', mode: 'create', details } as any} dataSource={ds} />, |
| 63 | + ); |
| 64 | + await waitFor(() => { |
| 65 | + const el = container.querySelector('[data-form-slot] [data-testid="md-collection"]'); |
| 66 | + if (!el) throw new Error('collection not placed'); |
| 67 | + }); |
| 68 | + const placed = await collection(container); |
| 69 | + const header = container.querySelector('[data-form-section="header"]')!; |
| 70 | + const totals = container.querySelector('[data-form-section="totals"]')!; |
| 71 | + expect(header).toBeTruthy(); |
| 72 | + expect(totals).toBeTruthy(); |
| 73 | + expect(placed.closest('form')).toBe(header.closest('form')); |
| 74 | + expect(before(header, placed)).toBe(true); |
| 75 | + expect(before(placed, totals)).toBe(true); |
| 76 | + expect(placed.textContent).toContain('Order lines'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('simple form: a collection no group places stays below the record fields', async () => { |
| 80 | + const ds = makeDataSource(parentWith([HEADER_GROUP, TOTALS_GROUP])); |
| 81 | + const { container } = render( |
| 82 | + <MasterDetailForm schema={{ objectName: 'po', mode: 'create', details } as any} dataSource={ds} />, |
| 83 | + ); |
| 84 | + await waitFor(() => { |
| 85 | + if (!container.querySelector('[data-form-section="totals"]')) throw new Error('header not ready'); |
| 86 | + }); |
| 87 | + const below = await collection(container); |
| 88 | + expect(below.closest('form')).toBeNull(); |
| 89 | + expect(container.querySelector('[data-form-slot]')).toBeNull(); |
| 90 | + expect(before(container.querySelector('[data-form-section="totals"]')!, below)).toBe(true); |
| 91 | + }); |
| 92 | + |
| 93 | + it('a group placed last: after every declared group', async () => { |
| 94 | + const ds = makeDataSource(parentWith([HEADER_GROUP, TOTALS_GROUP, LINES_GROUP])); |
| 95 | + const { container } = render( |
| 96 | + <MasterDetailForm schema={{ objectName: 'po', mode: 'create', details } as any} dataSource={ds} />, |
| 97 | + ); |
| 98 | + await waitFor(() => { |
| 99 | + if (!container.querySelector('[data-form-slot] [data-testid="md-collection"]')) throw new Error('not placed'); |
| 100 | + }); |
| 101 | + const placed = await collection(container); |
| 102 | + expect(before(container.querySelector('[data-form-section="totals"]')!, placed)).toBe(true); |
| 103 | + }); |
| 104 | + |
| 105 | + it('saves the record and its lines in one batch from the placed table', async () => { |
| 106 | + const batchTransaction = vi.fn().mockResolvedValue({ results: [{ id: 'po1' }] }); |
| 107 | + const ds = makeDataSource(parentWith([HEADER_GROUP, LINES_GROUP, TOTALS_GROUP]), { batchTransaction }); |
| 108 | + const { container } = render( |
| 109 | + <MasterDetailForm schema={{ objectName: 'po', mode: 'create', details } as any} dataSource={ds} />, |
| 110 | + ); |
| 111 | + await waitFor(() => { |
| 112 | + if (!container.querySelector('[data-form-slot] [data-testid="md-collection"]')) throw new Error('not placed'); |
| 113 | + }); |
| 114 | + const ref = container.querySelector('input[name="ref"]') as HTMLInputElement; |
| 115 | + fireEvent.change(ref, { target: { value: 'PO-1' } }); |
| 116 | + fireEvent.click(screen.getByTestId('md-form-submit')); |
| 117 | + await waitFor(() => expect(batchTransaction).toHaveBeenCalledTimes(1)); |
| 118 | + const ops = batchTransaction.mock.calls[0][0] as Array<{ object?: string; objectName?: string; data?: any }>; |
| 119 | + expect(JSON.stringify(ops)).toContain('PO-1'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('tabbed form: a tab of its own at its group, labelled with the group', async () => { |
| 123 | + const ds = makeDataSource(parentWith([HEADER_GROUP, LINES_GROUP, TOTALS_GROUP])); |
| 124 | + const { container } = render( |
| 125 | + <MasterDetailForm |
| 126 | + schema={{ |
| 127 | + objectName: 'po', |
| 128 | + mode: 'create', |
| 129 | + formType: 'tabbed', |
| 130 | + sections: [ |
| 131 | + { name: 'header', label: 'Header', fields: ['ref'] }, |
| 132 | + { name: 'totals', label: 'Totals', fields: ['memo'] }, |
| 133 | + ], |
| 134 | + details, |
| 135 | + } as any} |
| 136 | + dataSource={ds} |
| 137 | + />, |
| 138 | + ); |
| 139 | + await waitFor(() => { |
| 140 | + if (!container.querySelector('[data-form-slot] [data-testid="md-collection"]')) throw new Error('not placed'); |
| 141 | + }); |
| 142 | + const tabs = screen.getAllByRole('tab').map((tab) => tab.textContent?.trim()); |
| 143 | + expect(tabs).toEqual(['Header', 'Lines', 'Totals']); |
| 144 | + const placed = await collection(container); |
| 145 | + const panel = placed.closest('[role="tabpanel"]') as HTMLElement; |
| 146 | + expect(panel).toBeTruthy(); |
| 147 | + const label = panel.getAttribute('aria-labelledby'); |
| 148 | + expect(label && document.getElementById(label)?.textContent?.trim()).toBe('Lines'); |
| 149 | + }); |
| 150 | +}); |
0 commit comments