|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The inspector does not COMMIT an erase (objectui#9372). |
| 5 | + * |
| 6 | + * ## Why this file exists next to the pure one |
| 7 | + * |
| 8 | + * `datasetFilterCondition.unmappedInert-9372.test.ts` pins the decision — |
| 9 | + * {@link isClearedGroup} tells the author's CLEAR gesture apart from a |
| 10 | + * serialization that produced nothing. A decision nobody consults is a channel |
| 11 | + * with no reader, and the pure file cannot tell the difference: it would stay |
| 12 | + * green with the guard sitting unused beside the old `onCommit(...)` call. |
| 13 | + * |
| 14 | + * This file drives the real component, with the real `FilterBuilder` mounted |
| 15 | + * inside it, and watches the ONE thing that caused the data loss — the patch. |
| 16 | + * |
| 17 | + * ## The gesture, and why it is the value and not the operator menu |
| 18 | + * |
| 19 | + * The card's route is an operator pick, but the same defect is reachable by |
| 20 | + * blanking the VALUE of the only row, with no operator involved at all: the |
| 21 | + * incomplete-row `continue` drops it, the last part goes, and the commit is |
| 22 | + * `undefined`. The host applies patches as `{ ...draft, ...patch }`, so that |
| 23 | + * commit SETS `filter` to `undefined` — the patch shape `objectChangePatch` |
| 24 | + * uses deliberately to erase it. Blanking a text input is also the one gesture |
| 25 | + * that needs no Radix listbox interaction, so this pin holds without driving a |
| 26 | + * select open in a headless DOM. |
| 27 | + * |
| 28 | + * ## The control |
| 29 | + * |
| 30 | + * "`onPatch` was not called" is also what an unopened popover, a mis-queried |
| 31 | + * input and a dead handler all look like. So the same file types a REAL value |
| 32 | + * through the same input and asserts the patch that produces — if that control |
| 33 | + * stops firing, the absence below stops meaning anything. |
| 34 | + */ |
| 35 | +import { describe, it, expect, vi, afterEach, type Mock } from 'vitest'; |
| 36 | +import { render, screen, fireEvent, cleanup } from '@testing-library/react'; |
| 37 | +import '@testing-library/jest-dom'; |
| 38 | + |
| 39 | +// Stub the catalog hooks so the inspector renders without a MetadataClient / |
| 40 | +// network, but with ONE text field so the filter popover has something to draw. |
| 41 | +vi.mock('./useDatasetFields', () => ({ |
| 42 | + useObjectOptions: () => ({ options: [], loading: false }), |
| 43 | + useDatasetFieldCatalog: () => ({ |
| 44 | + relationships: [], |
| 45 | + fieldOptions: [{ value: 'name', label: 'Name', type: 'text' }], |
| 46 | + loading: false, |
| 47 | + }), |
| 48 | + useDatasetUsage: () => ({ reports: 0, dashboards: 0, loading: false }), |
| 49 | + fieldTypeToDimensionType: (t: string) => (t === 'date' ? 'date' : 'string'), |
| 50 | +})); |
| 51 | + |
| 52 | +import { DatasetDefaultInspector } from './DatasetDefaultInspector'; |
| 53 | + |
| 54 | +afterEach(cleanup); |
| 55 | + |
| 56 | +const baseProps = { type: 'dataset', name: 'sales', locale: 'en-US' as const }; |
| 57 | + |
| 58 | +/** A dataset whose filter is ALREADY stored — the thing that got destroyed. */ |
| 59 | +const draft = { |
| 60 | + name: 'sales', |
| 61 | + label: 'Sales', |
| 62 | + object: 'opportunity', |
| 63 | + include: [], |
| 64 | + dimensions: [], |
| 65 | + measures: [], |
| 66 | + filter: { name: { $eq: 'acme' } }, |
| 67 | +}; |
| 68 | + |
| 69 | +/** The inspector's patch channel, typed as the component declares it. */ |
| 70 | +type PatchSpy = Mock<(patch: Record<string, unknown>) => void>; |
| 71 | +const patchSpy = (): PatchSpy => vi.fn<(patch: Record<string, unknown>) => void>(); |
| 72 | + |
| 73 | +/** Render, open the Scope filter popover, and hand back the row's value input. */ |
| 74 | +function openScopeFilter(onPatch: PatchSpy) { |
| 75 | + render(<DatasetDefaultInspector {...baseProps} draft={draft} onPatch={onPatch} readOnly={false} />); |
| 76 | + // The trigger summarises the stored filter; seeing it at all is already a |
| 77 | + // reading that `conditionToGroup` found the stored shape representable. |
| 78 | + fireEvent.click(screen.getByText('1 condition')); |
| 79 | + return screen.getByDisplayValue('acme') as HTMLInputElement; |
| 80 | +} |
| 81 | + |
| 82 | +describe('DatasetDefaultInspector — a filter edit that cannot be stored commits nothing (objectui#9372)', () => { |
| 83 | + it('CONTROL: typing a real value still commits it, so the absence below is about the blank', () => { |
| 84 | + const onPatch = patchSpy(); |
| 85 | + const input = openScopeFilter(onPatch); |
| 86 | + fireEvent.change(input, { target: { value: 'contoso' } }); |
| 87 | + expect(onPatch).toHaveBeenCalledWith({ filter: { name: { $eq: 'contoso' } } }); |
| 88 | + }); |
| 89 | + |
| 90 | + it('THE DEFECT: blanking the only row\'s value does NOT patch `filter` to undefined', () => { |
| 91 | + const onPatch = patchSpy(); |
| 92 | + const input = openScopeFilter(onPatch); |
| 93 | + fireEvent.change(input, { target: { value: '' } }); |
| 94 | + // Before the repair this called `onPatch({ filter: undefined })`, which the |
| 95 | + // host spreads over the draft — the stored filter destroyed, nothing shown |
| 96 | + // to the author, and `JSON.stringify` then omits the key on save. |
| 97 | + for (const [patch] of onPatch.mock.calls) { |
| 98 | + expect( |
| 99 | + patch, |
| 100 | + 'the inspector committed a patch carrying `filter`; if it is undefined, that ERASES the stored filter', |
| 101 | + ).not.toHaveProperty('filter'); |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + it('and the author\'s own CLEAR gesture still reaches the draft', () => { |
| 106 | + // The other half: "Clear all" empties the group, which IS the clear |
| 107 | + // gesture, and must still commit `undefined`. Without this the repair |
| 108 | + // could have been a blanket "never commit undefined", stranding the filter |
| 109 | + // an author asked to remove. |
| 110 | + const onPatch = patchSpy(); |
| 111 | + render(<DatasetDefaultInspector {...baseProps} draft={draft} onPatch={onPatch} readOnly={false} />); |
| 112 | + fireEvent.click(screen.getByText('1 condition')); |
| 113 | + fireEvent.click(screen.getByText('Clear all')); |
| 114 | + expect(onPatch).toHaveBeenCalledWith({ filter: undefined }); |
| 115 | + }); |
| 116 | +}); |
0 commit comments