|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#25888 — a related list's "View all" carries the list's OWN |
| 5 | + * `filter` as well as its relationship, so the full list shows the rows the |
| 6 | + * list showed. |
| 7 | + * |
| 8 | + * The defect: the destination kept the relationship (`record_id`) and dropped |
| 9 | + * the list's `filter` (`object_name`). For most objects that only widens the |
| 10 | + * list. For `sys_approval_request` it emptied it: the request read gate admits |
| 11 | + * a record reader only on a read pinning BOTH fields, so a reader who saw two |
| 12 | + * requests on the record's Approvals list saw none on "View all". |
| 13 | + * |
| 14 | + * Every case mounts what a record page mounts, as the objectui#12741 suite |
| 15 | + * beside this one does: the real `RelatedRecordActionsBridge` under a router, |
| 16 | + * around the `{ type, properties }` node through the real `SchemaRenderer`, |
| 17 | + * `@object-ui/plugin-detail`'s registration and the real `RelatedList`. The |
| 18 | + * oracle for the destination is the READ side of the same URL family |
| 19 | + * (`parseUrlFilterTriples`, what `ObjectDataPage` parses), so the pin does not |
| 20 | + * restate how a filter is spelled. |
| 21 | + */ |
| 22 | + |
| 23 | +import * as React from 'react'; |
| 24 | +import { describe, it, expect, vi, beforeAll, afterEach } from 'vitest'; |
| 25 | +import { cleanup, fireEvent, render, screen } from '@testing-library/react'; |
| 26 | +import { MemoryRouter, useLocation } from 'react-router-dom'; |
| 27 | +import '@testing-library/jest-dom'; |
| 28 | +import { |
| 29 | + FilterScopeProvider, |
| 30 | + MetadataCtx, |
| 31 | + RecordContextProvider, |
| 32 | + SchemaRenderer, |
| 33 | + type MetadataContextValue, |
| 34 | +} from '@object-ui/react'; |
| 35 | +import { PermissionProvider } from '@object-ui/permissions'; |
| 36 | +// Registers `record:related_list`. Module scope on purpose (AGENTS.md, test discipline). |
| 37 | +import '@object-ui/plugin-detail'; |
| 38 | + |
| 39 | +import { RelatedRecordActionsBridge } from '../RelatedRecordActionsBridge'; |
| 40 | +import { parseUrlFilterTriples } from '../drillUrlFilters'; |
| 41 | + |
| 42 | +/** Desktop, pinned (objectui#8399): under 768px a table related list is a card gallery. */ |
| 43 | +beforeAll(() => { |
| 44 | + Object.defineProperty(window, 'innerWidth', { configurable: true, value: 1280 }); |
| 45 | +}); |
| 46 | + |
| 47 | +afterEach(() => cleanup()); |
| 48 | + |
| 49 | +const APP = 'clm'; |
| 50 | +const PARENT_ROUTE = `/apps/${APP}/contract/record/C-1`; |
| 51 | + |
| 52 | +/** |
| 53 | + * The repro's shape: a polymorphic child that points at its parent by |
| 54 | + * `object_name` + `record_id`, listed on the parent's record page by |
| 55 | + * `relationshipField: 'record_id'` plus a `filter` pinning `object_name`. |
| 56 | + */ |
| 57 | +const REQUEST = { |
| 58 | + name: 'approval_request', |
| 59 | + label: 'Approval Request', |
| 60 | + fields: { |
| 61 | + name: { type: 'text', label: 'Name' }, |
| 62 | + object_name: { type: 'text', label: 'Object' }, |
| 63 | + record_id: { type: 'text', label: 'Record' }, |
| 64 | + owner: { type: 'text', label: 'Owner' }, |
| 65 | + status: { type: 'text', label: 'Status' }, |
| 66 | + }, |
| 67 | +}; |
| 68 | + |
| 69 | +const ROWS = [ |
| 70 | + { id: 'r1', name: 'First approval', object_name: 'contract', record_id: 'C-1' }, |
| 71 | + { id: 'r2', name: 'Second approval', object_name: 'contract', record_id: 'C-1' }, |
| 72 | +]; |
| 73 | + |
| 74 | +/** The list's own scope, authored the way the spec declares it (`ViewFilterRule[]`). */ |
| 75 | +const PINS_OBJECT = [{ field: 'object_name', operator: 'equals', value: 'contract' }]; |
| 76 | + |
| 77 | +const metadata: MetadataContextValue = { |
| 78 | + apps: [], |
| 79 | + objects: [REQUEST], |
| 80 | + dashboards: [], |
| 81 | + reports: [], |
| 82 | + pages: [], |
| 83 | + loading: false, |
| 84 | + error: null, |
| 85 | + refresh: async () => {}, |
| 86 | + invalidate: () => {}, |
| 87 | + ensureType: async () => [], |
| 88 | + getItem: (async () => REQUEST) as unknown as MetadataContextValue['getItem'], |
| 89 | + getItemsByType: () => [], |
| 90 | + getTypeStatus: () => 'ready' as const, |
| 91 | +}; |
| 92 | + |
| 93 | +function RouterLocation() { |
| 94 | + const location = useLocation(); |
| 95 | + return <output data-testid="router-location">{`${location.pathname}${location.search}`}</output>; |
| 96 | +} |
| 97 | + |
| 98 | +/** The role-based provider, letting `viewer` read the child with the named fields denied. */ |
| 99 | +const denying = (fields: string[]) => (node: React.ReactElement) => ( |
| 100 | + <PermissionProvider |
| 101 | + roles={[]} |
| 102 | + userRoles={['viewer']} |
| 103 | + permissions={ |
| 104 | + [ |
| 105 | + { |
| 106 | + object: 'approval_request', |
| 107 | + roles: { viewer: { actions: ['read'], fieldPermissions: fields.map((field) => ({ field, read: false })) } }, |
| 108 | + }, |
| 109 | + ] as never |
| 110 | + } |
| 111 | + > |
| 112 | + {node} |
| 113 | + </PermissionProvider> |
| 114 | +); |
| 115 | + |
| 116 | +function mount({ |
| 117 | + properties = {}, |
| 118 | + wrap = (node: React.ReactElement) => node, |
| 119 | +}: { |
| 120 | + properties?: Record<string, unknown>; |
| 121 | + wrap?: (node: React.ReactElement) => React.ReactElement; |
| 122 | +} = {}) { |
| 123 | + const dataSource = { |
| 124 | + find: vi.fn(async () => ROWS), |
| 125 | + getObjectSchema: vi.fn(async () => REQUEST), |
| 126 | + }; |
| 127 | + const doc = { |
| 128 | + type: 'record:related_list', |
| 129 | + properties: { |
| 130 | + objectName: 'approval_request', |
| 131 | + relationshipField: 'record_id', |
| 132 | + columns: ['name'], |
| 133 | + ...properties, |
| 134 | + }, |
| 135 | + }; |
| 136 | + render( |
| 137 | + <MetadataCtx.Provider value={metadata}> |
| 138 | + <MemoryRouter initialEntries={[PARENT_ROUTE]}> |
| 139 | + {wrap( |
| 140 | + <RelatedRecordActionsBridge |
| 141 | + appName={APP} |
| 142 | + objects={[REQUEST]} |
| 143 | + dataSource={dataSource} |
| 144 | + parentObjectName="contract" |
| 145 | + parentRecordId="C-1" |
| 146 | + parentTitle="MSA" |
| 147 | + parentRecord={{ id: 'C-1', name: 'MSA' }} |
| 148 | + > |
| 149 | + <RecordContextProvider objectName="contract" recordId="C-1" dataSource={dataSource as never}> |
| 150 | + <SchemaRenderer schema={doc as never} /> |
| 151 | + </RecordContextProvider> |
| 152 | + </RelatedRecordActionsBridge>, |
| 153 | + )} |
| 154 | + <RouterLocation /> |
| 155 | + </MemoryRouter> |
| 156 | + </MetadataCtx.Provider>, |
| 157 | + ); |
| 158 | + return dataSource; |
| 159 | +} |
| 160 | + |
| 161 | +const viewAll = () => screen.queryByRole('button', { name: /view all/i }); |
| 162 | +const location = () => screen.getByTestId('router-location').textContent ?? ''; |
| 163 | +/** Every row painted: each "no link" read below is taken on a list that drew its records. */ |
| 164 | +const waitForRows = async () => { |
| 165 | + await screen.findByText('First approval'); |
| 166 | + await screen.findByText('Second approval'); |
| 167 | +}; |
| 168 | +/** Click "View all" and read the destination back the way `ObjectDataPage` reads it. */ |
| 169 | +const followViewAll = () => { |
| 170 | + const link = viewAll(); |
| 171 | + expect(link).toBeInTheDocument(); |
| 172 | + fireEvent.click(link!); |
| 173 | + const [path, search] = location().split('?'); |
| 174 | + expect(path).toBe(`/apps/${APP}/approval_request/data`); |
| 175 | + return parseUrlFilterTriples(new URLSearchParams(search)); |
| 176 | +}; |
| 177 | +/** Order-free comparison: the list route ANDs its triples, so order says nothing. */ |
| 178 | +const sorted = (triples: unknown[][]) => [...triples].sort((a, b) => JSON.stringify(a).localeCompare(JSON.stringify(b))); |
| 179 | + |
| 180 | +describe('objectstack#25888 — "View all" carries the related list’s own filter', () => { |
| 181 | + it('the repro: a list scoped by `object_name` on a `record_id` relationship opens a list scoped by BOTH', async () => { |
| 182 | + mount({ properties: { filter: PINS_OBJECT } }); |
| 183 | + await waitForRows(); |
| 184 | + expect(sorted(followViewAll())).toEqual( |
| 185 | + sorted([ |
| 186 | + ['record_id', '=', 'C-1'], |
| 187 | + ['object_name', '=', 'contract'], |
| 188 | + ]), |
| 189 | + ); |
| 190 | + }); |
| 191 | + |
| 192 | + it('CONTROL: with no `filter` the destination carries the relationship alone, as before', async () => { |
| 193 | + mount(); |
| 194 | + await waitForRows(); |
| 195 | + expect(followViewAll()).toEqual([['record_id', '=', 'C-1']]); |
| 196 | + }); |
| 197 | + |
| 198 | + it('a placeholder in the filter is written RESOLVED, against the scope the list read its rows with', async () => { |
| 199 | + mount({ |
| 200 | + properties: { filter: [{ field: 'owner', operator: 'equals', value: '{current_user_id}' }] }, |
| 201 | + wrap: (node) => <FilterScopeProvider currentUserId="usr_7">{node}</FilterScopeProvider>, |
| 202 | + }); |
| 203 | + await waitForRows(); |
| 204 | + expect(sorted(followViewAll())).toEqual( |
| 205 | + sorted([ |
| 206 | + ['record_id', '=', 'C-1'], |
| 207 | + ['owner', '=', 'usr_7'], |
| 208 | + ]), |
| 209 | + ); |
| 210 | + }); |
| 211 | + |
| 212 | + it('a range and an is-null condition travel too: the route has a spelling for each', async () => { |
| 213 | + mount({ |
| 214 | + properties: { |
| 215 | + filter: [ |
| 216 | + ['name', '>=', 'A'], |
| 217 | + ['status', 'is_null', true], |
| 218 | + ], |
| 219 | + }, |
| 220 | + }); |
| 221 | + await waitForRows(); |
| 222 | + expect(sorted(followViewAll())).toEqual( |
| 223 | + sorted([ |
| 224 | + ['record_id', '=', 'C-1'], |
| 225 | + ['name', '>=', 'A'], |
| 226 | + ['status', 'is_null', true], |
| 227 | + ]), |
| 228 | + ); |
| 229 | + }); |
| 230 | + |
| 231 | + it('a filter with an `or` draws no link: the route cannot say it, and dropping it would widen the list', async () => { |
| 232 | + mount({ |
| 233 | + properties: { |
| 234 | + filter: ['or', ['status', '=', 'pending'], ['status', '=', 'approved']], |
| 235 | + }, |
| 236 | + }); |
| 237 | + await waitForRows(); |
| 238 | + expect(viewAll()).toBeNull(); |
| 239 | + }); |
| 240 | + |
| 241 | + it('a filter on a field the viewer cannot read draws no link: `ObjectDataPage` would drop that condition', async () => { |
| 242 | + mount({ properties: { filter: PINS_OBJECT }, wrap: denying(['object_name']) }); |
| 243 | + await waitForRows(); |
| 244 | + expect(viewAll()).toBeNull(); |
| 245 | + }); |
| 246 | + |
| 247 | + it('CONTROL: under the same provider, denying a field the filter does not name, the link draws and carries both', async () => { |
| 248 | + mount({ properties: { filter: PINS_OBJECT }, wrap: denying(['owner']) }); |
| 249 | + await waitForRows(); |
| 250 | + expect(sorted(followViewAll())).toEqual( |
| 251 | + sorted([ |
| 252 | + ['record_id', '=', 'C-1'], |
| 253 | + ['object_name', '=', 'contract'], |
| 254 | + ]), |
| 255 | + ); |
| 256 | + }); |
| 257 | +}); |
0 commit comments