|
| 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 | + * objectstack#35727 — the data page opened with an address condition (a |
| 11 | + * record's related tab 「↗」 lands here with `filter[account]=<id>`) draws |
| 12 | + * that condition ONCE: as its own 「Filtered by」 tag. The list under it still |
| 13 | + * filters on the condition, and draws no second, locked chip for it and no |
| 14 | + * 「Reset」 of its own while the reader has added nothing. |
| 15 | + * |
| 16 | + * The real `ListView` renders here — the defect lived where the two surfaces |
| 17 | + * meet, so a stand-in list would pin nothing. |
| 18 | + */ |
| 19 | + |
| 20 | +import * as React from 'react'; |
| 21 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 22 | +import { render, cleanup, screen, waitFor, within, act } from '@testing-library/react'; |
| 23 | +import { MemoryRouter, Routes, Route } from 'react-router-dom'; |
| 24 | + |
| 25 | +// One answer object per hook, held for the file: the page keys its address |
| 26 | +// conditions on `canRead`, so a fresh function every render would rebuild the |
| 27 | +// list's schema every render and re-read the records without end — a stand-in |
| 28 | +// artefact, not the page's behaviour (the real hooks answer stable values). |
| 29 | +const permissionAnswers = vi.hoisted(() => ({ |
| 30 | + permissions: { |
| 31 | + check: () => ({ allowed: true }), |
| 32 | + checkField: () => true, |
| 33 | + getFieldPermissions: () => [], |
| 34 | + getRowFilter: () => undefined, |
| 35 | + getObjectApiOperations: () => undefined, |
| 36 | + roles: [], |
| 37 | + isLoaded: false, |
| 38 | + hasCapabilities: () => true, |
| 39 | + can: () => true, |
| 40 | + cannot: () => false, |
| 41 | + }, |
| 42 | + fields: { canRead: () => true, canWrite: () => true, permissions: [] }, |
| 43 | +})); |
| 44 | + |
| 45 | +vi.mock('@object-ui/permissions', async (importOriginal) => { |
| 46 | + const actual = await importOriginal<typeof import('@object-ui/permissions')>(); |
| 47 | + return { |
| 48 | + ...actual, |
| 49 | + usePermissions: () => permissionAnswers.permissions, |
| 50 | + useFieldPermissions: () => permissionAnswers.fields, |
| 51 | + }; |
| 52 | +}); |
| 53 | + |
| 54 | +const authAnswer = vi.hoisted(() => ({ user: { id: 'u1', name: 'Ada' }, activeOrganization: null })); |
| 55 | + |
| 56 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 57 | + ...(await importOriginal<Record<string, unknown>>()), |
| 58 | + useAuth: () => authAnswer, |
| 59 | + useWorkspaceAdminStatus: () => ({ isAdmin: false, isResolved: true }), |
| 60 | + createAuthenticatedFetch: () => vi.fn(), |
| 61 | +})); |
| 62 | + |
| 63 | +vi.mock('./RecordDetailView', () => ({ RecordDetailView: () => null })); |
| 64 | +vi.mock('./CreateViewDialog', () => ({ CreateViewDialog: () => null })); |
| 65 | +vi.mock('./metadata-admin/useMetadata', () => ({ useMetadataClient: () => ({}) })); |
| 66 | + |
| 67 | +import { I18nProvider } from '@object-ui/i18n'; |
| 68 | +// The page reaches a dynamic `import('@objectstack/lint')`; its first load is |
| 69 | +// paid here, at collection, never inside a clocked test window. |
| 70 | +import '@objectstack/lint'; |
| 71 | +import { ObjectDataPage } from './ObjectDataPage'; |
| 72 | +import { ExpressionProvider } from '../providers/ExpressionProvider'; |
| 73 | + |
| 74 | +const h = React.createElement; |
| 75 | +const OBJECT_NAME = 'contact_35727'; |
| 76 | +const ACCOUNT = 'account_35727'; |
| 77 | + |
| 78 | +const CONTACT = { |
| 79 | + name: OBJECT_NAME, |
| 80 | + label: 'Contact', |
| 81 | + managedBy: 'platform', |
| 82 | + fields: { |
| 83 | + name: { type: 'text', label: 'Name' }, |
| 84 | + account: { type: 'lookup', label: 'Account', reference: ACCOUNT }, |
| 85 | + }, |
| 86 | +}; |
| 87 | +const OBJECTS = [CONTACT, { name: ACCOUNT, label: 'Account', fields: { name: { type: 'text', label: 'Name' } } }]; |
| 88 | + |
| 89 | +function makeDataSource() { |
| 90 | + return { |
| 91 | + find: vi.fn(async (object: string) => |
| 92 | + object === ACCOUNT |
| 93 | + ? { data: [{ id: 'acc_1', name: 'Globex' }], total: 1 } |
| 94 | + : { data: [{ id: 'c1', name: 'Ada', account: 'acc_1' }], total: 1 }, |
| 95 | + ), |
| 96 | + findOne: vi.fn(async () => null), |
| 97 | + create: vi.fn(async () => ({})), |
| 98 | + update: vi.fn(async () => ({})), |
| 99 | + delete: vi.fn(async () => ({})), |
| 100 | + getObjectSchema: vi.fn(async (object: string) => structuredClone(OBJECTS.find((o) => o.name === object))), |
| 101 | + } as any; |
| 102 | +} |
| 103 | + |
| 104 | +// Module constants: `I18nProvider` re-boots on a new `config` identity. |
| 105 | +const ZH = { defaultLanguage: 'zh-Hans', detectBrowserLanguage: false }; |
| 106 | +const EN = { defaultLanguage: 'en', detectBrowserLanguage: false }; |
| 107 | + |
| 108 | +function renderAt(config: typeof ZH, search: string) { |
| 109 | + const dataSource = makeDataSource(); |
| 110 | + const page = h( |
| 111 | + MemoryRouter, |
| 112 | + { initialEntries: [`/apps/demo/${OBJECT_NAME}/data?${search}`] }, |
| 113 | + h( |
| 114 | + Routes, |
| 115 | + null, |
| 116 | + h(Route, { |
| 117 | + path: '/apps/:appName/:objectName/data', |
| 118 | + element: h(ObjectDataPage, { dataSource, objects: OBJECTS }), |
| 119 | + }), |
| 120 | + ), |
| 121 | + ); |
| 122 | + render( |
| 123 | + h(I18nProvider, { |
| 124 | + config, |
| 125 | + children: h(ExpressionProvider, { user: { id: 'u1', name: 'Ada', profile: 'admin' }, children: page }), |
| 126 | + }), |
| 127 | + ); |
| 128 | + return dataSource; |
| 129 | +} |
| 130 | + |
| 131 | +const recordReads = (ds: { find: ReturnType<typeof vi.fn> }) => |
| 132 | + ds.find.mock.calls.filter((call: unknown[]) => call[0] === OBJECT_NAME); |
| 133 | + |
| 134 | +beforeEach(() => { |
| 135 | + cleanup(); |
| 136 | + window.localStorage.clear(); |
| 137 | + vi.stubGlobal( |
| 138 | + 'fetch', |
| 139 | + vi.fn(async () => new Response(JSON.stringify({ data: [] }), { status: 200, headers: { 'content-type': 'application/json' } })), |
| 140 | + ); |
| 141 | +}); |
| 142 | + |
| 143 | +afterEach(() => { |
| 144 | + vi.unstubAllGlobals(); |
| 145 | + vi.clearAllMocks(); |
| 146 | +}); |
| 147 | + |
| 148 | +describe('a sourced open of the data page draws each condition once (objectstack#35727)', () => { |
| 149 | + it.each([ |
| 150 | + ['zh', ZH], |
| 151 | + ['en', EN], |
| 152 | + ])('%s: one page tag, no locked chip, no list Reset — and the records are still filtered', async (_lang, config) => { |
| 153 | + const ds = renderAt(config, 'filter[account]=acc_1'); |
| 154 | + const row = await screen.findByTestId('list-condition-row'); |
| 155 | + await waitFor(() => expect(recordReads(ds).length).toBeGreaterThan(0)); |
| 156 | + await act(async () => {}); |
| 157 | + |
| 158 | + const tags = screen.getByTestId('object-data-filter-chips'); |
| 159 | + expect(within(tags).getAllByTestId(/^object-data-remove-filter-/)).toHaveLength(1); |
| 160 | + expect(screen.queryAllByTestId('view-condition-chip')).toHaveLength(0); |
| 161 | + expect(within(row).queryByTestId('condition-reset')).toBeNull(); |
| 162 | + expect(JSON.stringify(recordReads(ds).at(-1)![1])).toContain('acc_1'); |
| 163 | + }); |
| 164 | + |
| 165 | + it('CONTROL: with no address condition there is no page tag and the list reads unfiltered', async () => { |
| 166 | + const ds = renderAt(EN, ''); |
| 167 | + await screen.findByTestId('list-condition-row'); |
| 168 | + await waitFor(() => expect(recordReads(ds).length).toBeGreaterThan(0)); |
| 169 | + expect(screen.queryByTestId('object-data-filter-chips')).toBeNull(); |
| 170 | + expect(JSON.stringify(recordReads(ds).at(-1)![1])).not.toContain('acc_1'); |
| 171 | + }); |
| 172 | +}); |
0 commit comments