|
| 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#34605 — the form renderer's half of the section navigation: the |
| 11 | + * section list it reports to a host through `FormSectionNavContext`. |
| 12 | + * |
| 13 | + * Pinned: each drawn heading row carries a stable DOM id and is reported with |
| 14 | + * it, in order; a heading its predicate hides is not reported; a headingless |
| 15 | + * section is not reported and its fields count for no heading; a refused |
| 16 | + * submit counts each section's errored fields; a tabbed form and a form |
| 17 | + * nested in another report nothing; unmounting reports `null`. |
| 18 | + */ |
| 19 | + |
| 20 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 21 | +import { render, cleanup, fireEvent, waitFor } from '@testing-library/react'; |
| 22 | +import React from 'react'; |
| 23 | +import { ComponentRegistry } from '@object-ui/core'; |
| 24 | +// Module-scope import (not `beforeAll`) — objectui#3010/#3021. |
| 25 | +import '../../../renderers'; |
| 26 | +// The renderers reach `react-runtime` through a dynamic import: pay its load at collection. |
| 27 | +import '@object-ui/react-runtime'; |
| 28 | +import { FormSectionNavContext, type FormSectionNavItem } from '../section-nav-context'; |
| 29 | + |
| 30 | +afterEach(cleanup); |
| 31 | + |
| 32 | +const cel = (source: string) => ({ dialect: 'cel', source }); |
| 33 | + |
| 34 | +const divider = (name: string, label: string | undefined, fields: string[], extra: Record<string, unknown> = {}) => ({ |
| 35 | + name, |
| 36 | + type: 'section-divider', |
| 37 | + label, |
| 38 | + fields, |
| 39 | + ...extra, |
| 40 | +}); |
| 41 | + |
| 42 | +const SECTIONED = [ |
| 43 | + divider('s_overview', 'Overview', ['name']), |
| 44 | + { name: 'name', label: 'Name', type: 'input' }, |
| 45 | + divider('s_forecast', 'Forecast', ['forecast', 'quarter']), |
| 46 | + { name: 'forecast', label: 'Forecast Category', type: 'input', required: true }, |
| 47 | + { name: 'quarter', label: 'Quarter', type: 'input', required: true }, |
| 48 | + divider('s_hidden', 'Won Details', ['won_note'], { visibleWhen: cel("record.name == 'won'") }), |
| 49 | + { name: 'won_note', label: 'Won Note', type: 'input' }, |
| 50 | + divider('s_untitled', undefined, ['loose']), |
| 51 | + { name: 'loose', label: 'Loose', type: 'input', required: true }, |
| 52 | +]; |
| 53 | + |
| 54 | +function renderWithSink(schema: Record<string, unknown>) { |
| 55 | + const reports: Array<FormSectionNavItem[] | null> = []; |
| 56 | + const sink = vi.fn((items: FormSectionNavItem[] | null) => { |
| 57 | + reports.push(items); |
| 58 | + }); |
| 59 | + const Form = ComponentRegistry.get('form')!; |
| 60 | + const utils = render( |
| 61 | + <FormSectionNavContext.Provider value={sink}> |
| 62 | + <Form schema={{ type: 'form', showCancel: false, ...schema }} /> |
| 63 | + </FormSectionNavContext.Provider>, |
| 64 | + ); |
| 65 | + return { ...utils, reports, sink, last: () => reports[reports.length - 1] }; |
| 66 | +} |
| 67 | + |
| 68 | +describe('form section list for the host navigation (objectstack#34605)', () => { |
| 69 | + it('reports the drawn headings in order, each with the DOM id of its row', async () => { |
| 70 | + const { last } = renderWithSink({ fields: SECTIONED }); |
| 71 | + await waitFor(() => expect(last()).toBeTruthy()); |
| 72 | + |
| 73 | + const items = last()!; |
| 74 | + expect(items.map((i) => [i.key, i.label])).toEqual([ |
| 75 | + ['s_overview', 'Overview'], |
| 76 | + ['s_forecast', 'Forecast'], |
| 77 | + ]); |
| 78 | + for (const item of items) { |
| 79 | + const row = document.getElementById(item.anchorId); |
| 80 | + expect(row).not.toBeNull(); |
| 81 | + expect(row!.getAttribute('data-form-section')).toBe(item.key); |
| 82 | + expect(row!.textContent).toBe(item.label); |
| 83 | + expect(item.errorCount).toBe(0); |
| 84 | + expect(item.collapsed).toBe(false); |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + it('adds a heading when its predicate admits it, by the verdict that draws the row', async () => { |
| 89 | + const { last } = renderWithSink({ fields: SECTIONED }); |
| 90 | + await waitFor(() => expect(last()).toBeTruthy()); |
| 91 | + fireEvent.change(document.querySelector('input[name="name"]')!, { target: { value: 'won' } }); |
| 92 | + await waitFor(() => expect(last()!.map((i) => i.key)).toEqual(['s_overview', 's_forecast', 's_hidden'])); |
| 93 | + }); |
| 94 | + |
| 95 | + it('counts the errored fields of each section after a refused submit, and none for a headingless run', async () => { |
| 96 | + const { last } = renderWithSink({ fields: SECTIONED }); |
| 97 | + await waitFor(() => expect(last()).toBeTruthy()); |
| 98 | + |
| 99 | + fireEvent.submit(document.querySelector('form')!); |
| 100 | + |
| 101 | + await waitFor(() => expect(last()!.find((i) => i.key === 's_forecast')!.errorCount).toBe(2)); |
| 102 | + expect(last()!.find((i) => i.key === 's_overview')!.errorCount).toBe(0); |
| 103 | + // `loose` holds an error too, but sits under no heading: no entry claims it. |
| 104 | + expect(last()!.reduce((n, i) => n + i.errorCount, 0)).toBe(2); |
| 105 | + |
| 106 | + fireEvent.change(document.querySelector('input[name="forecast"]')!, { target: { value: 'Commit' } }); |
| 107 | + await waitFor(() => expect(last()!.find((i) => i.key === 's_forecast')!.errorCount).toBe(1)); |
| 108 | + }); |
| 109 | + |
| 110 | + it('reports a collapsed section, and its expand unfolds it through the row toggle', async () => { |
| 111 | + const onToggle = vi.fn(); |
| 112 | + const { last } = renderWithSink({ |
| 113 | + fields: [ |
| 114 | + divider('s_a', 'A', ['a']), |
| 115 | + { name: 'a', label: 'A field', type: 'input' }, |
| 116 | + divider('s_b', 'B', ['b'], { collapsible: true, collapsed: true, onToggle }), |
| 117 | + { name: 'b', label: 'B field', type: 'input', hidden: true }, |
| 118 | + ], |
| 119 | + }); |
| 120 | + await waitFor(() => expect(last()).toBeTruthy()); |
| 121 | + const b = last()!.find((i) => i.key === 's_b')!; |
| 122 | + expect(b.collapsed).toBe(true); |
| 123 | + b.expand(); |
| 124 | + expect(onToggle).toHaveBeenCalledTimes(1); |
| 125 | + last()!.find((i) => i.key === 's_a')!.expand(); |
| 126 | + expect(onToggle).toHaveBeenCalledTimes(1); |
| 127 | + }); |
| 128 | + |
| 129 | + it('reports nothing for a tabbed form', async () => { |
| 130 | + const { reports } = renderWithSink({ |
| 131 | + fields: [ |
| 132 | + { name: 'a', label: 'A', type: 'input' }, |
| 133 | + { name: 'b', label: 'B', type: 'input' }, |
| 134 | + ], |
| 135 | + fieldTabs: [ |
| 136 | + { key: 't1', label: 'One', fields: ['a'] }, |
| 137 | + { key: 't2', label: 'Two', fields: ['b'] }, |
| 138 | + ], |
| 139 | + }); |
| 140 | + await waitFor(() => expect(document.querySelector('[data-testid="form-tab:t1"]')).not.toBeNull()); |
| 141 | + expect(reports.every((r) => r === null)).toBe(true); |
| 142 | + }); |
| 143 | + |
| 144 | + it('keeps a form nested in another out of the host sink', async () => { |
| 145 | + const { reports } = renderWithSink({ |
| 146 | + children: [{ type: 'form', showSubmit: false, showCancel: false, fields: SECTIONED }], |
| 147 | + }); |
| 148 | + await waitFor(() => expect(document.querySelectorAll('form').length).toBe(2)); |
| 149 | + expect(document.querySelector('[data-form-section="s_forecast"]')).not.toBeNull(); |
| 150 | + expect(reports.every((r) => r === null)).toBe(true); |
| 151 | + }); |
| 152 | + |
| 153 | + it('reports null when the form unmounts', async () => { |
| 154 | + const { last, unmount } = renderWithSink({ fields: SECTIONED }); |
| 155 | + await waitFor(() => expect(last()).toBeTruthy()); |
| 156 | + unmount(); |
| 157 | + expect(last()).toBeNull(); |
| 158 | + }); |
| 159 | +}); |
0 commit comments