|
| 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#10887 member 2 — a dashboard filter's `optionsFrom` options are |
| 11 | + * re-read when the data-invalidation bus (`notifyDataChanged` from |
| 12 | + * `@object-ui/react`) reports a change to `optionsFrom.object`, and the value |
| 13 | + * the user selected survives the re-read. |
| 14 | + * |
| 15 | + * Before this card the options effect keyed on the object, the fields, the |
| 16 | + * options filter and the data source, and on no nonce. After a page action over |
| 17 | + * raw HTTP the filter kept offering the pre-action values until `PageView` |
| 18 | + * remounted the page, and objectui#10519 removes that remount. The effect now |
| 19 | + * names the `useDataInvalidation` nonce for `optionsFrom.object`, the |
| 20 | + * objectui#10853 shape (the record picker's options). |
| 21 | + * |
| 22 | + * Rendered through the real `SchemaRenderer` and this package's own |
| 23 | + * registration of `dashboard`, with the adapter injected by |
| 24 | + * `SchemaRendererProvider`, on both option reads: the server-side dataset |
| 25 | + * GROUP BY and the client-side `find` fallback. The selected value lives in the |
| 26 | + * dashboard's own variables provider, so it is chosen through the real select. |
| 27 | + * The bare `useDataInvalidation` reader beside the dashboard and an |
| 28 | + * `object-metric` block (it reads the bus itself) are the positive controls. |
| 29 | + */ |
| 30 | +import * as React from 'react'; |
| 31 | +import { describe, it, expect, vi, beforeAll, afterEach } from 'vitest'; |
| 32 | +import { render, act, cleanup, fireEvent, screen, waitFor } from '@testing-library/react'; |
| 33 | +import { SchemaRenderer, SchemaRendererProvider, notifyDataChanged, useDataInvalidation } from '@object-ui/react'; |
| 34 | +// Module scope, not a hook: this import IS the registration of `dashboard`. |
| 35 | +import '../index'; |
| 36 | + |
| 37 | +// Radix Select opens on pointer events the DOM environment does not implement; |
| 38 | +// the same shim `DashboardFilterBar.options.test.tsx` uses. |
| 39 | +beforeAll(() => { |
| 40 | + class MockPointerEvent extends Event { |
| 41 | + button: number; |
| 42 | + ctrlKey: boolean; |
| 43 | + pointerType: string; |
| 44 | + constructor(type: string, props: PointerEventInit = {}) { |
| 45 | + super(type, props); |
| 46 | + this.button = props.button ?? 0; |
| 47 | + this.ctrlKey = props.ctrlKey ?? false; |
| 48 | + this.pointerType = props.pointerType ?? 'mouse'; |
| 49 | + } |
| 50 | + } |
| 51 | + Object.assign(window, { PointerEvent: MockPointerEvent }); |
| 52 | + Object.assign(HTMLElement.prototype, { |
| 53 | + hasPointerCapture: vi.fn(), |
| 54 | + releasePointerCapture: vi.fn(), |
| 55 | + scrollIntoView: vi.fn(), |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +afterEach(cleanup); |
| 60 | + |
| 61 | +type Path = 'dataset' | 'fallback'; |
| 62 | + |
| 63 | +/** |
| 64 | + * The options on the first read are `finance` and `retail`; every later read |
| 65 | + * adds `energy`, so a re-read that landed is visible in the list. |
| 66 | + */ |
| 67 | +function makeDataSource(path: Path) { |
| 68 | + let answered = 0; |
| 69 | + const values = () => { |
| 70 | + answered += 1; |
| 71 | + return answered === 1 ? ['finance', 'retail'] : ['energy', 'finance', 'retail']; |
| 72 | + }; |
| 73 | + const find = vi.fn(async (_object: string, _query?: unknown) => ({ data: values().map((industry) => ({ industry })) })); |
| 74 | + const queryDataset = vi.fn(async (_draft: unknown, _selection?: unknown) => ({ |
| 75 | + rows: values().map((industry) => ({ industry, option_count: 1 })), |
| 76 | + })); |
| 77 | + return path === 'dataset' ? { find, queryDataset } : { find }; |
| 78 | +} |
| 79 | + |
| 80 | +/** The option reads, whichever of the two paths served them. */ |
| 81 | +const reads = (ds: ReturnType<typeof makeDataSource>, path: Path) => |
| 82 | + path === 'dataset' ? (ds as { queryDataset: ReturnType<typeof vi.fn> }).queryDataset : ds.find; |
| 83 | + |
| 84 | +/** The positive control: a bare reader of the filter's source object. */ |
| 85 | +function BusControl() { |
| 86 | + return <span data-testid="bus-control">{useDataInvalidation('accounts')}</span>; |
| 87 | +} |
| 88 | + |
| 89 | +const selectFilter = (extra: Record<string, unknown> = {}) => ({ |
| 90 | + name: 'industry', |
| 91 | + field: 'industry', |
| 92 | + label: 'Industry', |
| 93 | + type: 'select', |
| 94 | + ...extra, |
| 95 | +}); |
| 96 | + |
| 97 | +const dashboardWith = (filter: Record<string, unknown>) => ({ type: 'dashboard', globalFilters: [filter], widgets: [] }); |
| 98 | + |
| 99 | +const OPTIONS_FROM = { optionsFrom: { object: 'accounts', valueField: 'industry', labelField: 'industry' } }; |
| 100 | + |
| 101 | +function mount(path: Path, node: Record<string, unknown> = dashboardWith(selectFilter(OPTIONS_FROM))) { |
| 102 | + const ds = makeDataSource(path); |
| 103 | + render( |
| 104 | + <SchemaRendererProvider dataSource={ds as never}> |
| 105 | + <BusControl /> |
| 106 | + <SchemaRenderer schema={node as never} /> |
| 107 | + </SchemaRendererProvider>, |
| 108 | + ); |
| 109 | + return ds; |
| 110 | +} |
| 111 | + |
| 112 | +const rest = () => act(() => new Promise<void>((resolve) => setTimeout(resolve, 60))); |
| 113 | +const emit = (change: { objectName: string; recordId?: string }) => |
| 114 | + act(async () => { |
| 115 | + notifyDataChanged(change); |
| 116 | + }); |
| 117 | +const trigger = () => screen.getByTestId('dashboard-filter-industry'); |
| 118 | + |
| 119 | +async function mountAtRest(path: Path) { |
| 120 | + const ds = mount(path); |
| 121 | + const read = reads(ds, path); |
| 122 | + await waitFor(() => expect(read).toHaveBeenCalledTimes(1)); |
| 123 | + await rest(); |
| 124 | + expect(read, 'one read on mount').toHaveBeenCalledTimes(1); |
| 125 | + return { ds, read }; |
| 126 | +} |
| 127 | + |
| 128 | +describe('a dashboard filter re-reads its optionsFrom options on the data-invalidation bus (objectui#10887)', () => { |
| 129 | + for (const path of ['dataset', 'fallback'] as const) { |
| 130 | + it(`an unscoped change ("*") re-reads the options once (${path} read)`, async () => { |
| 131 | + const { read } = await mountAtRest(path); |
| 132 | + |
| 133 | + await emit({ objectName: '*' }); |
| 134 | + await rest(); |
| 135 | + |
| 136 | + expect(screen.getByTestId('bus-control').textContent, 'control: the event never reached a subscriber').toBe('1'); |
| 137 | + expect(read, 'the options never re-read after the bus reported a change').toHaveBeenCalledTimes(2); |
| 138 | + }); |
| 139 | + |
| 140 | + it(`a change to its own object re-reads once; an unrelated object does not (${path} read)`, async () => { |
| 141 | + const { read } = await mountAtRest(path); |
| 142 | + |
| 143 | + await emit({ objectName: 'unrelated_object' }); |
| 144 | + await rest(); |
| 145 | + expect(read, 'a change to another object re-read the options').toHaveBeenCalledTimes(1); |
| 146 | + |
| 147 | + await emit({ objectName: 'accounts', recordId: 'a1' }); |
| 148 | + await rest(); |
| 149 | + expect(read, 'a change to its own object did not re-read the options exactly once').toHaveBeenCalledTimes(2); |
| 150 | + }); |
| 151 | + } |
| 152 | + |
| 153 | + it('the selected value survives the re-read, and the re-read options reach the list', async () => { |
| 154 | + const { read } = await mountAtRest('dataset'); |
| 155 | + |
| 156 | + fireEvent.pointerDown(trigger(), { button: 0 }); |
| 157 | + fireEvent.click(await screen.findByRole('option', { name: 'retail' })); |
| 158 | + await waitFor(() => expect(trigger().textContent).toBe('retail')); |
| 159 | + await rest(); |
| 160 | + const triggerNode = trigger(); |
| 161 | + |
| 162 | + await emit({ objectName: '*' }); |
| 163 | + await rest(); |
| 164 | + expect(read).toHaveBeenCalledTimes(2); |
| 165 | + |
| 166 | + expect(trigger(), 'the re-read remounted the filter').toBe(triggerNode); |
| 167 | + expect(trigger().textContent, 'the re-read dropped the selected value').toBe('retail'); |
| 168 | + fireEvent.pointerDown(trigger(), { button: 0 }); |
| 169 | + expect(await screen.findByRole('option', { name: 'energy' }), 'the re-read options never reached the list').toBeTruthy(); |
| 170 | + expect(trigger().textContent).toBe('retail'); |
| 171 | + }); |
| 172 | + |
| 173 | + it('lit control: an object-metric block beside the dashboard re-reads on the same event through its own reader', async () => { |
| 174 | + const ds = { ...makeDataSource('dataset'), aggregate: vi.fn(async () => [{ industry: 3 }]) }; |
| 175 | + render( |
| 176 | + <SchemaRendererProvider dataSource={ds as never}> |
| 177 | + <SchemaRenderer schema={dashboardWith(selectFilter(OPTIONS_FROM)) as never} /> |
| 178 | + <SchemaRenderer |
| 179 | + schema={{ type: 'object-metric', objectName: 'accounts', aggregate: { field: 'industry', function: 'count' }, label: 'Accounts' } as never} |
| 180 | + /> |
| 181 | + </SchemaRendererProvider>, |
| 182 | + ); |
| 183 | + await waitFor(() => expect(ds.aggregate).toHaveBeenCalledTimes(1)); |
| 184 | + await rest(); |
| 185 | + |
| 186 | + await emit({ objectName: '*' }); |
| 187 | + await rest(); |
| 188 | + |
| 189 | + expect(ds.aggregate, 'the metric never re-read: the event did not reach a block that reads the bus').toHaveBeenCalledTimes(2); |
| 190 | + }); |
| 191 | + |
| 192 | + it('control: a select filter with no optionsFrom reads nothing, on mount or on an invalidation', async () => { |
| 193 | + const ds = mount('dataset', dashboardWith(selectFilter({ options: [{ value: 'finance', label: 'Finance' }] }))); |
| 194 | + await rest(); |
| 195 | + |
| 196 | + await emit({ objectName: '*' }); |
| 197 | + await rest(); |
| 198 | + |
| 199 | + expect(screen.getByTestId('bus-control').textContent).toBe('1'); |
| 200 | + expect(reads(ds, 'dataset')).not.toHaveBeenCalled(); |
| 201 | + expect(ds.find).not.toHaveBeenCalled(); |
| 202 | + }); |
| 203 | +}); |
0 commit comments