|
| 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 | + * objectui#7333 — an EMPTY host `data` array is "no host rows yet", not "the |
| 11 | + * host owns zero rows". |
| 12 | + * |
| 13 | + * `ObjectGantt` read a host `data` prop at two sites, and both took `[]` as an |
| 14 | + * authoritative answer: `reload`'s short-circuit (`data && Array.isArray(data)` |
| 15 | + * — `[]` is truthy) adopted it and returned before the chart's own query, and |
| 16 | + * the gating flag (`Array.isArray(data)`) switched off the object-schema gate |
| 17 | + * and the invalidation subscription that belong to that query. So a gantt whose |
| 18 | + * `data` names its own source painted an EMPTY chart the moment a host handed |
| 19 | + * it an array it had not filled yet. |
| 20 | + * |
| 21 | + * Both sites now read one predicate, `hostRows` in `ObjectGantt.tsx`: a |
| 22 | + * non-empty host array is adopted as it always was, and an empty one leaves the |
| 23 | + * chart reading from its own source, exactly as with no host array at all. |
| 24 | + * |
| 25 | + * ⚠️ Reach: the registered `object-gantt` renderer forwards no host prop, which |
| 26 | + * `ObjectGantt.hostDataProp-7210.test.tsx` pins, and `ObjectGanttProps` does |
| 27 | + * not declare `data`. So every case here renders `ObjectGantt` directly and |
| 28 | + * spreads the prop in, as a direct caller would have to. |
| 29 | + * |
| 30 | + * Cases 1 and 4 are the pins; 2 and 3 are their controls, and 5 is the "yet": |
| 31 | + * 1. api provider + host `[]` ⇒ the endpoint is read and its rows paint; |
| 32 | + * 2. api provider + host rows ⇒ those rows paint, the endpoint is never read; |
| 33 | + * 3. api provider + no host `data` ⇒ the endpoint is read; |
| 34 | + * 4. object provider + host `[]` ⇒ the query waits for the object schema and |
| 35 | + * goes out ONCE, already expanded (the gating flag's half); |
| 36 | + * 5. host rows that arrive after an empty first render are adopted. |
| 37 | + */ |
| 38 | + |
| 39 | +import React from 'react'; |
| 40 | +import { render, screen, waitFor, act } from '@testing-library/react'; |
| 41 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 42 | +import { SchemaRendererProvider } from '@object-ui/react'; |
| 43 | +import { ObjectGantt } from './ObjectGantt'; |
| 44 | + |
| 45 | +vi.mock('sonner', () => ({ toast: { error: vi.fn() } })); |
| 46 | + |
| 47 | +vi.mock('./GanttView', () => ({ |
| 48 | + GanttView: ({ tasks }: any) => ( |
| 49 | + <div data-testid="gantt-view" data-task-count={String(tasks.length)}> |
| 50 | + {tasks.map((t: any) => ( |
| 51 | + <div key={t.id} data-testid={`gv-task-${t.id}`}>{t.title}</div> |
| 52 | + ))} |
| 53 | + </div> |
| 54 | + ), |
| 55 | +})); |
| 56 | + |
| 57 | +vi.mock('@object-ui/plugin-detail', async (importOriginal) => ({ |
| 58 | + ...(await importOriginal<typeof import('@object-ui/plugin-detail')>()), |
| 59 | + RecordDetailDrawer: () => null, |
| 60 | + deriveRecordPageHref: () => null, |
| 61 | +})); |
| 62 | + |
| 63 | +/** What the gantt's own `api` endpoint answers with. */ |
| 64 | +const ENDPOINT_ROWS = [ |
| 65 | + { id: 'e1', name: 'Endpoint task 1', start_date: '2026-01-01', end_date: '2026-01-05' }, |
| 66 | + { id: 'e2', name: 'Endpoint task 2', start_date: '2026-01-02', end_date: '2026-01-06' }, |
| 67 | +]; |
| 68 | + |
| 69 | +/** What a host hands down as `data` when it DOES have rows. */ |
| 70 | +const HOST_ROWS = [ |
| 71 | + { id: 'h1', name: 'Host task 1', start_date: '2026-02-01', end_date: '2026-02-03' }, |
| 72 | +]; |
| 73 | + |
| 74 | +const ENDPOINT = '/api/gantt/tree'; |
| 75 | + |
| 76 | +function apiSchema(): any { |
| 77 | + return { |
| 78 | + type: 'object-gantt', |
| 79 | + objectName: 'tasks', |
| 80 | + gantt: { titleField: 'name', startDateField: 'start_date', endDateField: 'end_date' }, |
| 81 | + data: { provider: 'api', read: { url: ENDPOINT, method: 'GET' } }, |
| 82 | + }; |
| 83 | +} |
| 84 | + |
| 85 | +function makeApiFetch() { |
| 86 | + return vi.fn(async () => |
| 87 | + new Response(JSON.stringify({ data: ENDPOINT_ROWS, total: ENDPOINT_ROWS.length }), { |
| 88 | + status: 200, |
| 89 | + headers: { 'Content-Type': 'application/json' }, |
| 90 | + }), |
| 91 | + ); |
| 92 | +} |
| 93 | + |
| 94 | +/** The URLs the endpoint was read at, in order. */ |
| 95 | +function readUrls(apiFetch: ReturnType<typeof makeApiFetch>): string[] { |
| 96 | + return (apiFetch.mock.calls as unknown as Array<[unknown]>).map(([url]) => String(url)); |
| 97 | +} |
| 98 | + |
| 99 | +/** Renders an api-provider gantt, spreading `hostProps` in as a direct caller would. */ |
| 100 | +function renderApiGantt(apiFetch: ReturnType<typeof makeApiFetch>, hostProps: Record<string, unknown>) { |
| 101 | + const tree = (props: Record<string, unknown>) => ( |
| 102 | + <SchemaRendererProvider dataSource={null} apiFetch={apiFetch as any}> |
| 103 | + <ObjectGantt schema={apiSchema()} {...(props as any)} /> |
| 104 | + </SchemaRendererProvider> |
| 105 | + ); |
| 106 | + const utils = render(tree(hostProps)); |
| 107 | + return { ...utils, rerenderWith: (props: Record<string, unknown>) => utils.rerender(tree(props)) }; |
| 108 | +} |
| 109 | + |
| 110 | +const taskCount = () => screen.getByTestId('gantt-view').getAttribute('data-task-count'); |
| 111 | + |
| 112 | +/** Lets every in-flight read settle, so a query that would follow is counted. */ |
| 113 | +const settle = () => act(() => new Promise<void>((resolve) => setTimeout(resolve, 100))); |
| 114 | + |
| 115 | +describe('objectui#7333 — an api-provider gantt handed an EMPTY host array still reads its endpoint', () => { |
| 116 | + // No test here may reach the network through the bare global fetch. |
| 117 | + const bareFetch = vi.fn(async () => new Response('{}', { status: 599 })); |
| 118 | + |
| 119 | + beforeEach(() => { |
| 120 | + bareFetch.mockClear(); |
| 121 | + vi.stubGlobal('fetch', bareFetch); |
| 122 | + }); |
| 123 | + |
| 124 | + afterEach(() => { |
| 125 | + vi.unstubAllGlobals(); |
| 126 | + }); |
| 127 | + |
| 128 | + it('host `data: []` ⇒ the endpoint is read and its rows paint', async () => { |
| 129 | + const apiFetch = makeApiFetch(); |
| 130 | + |
| 131 | + renderApiGantt(apiFetch, { data: [] }); |
| 132 | + |
| 133 | + await waitFor(() => expect(screen.getByTestId('gv-task-e2')).toBeTruthy()); |
| 134 | + expect(taskCount()).toBe(String(ENDPOINT_ROWS.length)); |
| 135 | + expect(readUrls(apiFetch).some((url) => url.includes(ENDPOINT))).toBe(true); |
| 136 | + expect(bareFetch).not.toHaveBeenCalled(); |
| 137 | + }); |
| 138 | + |
| 139 | + it('CONTROL — host `data` with rows ⇒ those rows paint and the endpoint is never read', async () => { |
| 140 | + const apiFetch = makeApiFetch(); |
| 141 | + |
| 142 | + renderApiGantt(apiFetch, { data: HOST_ROWS }); |
| 143 | + |
| 144 | + await waitFor(() => expect(screen.getByTestId('gv-task-h1')).toBeTruthy()); |
| 145 | + expect(taskCount()).toBe(String(HOST_ROWS.length)); |
| 146 | + expect(screen.queryByTestId('gv-task-e1')).toBeNull(); |
| 147 | + expect(apiFetch).not.toHaveBeenCalled(); |
| 148 | + }); |
| 149 | + |
| 150 | + it('CONTROL — no host `data` ⇒ the endpoint is read', async () => { |
| 151 | + const apiFetch = makeApiFetch(); |
| 152 | + |
| 153 | + renderApiGantt(apiFetch, {}); |
| 154 | + |
| 155 | + await waitFor(() => expect(screen.getByTestId('gv-task-e2')).toBeTruthy()); |
| 156 | + expect(readUrls(apiFetch).some((url) => url.includes(ENDPOINT))).toBe(true); |
| 157 | + }); |
| 158 | + |
| 159 | + it('host rows that arrive after an empty first render are adopted', async () => { |
| 160 | + const apiFetch = makeApiFetch(); |
| 161 | + |
| 162 | + const { rerenderWith } = renderApiGantt(apiFetch, { data: [] }); |
| 163 | + await waitFor(() => expect(screen.getByTestId('gv-task-e2')).toBeTruthy()); |
| 164 | + |
| 165 | + rerenderWith({ data: HOST_ROWS }); |
| 166 | + |
| 167 | + await waitFor(() => expect(screen.getByTestId('gv-task-h1')).toBeTruthy()); |
| 168 | + expect(taskCount()).toBe(String(HOST_ROWS.length)); |
| 169 | + expect(screen.queryByTestId('gv-task-e1')).toBeNull(); |
| 170 | + }); |
| 171 | +}); |
| 172 | + |
| 173 | +describe('objectui#7333 — an EMPTY host array does not switch off the object-schema gate', () => { |
| 174 | + /** A definition with a lookup, so the gated query has a real `$expand` to carry. */ |
| 175 | + const OBJECT_DEF = { |
| 176 | + name: 'task', |
| 177 | + fields: { |
| 178 | + id: { name: 'id', type: 'text' }, |
| 179 | + subject: { name: 'subject', type: 'text' }, |
| 180 | + owner: { name: 'owner', type: 'lookup', reference_to: 'user' }, |
| 181 | + visible_from: { name: 'visible_from', type: 'date' }, |
| 182 | + due_date: { name: 'due_date', type: 'date' }, |
| 183 | + }, |
| 184 | + }; |
| 185 | + |
| 186 | + const OBJECT_ROWS = [ |
| 187 | + { id: '1', subject: 'Renewal', owner: 'u1', visible_from: '2026-01-01', due_date: '2026-01-05' }, |
| 188 | + ]; |
| 189 | + |
| 190 | + const objectSchema: any = { |
| 191 | + type: 'object-gantt', |
| 192 | + objectName: 'task', |
| 193 | + gantt: { titleField: 'subject', startDateField: 'visible_from', endDateField: 'due_date' }, |
| 194 | + }; |
| 195 | + |
| 196 | + it('host `data: []` on an object-bound gantt ⇒ ONE query, issued after the schema settles, already expanded', async () => { |
| 197 | + const adapter = { |
| 198 | + find: vi.fn(async () => ({ data: OBJECT_ROWS, total: OBJECT_ROWS.length })), |
| 199 | + findOne: vi.fn(), |
| 200 | + create: vi.fn(), |
| 201 | + update: vi.fn(), |
| 202 | + delete: vi.fn(), |
| 203 | + getObjectSchema: vi.fn(async () => OBJECT_DEF), |
| 204 | + } as any; |
| 205 | + |
| 206 | + render(<ObjectGantt schema={objectSchema} dataSource={adapter} {...({ data: [] } as any)} />); |
| 207 | + |
| 208 | + await waitFor(() => expect(taskCount()).toBe(String(OBJECT_ROWS.length))); |
| 209 | + await waitFor(() => expect(adapter.find).toHaveBeenCalled()); |
| 210 | + await settle(); |
| 211 | + // Ungated, the first query goes out before the definition lands and |
| 212 | + // carries no expansion, and a second follows once it does: `[null, |
| 213 | + // ['owner']]` — the signature `ObjectGantt.fetchGate-7225.test.tsx` |
| 214 | + // records for the ungated regime. |
| 215 | + const expandSets = adapter.find.mock.calls.map(([, params]: [string, any]) => params?.$expand ?? null); |
| 216 | + expect(expandSets).toEqual([['owner']]); |
| 217 | + }); |
| 218 | +}); |
0 commit comments