|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#26884 — the permission-set header names the set by the label |
| 5 | + * the published list serves. `GET /meta/permission` is localized at the REST |
| 6 | + * boundary (the server's permission-set translator), so under zh-CN Setup → |
| 7 | + * 权限集 lists the set as 销售代表; the layered read the editor's draft is |
| 8 | + * built from carries the authored text, and the header used to print that — |
| 9 | + * "Sales Representative" on a zh-CN page. |
| 10 | + * |
| 11 | + * The served label is display only: a save sends the authored label back |
| 12 | + * unchanged, a set the list does not hold keeps its draft label, and an edit |
| 13 | + * to the label shows the edit. |
| 14 | + */ |
| 15 | + |
| 16 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 17 | +import { render, screen, cleanup, fireEvent, waitFor, within } from '@testing-library/react'; |
| 18 | +import { MemoryRouter } from 'react-router-dom'; |
| 19 | + |
| 20 | +interface Server { |
| 21 | + set: Record<string, unknown>; |
| 22 | + served: Array<Record<string, unknown>>; |
| 23 | + saved: Array<Record<string, unknown>>; |
| 24 | +} |
| 25 | + |
| 26 | +function freshServer(served: Array<Record<string, unknown>>): Server { |
| 27 | + return { |
| 28 | + saved: [], |
| 29 | + served, |
| 30 | + set: { |
| 31 | + name: 'sales_rep', |
| 32 | + label: 'Sales Representative', |
| 33 | + objects: { a_account: { allowRead: true, allowCreate: true } }, |
| 34 | + fields: {}, |
| 35 | + }, |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +function makeClient(server: Server) { |
| 40 | + return { |
| 41 | + layered: async () => ({ effective: server.set, code: null, overlay: null, overlayScope: null }), |
| 42 | + getDraft: async () => null, |
| 43 | + list: async (type: string) => { |
| 44 | + if (type === 'object') return [{ item: { name: 'a_account' } }]; |
| 45 | + if (type === 'permission') return server.served; |
| 46 | + return []; |
| 47 | + }, |
| 48 | + get: async (type: string) => (type === 'object' ? { fields: [{ name: 'name', label: 'Name' }] } : null), |
| 49 | + save: async (_t: string, _n: string, payload: Record<string, unknown>) => { |
| 50 | + server.saved.push(payload); |
| 51 | + server.set = payload; |
| 52 | + return payload; |
| 53 | + }, |
| 54 | + } as any; |
| 55 | +} |
| 56 | + |
| 57 | +let clientImpl: any; |
| 58 | + |
| 59 | +vi.mock('./useMetadata', () => ({ |
| 60 | + useMetadataClient: () => clientImpl, |
| 61 | + useMetadataTypes: () => ({ |
| 62 | + loading: false, |
| 63 | + error: null, |
| 64 | + entries: [{ type: 'permission', label: 'Permission', allowEnvironmentOverlay: true }], |
| 65 | + }), |
| 66 | +})); |
| 67 | + |
| 68 | +let localeFixture: 'zh-CN' | 'ja-JP' = 'zh-CN'; |
| 69 | +vi.mock('./i18n', async (importOriginal) => ({ |
| 70 | + ...(await importOriginal<Record<string, unknown>>()), |
| 71 | + useMetadataLocale: () => localeFixture, |
| 72 | +})); |
| 73 | + |
| 74 | +vi.mock('./AssignedUsersSection', () => ({ AssignedUsersSection: () => null })); |
| 75 | + |
| 76 | +import { PermissionMatrixEditPage } from './PermissionMatrixEditor'; |
| 77 | + |
| 78 | +// The editor's `perm.*` rows live in `@object-ui/i18n`'s `studio` namespace, |
| 79 | +// whose `zh` catalogue is fetched on demand (objectstack#25922). Resident |
| 80 | +// before any case, so the zh chrome reads synchronously. |
| 81 | +import { loadStudioCatalogue } from '@object-ui/i18n/namespaces/studio'; |
| 82 | +await loadStudioCatalogue('zh'); |
| 83 | + |
| 84 | +afterEach(() => { |
| 85 | + cleanup(); |
| 86 | + localeFixture = 'zh-CN'; |
| 87 | +}); |
| 88 | + |
| 89 | +function renderSet() { |
| 90 | + return render( |
| 91 | + <MemoryRouter> |
| 92 | + <PermissionMatrixEditPage type="permission" name="sales_rep" packageId="app.a" /> |
| 93 | + </MemoryRouter>, |
| 94 | + ); |
| 95 | +} |
| 96 | + |
| 97 | +describe('PermissionMatrixEditPage — header shows the served label (objectstack#26884)', () => { |
| 98 | + it('zh-CN: the header reads the served 销售代表, and a save keeps the authored label', async () => { |
| 99 | + const server = freshServer([{ name: 'sales_rep', label: '销售代表' }]); |
| 100 | + clientImpl = makeClient(server); |
| 101 | + renderSet(); |
| 102 | + |
| 103 | + expect(await screen.findByText('销售代表')).toBeInTheDocument(); |
| 104 | + expect(screen.queryByText('Sales Representative')).toBeNull(); |
| 105 | + |
| 106 | + // Any matrix edit → the autosave sends the draft (objectui#11787). |
| 107 | + const row = screen.getByText('a_account').closest('tr')!; |
| 108 | + fireEvent.click(within(row).getByRole('button', { name: /None|无/ })); |
| 109 | + await waitFor(() => expect(server.saved).toHaveLength(1), { timeout: 4000 }); |
| 110 | + expect(server.saved[0].label).toBe('Sales Representative'); |
| 111 | + // The header still names the set by the served label after the save. |
| 112 | + expect(screen.getByText('销售代表')).toBeInTheDocument(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('ja-JP: the header reads the label the served row carries', async () => { |
| 116 | + localeFixture = 'ja-JP'; |
| 117 | + clientImpl = makeClient(freshServer([{ name: 'sales_rep', label: '営業担当' }])); |
| 118 | + renderSet(); |
| 119 | + |
| 120 | + expect(await screen.findByText('営業担当')).toBeInTheDocument(); |
| 121 | + expect(screen.queryByText('Sales Representative')).toBeNull(); |
| 122 | + }); |
| 123 | + |
| 124 | + it('a set the published list does not hold keeps its draft label', async () => { |
| 125 | + clientImpl = makeClient(freshServer([{ name: 'other_set', label: '其他' }])); |
| 126 | + renderSet(); |
| 127 | + |
| 128 | + expect(await screen.findByText('Sales Representative')).toBeInTheDocument(); |
| 129 | + }); |
| 130 | + |
| 131 | + it('an edit to the label shows the edit, not the served label', async () => { |
| 132 | + clientImpl = makeClient(freshServer([{ name: 'sales_rep', label: '销售代表' }])); |
| 133 | + renderSet(); |
| 134 | + |
| 135 | + fireEvent.click(await screen.findByRole('button', { name: /销售代表/ })); |
| 136 | + fireEvent.change(screen.getByDisplayValue('Sales Representative'), { target: { value: 'Field Sales' } }); |
| 137 | + expect(screen.getByText('Field Sales')).toBeInTheDocument(); |
| 138 | + expect(screen.queryByText('销售代表')).toBeNull(); |
| 139 | + }); |
| 140 | +}); |
0 commit comments