|
| 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#10689 — `ObjectGrid`'s load effect re-reads when an input its query |
| 11 | + * reads changes, and only then. The objectui#10664 family rule: every input the |
| 12 | + * query reads is in the effect's dependencies, keyed by content. |
| 13 | + * |
| 14 | + * The query reads four schema inputs that the dependency list did not name: |
| 15 | + * |
| 16 | + * - `conditionalFormatting`, `rowActionDefs` and `bulkActionDefs`, through the |
| 17 | + * predicate-operand harvest (objectui#3501) that puts each predicate's |
| 18 | + * operands into `$select`; |
| 19 | + * - `searchableFields`, sent as `$searchFields` beside a search term. |
| 20 | + * |
| 21 | + * So a rule added to a mounted grid never had its operand fetched: the rows |
| 22 | + * kept arriving without the field, and the rule, which reads it, never matched. |
| 23 | + * |
| 24 | + * Each input is keyed by the part of it the query reads, as a string compared |
| 25 | + * by value (AGENTS.md #10): the harvested operand NAMES for the three predicate |
| 26 | + * carriers (the shape `groupingProjectionKey` already takes for `grouping`), and |
| 27 | + * the `$searchFields` the query sends. So an equal re-render, or a change the |
| 28 | + * query cannot see (a rule's colour), costs no round trip. |
| 29 | + */ |
| 30 | +import { describe, it, expect, vi, beforeAll, afterEach } from 'vitest'; |
| 31 | +import { render, waitFor, act, cleanup } from '@testing-library/react'; |
| 32 | +import '@testing-library/jest-dom'; |
| 33 | +import React from 'react'; |
| 34 | + |
| 35 | +import { ObjectGrid } from '../ObjectGrid'; |
| 36 | +import { registerAllFields } from '@object-ui/fields'; |
| 37 | + |
| 38 | +registerAllFields(); |
| 39 | + |
| 40 | +beforeAll(() => { |
| 41 | + if (!Element.prototype.scrollIntoView) { |
| 42 | + Element.prototype.scrollIntoView = vi.fn() as any; |
| 43 | + } |
| 44 | +}); |
| 45 | + |
| 46 | +afterEach(() => cleanup()); |
| 47 | + |
| 48 | +const OBJECT = 'account'; |
| 49 | + |
| 50 | +// Held as module constants so every re-render below hands the grid the SAME |
| 51 | +// references for the inputs this card does not touch. `columns` and |
| 52 | +// `pagination` are named by identity in the effect's dependency list, so a |
| 53 | +// fresh literal of either would re-read for a reason of its own. |
| 54 | +const COLUMNS = [{ field: 'name', label: 'Name' }]; |
| 55 | +const PAGINATION = { pageSize: 50 }; |
| 56 | + |
| 57 | +function makeDataSource() { |
| 58 | + const find = vi.fn(async () => ({ |
| 59 | + data: [{ id: 'r1', name: 'Acme', industry: 'Tech', owner: 'u1', status: 'open', region: 'EU' }], |
| 60 | + total: 1, |
| 61 | + hasMore: false, |
| 62 | + pageSize: 50, |
| 63 | + })); |
| 64 | + return { |
| 65 | + find, |
| 66 | + getObjectSchema: async (name: string) => ({ |
| 67 | + name, |
| 68 | + fields: { |
| 69 | + id: { type: 'text' }, |
| 70 | + name: { type: 'text' }, |
| 71 | + industry: { type: 'text' }, |
| 72 | + owner: { type: 'text' }, |
| 73 | + status: { type: 'text' }, |
| 74 | + region: { type: 'text' }, |
| 75 | + }, |
| 76 | + }), |
| 77 | + } as any; |
| 78 | +} |
| 79 | + |
| 80 | +const gridSchema = (extra: Record<string, unknown> = {}): any => ({ |
| 81 | + type: 'object-grid', |
| 82 | + objectName: OBJECT, |
| 83 | + columns: COLUMNS, |
| 84 | + pagination: PAGINATION, |
| 85 | + ...extra, |
| 86 | +}); |
| 87 | + |
| 88 | +/** Long enough for any effect a step schedules to have run and settled. */ |
| 89 | +const settle = () => act(() => new Promise<void>((resolve) => setTimeout(resolve, 150))); |
| 90 | + |
| 91 | +const selects = (find: ReturnType<typeof vi.fn>) => |
| 92 | + find.mock.calls.map((call: any[]) => call[1]?.$select as string[] | undefined); |
| 93 | + |
| 94 | +/** Mount, wait for the first read to land, and settle every follow-up effect. */ |
| 95 | +async function mount(ds: any, extra: Record<string, unknown> = {}, props: Record<string, unknown> = {}) { |
| 96 | + const view = render(<ObjectGrid schema={gridSchema(extra)} dataSource={ds} {...props} />); |
| 97 | + await waitFor(() => expect(ds.find).toHaveBeenCalled()); |
| 98 | + await settle(); |
| 99 | + const rerender = async (next: Record<string, unknown>, nextProps: Record<string, unknown> = props) => { |
| 100 | + view.rerender(<ObjectGrid schema={gridSchema(next)} dataSource={ds} {...nextProps} />); |
| 101 | + await settle(); |
| 102 | + }; |
| 103 | + return { rerender }; |
| 104 | +} |
| 105 | + |
| 106 | +const INDUSTRY_RULE = { condition: "record.industry == 'Tech'", style: { backgroundColor: '#fee2e2' } }; |
| 107 | +const ROW_DEF = { name: 'escalate', label: 'Escalate', visible: "record.owner == 'u1'" }; |
| 108 | +const BULK_DEF = { name: 'archive', label: 'Archive', disabled: "record.status == 'closed'" }; |
| 109 | + |
| 110 | +describe('ObjectGrid re-reads when a harvested predicate input changes (objectui#10689)', () => { |
| 111 | + it('CONTROL — a fresh mount with the rule selects its operand, in one read', async () => { |
| 112 | + const ds = makeDataSource(); |
| 113 | + await mount(ds, { conditionalFormatting: [INDUSTRY_RULE] }); |
| 114 | + expect(selects(ds.find)).toEqual([['id', 'name', 'industry']]); |
| 115 | + }); |
| 116 | + |
| 117 | + it('a `conditionalFormatting` rule added to a mounted grid re-reads once, with its operand in `$select`', async () => { |
| 118 | + const ds = makeDataSource(); |
| 119 | + const { rerender } = await mount(ds); |
| 120 | + expect(selects(ds.find)).toEqual([['id', 'name']]); |
| 121 | + |
| 122 | + await rerender({ conditionalFormatting: [INDUSTRY_RULE] }); |
| 123 | + |
| 124 | + expect( |
| 125 | + selects(ds.find), |
| 126 | + 'The rule was added to a mounted grid and the grid never re-read, so the rows keep\n' |
| 127 | + + 'arriving without `industry` and the rule never matches. The load effect must name\n' |
| 128 | + + 'the harvested operands in its dependencies (objectui#10689).', |
| 129 | + ).toEqual([['id', 'name'], ['id', 'name', 'industry']]); |
| 130 | + }); |
| 131 | + |
| 132 | + it('a `rowActionDefs` def added to a mounted grid re-reads once, with its `visible` operand', async () => { |
| 133 | + const ds = makeDataSource(); |
| 134 | + const { rerender } = await mount(ds); |
| 135 | + |
| 136 | + await rerender({ rowActionDefs: [ROW_DEF] }); |
| 137 | + |
| 138 | + expect(selects(ds.find)).toEqual([['id', 'name'], ['id', 'name', 'owner']]); |
| 139 | + }); |
| 140 | + |
| 141 | + it('a `bulkActionDefs` def added to a mounted grid re-reads once, with its `disabled` operand', async () => { |
| 142 | + const ds = makeDataSource(); |
| 143 | + const { rerender } = await mount(ds); |
| 144 | + |
| 145 | + await rerender({ bulkActionDefs: [BULK_DEF] }); |
| 146 | + |
| 147 | + expect(selects(ds.find)).toEqual([['id', 'name'], ['id', 'name', 'status']]); |
| 148 | + }); |
| 149 | + |
| 150 | + it('CONTROL — an equal re-render (new arrays, same content) does not re-read', async () => { |
| 151 | + const ds = makeDataSource(); |
| 152 | + const { rerender } = await mount(ds, { |
| 153 | + conditionalFormatting: [INDUSTRY_RULE], |
| 154 | + rowActionDefs: [ROW_DEF], |
| 155 | + bulkActionDefs: [BULK_DEF], |
| 156 | + searchableFields: ['name', 'industry'], |
| 157 | + }, { search: 'acme' }); |
| 158 | + expect(ds.find).toHaveBeenCalledTimes(1); |
| 159 | + |
| 160 | + await rerender({ |
| 161 | + conditionalFormatting: [{ ...INDUSTRY_RULE, style: { ...INDUSTRY_RULE.style } }], |
| 162 | + rowActionDefs: [{ ...ROW_DEF }], |
| 163 | + bulkActionDefs: [{ ...BULK_DEF }], |
| 164 | + searchableFields: ['name', 'industry'], |
| 165 | + }, { search: 'acme' }); |
| 166 | + |
| 167 | + expect(ds.find, 'an equal re-render must cost no round trip').toHaveBeenCalledTimes(1); |
| 168 | + }); |
| 169 | + |
| 170 | + it('a rule change the query cannot see (its style) does not re-read', async () => { |
| 171 | + const ds = makeDataSource(); |
| 172 | + const { rerender } = await mount(ds, { conditionalFormatting: [INDUSTRY_RULE] }); |
| 173 | + |
| 174 | + await rerender({ |
| 175 | + conditionalFormatting: [{ ...INDUSTRY_RULE, style: { backgroundColor: '#dcfce7' } }], |
| 176 | + }); |
| 177 | + |
| 178 | + expect(ds.find).toHaveBeenCalledTimes(1); |
| 179 | + }); |
| 180 | +}); |
| 181 | + |
| 182 | +describe('ObjectGrid re-reads when `searchableFields` changes under a search term (objectui#10689)', () => { |
| 183 | + const searchFieldsOf = (find: ReturnType<typeof vi.fn>) => |
| 184 | + find.mock.calls.map((call: any[]) => call[1]?.$searchFields as string[] | undefined); |
| 185 | + |
| 186 | + it('`searchableFields` changed on a mounted grid re-reads once, with the new `$searchFields`', async () => { |
| 187 | + const ds = makeDataSource(); |
| 188 | + const { rerender } = await mount(ds, { searchableFields: ['name'] }, { search: 'acme' }); |
| 189 | + expect(searchFieldsOf(ds.find)).toEqual([['name']]); |
| 190 | + |
| 191 | + await rerender({ searchableFields: ['name', 'region'] }, { search: 'acme' }); |
| 192 | + |
| 193 | + expect( |
| 194 | + searchFieldsOf(ds.find), |
| 195 | + 'The view narrowed its search to new fields and the grid never re-read, so the rows on\n' |
| 196 | + + 'screen still answer the old `$searchFields` (objectui#10689).', |
| 197 | + ).toEqual([['name'], ['name', 'region']]); |
| 198 | + }); |
| 199 | + |
| 200 | + it('without a search term `$searchFields` is not sent, so a change to it does not re-read', async () => { |
| 201 | + const ds = makeDataSource(); |
| 202 | + const { rerender } = await mount(ds, { searchableFields: ['name'] }); |
| 203 | + expect(searchFieldsOf(ds.find)).toEqual([undefined]); |
| 204 | + |
| 205 | + await rerender({ searchableFields: ['name', 'region'] }); |
| 206 | + |
| 207 | + expect(ds.find).toHaveBeenCalledTimes(1); |
| 208 | + }); |
| 209 | +}); |
0 commit comments