|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#26846 — the field sub-table under an expanded object stays |
| 5 | + * readable when the matrix scrolls sideways (its floor is wider than the |
| 6 | + * Studio content column at 1440 px): |
| 7 | + * |
| 8 | + * - the "visible / total" count sits beside the filter it counts, not at the |
| 9 | + * far end of the row, where it fell past the visible edge; |
| 10 | + * - the field-name column, header and rows, is pinned to the left edge of the |
| 11 | + * scroll container over an opaque background. |
| 12 | + * |
| 13 | + * jsdom lays nothing out, so this pins the structure and the classes the |
| 14 | + * browser reads; it is no screenshot. |
| 15 | + */ |
| 16 | + |
| 17 | +import '@testing-library/jest-dom/vitest'; |
| 18 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 19 | +import { cleanup, fireEvent, render, screen } from '@testing-library/react'; |
| 20 | +import { MemoryRouter } from 'react-router-dom'; |
| 21 | +import { t } from './i18n'; |
| 22 | + |
| 23 | +// `withPreviewDrafts` answers the client itself, not a promise (objectstack#26840). |
| 24 | +type FakeClient = Record<string, (...args: never[]) => unknown>; |
| 25 | + |
| 26 | +let clientImpl: FakeClient; |
| 27 | + |
| 28 | +vi.mock('./useMetadata', () => ({ |
| 29 | + useMetadataClient: () => clientImpl, |
| 30 | + useMetadataTypes: () => ({ |
| 31 | + loading: false, |
| 32 | + error: null, |
| 33 | + entries: [{ type: 'permission', label: 'Permission', allowEnvironmentOverlay: true }], |
| 34 | + }), |
| 35 | +})); |
| 36 | + |
| 37 | +vi.mock('./AssignedUsersSection', () => ({ AssignedUsersSection: () => null })); |
| 38 | + |
| 39 | +import { PermissionMatrixEditPage } from './PermissionMatrixEditor'; |
| 40 | + |
| 41 | +afterEach(cleanup); |
| 42 | + |
| 43 | +// Eight fields, so the filter shows (threshold is > 6). |
| 44 | +const FIELDS = ['amount', 'email', 'name', 'notes', 'phone', 'region', 'revenue', 'status']; |
| 45 | + |
| 46 | +function makeClient(): FakeClient { |
| 47 | + return { |
| 48 | + layered: async () => ({ |
| 49 | + effective: { name: 'project_editor', label: 'Project editor', objects: { a_project: { allowRead: true } }, fields: {} }, |
| 50 | + code: null, |
| 51 | + overlay: null, |
| 52 | + overlayScope: null, |
| 53 | + }), |
| 54 | + getDraft: async () => null, |
| 55 | + withPreviewDrafts() { return this; }, |
| 56 | + list: async (type: string) => (type === 'object' ? [{ item: { name: 'a_project' } }] : []), |
| 57 | + get: async (type: string) => (type === 'object' ? { fields: FIELDS.map((name) => ({ name })) } : null), |
| 58 | + save: async (_type: string, _name: string, payload: Record<string, unknown>) => payload, |
| 59 | + }; |
| 60 | +} |
| 61 | + |
| 62 | +async function renderExpanded() { |
| 63 | + clientImpl = makeClient(); |
| 64 | + render( |
| 65 | + <MemoryRouter> |
| 66 | + <PermissionMatrixEditPage type="permission" name="project_editor" packageId="app.a" /> |
| 67 | + </MemoryRouter>, |
| 68 | + ); |
| 69 | + fireEvent.click(await screen.findByRole('button', { name: /a_project/ })); |
| 70 | + await screen.findByLabelText('a_project.notes readable'); |
| 71 | +} |
| 72 | + |
| 73 | +const PINNED = ['sticky', 'left-0', 'bg-background']; |
| 74 | + |
| 75 | +describe('the field sub-table when the matrix scrolls sideways (objectstack#26846)', () => { |
| 76 | + it('the count sits right after the filter, not pushed to the far end of the row', async () => { |
| 77 | + await renderExpanded(); |
| 78 | + const count = screen.getByTestId('perm-field-count-a_project'); |
| 79 | + expect(count).toHaveTextContent('8 / 8'); |
| 80 | + expect(count.previousElementSibling).toBe(screen.getByLabelText(t('perm.field.filter', 'en'))); |
| 81 | + expect(count).not.toHaveClass('ml-auto'); |
| 82 | + expect(count).toHaveClass('shrink-0', 'whitespace-nowrap'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('the field-name column is pinned left, header and every row', async () => { |
| 86 | + await renderExpanded(); |
| 87 | + const header = screen.getByRole('columnheader', { name: t('perm.field.col.name', 'en') }); |
| 88 | + expect(header).toHaveClass(...PINNED); |
| 89 | + for (const f of FIELDS) { |
| 90 | + expect(screen.getByTestId(`perm-field-name-a_project-${f}`)).toHaveClass(...PINNED); |
| 91 | + } |
| 92 | + // CONTROL: the boxes beside it scroll; only the name column is pinned. |
| 93 | + expect(screen.getByLabelText('a_project.notes readable').closest('td')).not.toHaveClass('sticky'); |
| 94 | + }); |
| 95 | +}); |
0 commit comments