1212 * the environment scope, and its permission list is the registry's whole
1313 * list, as Setup's catalog is.
1414 * - The datasource tile opens Setup's Datasources page.
15+ * - objectstack#36905 — a read-only base's list offers "New similar" on the
16+ * base's own rows (not on its pair's), the copy opens at its metadata
17+ * address in the package the clone door names, an answer naming no
18+ * package opens nothing and says so; a writable package offers none.
1519 */
1620
1721import '@testing-library/jest-dom/vitest' ;
@@ -20,7 +24,8 @@ import '@testing-library/jest-dom/vitest';
2024import '@objectstack/lint' ;
2125import * as React from 'react' ;
2226import { describe , it , expect , vi , afterEach , beforeEach } from 'vitest' ;
23- import { render , screen , cleanup , waitFor } from '@testing-library/react' ;
27+ import { render , screen , cleanup , fireEvent , waitFor , within } from '@testing-library/react' ;
28+ import { toast } from 'sonner' ;
2429import { MemoryRouter , Route , Routes , useLocation } from 'react-router-dom' ;
2530// The switch column waits on `audienceAnchorsOf`'s dynamic import of this exact
2631// specifier (see `ResourceListPage.environmentScope-7611.test.tsx`).
@@ -38,6 +43,9 @@ const PERMISSION_SETS = [
3843 { name : 'env_authored_set' , label : 'Environment Set' } ,
3944] ;
4045
46+ /** Sets a case adds to the served list (a base's pair's, for "New similar"), emptied after each case. */
47+ const EXTRA_SETS : Array < Record < string , unknown > > = [ ] ;
48+
4149const CLIENT = {
4250 list : async ( type : string ) =>
4351 type === 'package'
@@ -46,7 +54,7 @@ const CLIENT = {
4654 { manifest : { id : PROJECT , scope : 'project' , name : 'CRM' } } ,
4755 ]
4856 : type === 'permission'
49- ? PERMISSION_SETS
57+ ? [ ... PERMISSION_SETS , ... EXTRA_SETS ]
5058 : [ ] ,
5159} ;
5260
@@ -97,7 +105,7 @@ vi.mock('../../hooks/useCanAuthorMetadata', async (importOriginal) => ({
97105
98106import { MetadataDirectoryPage } from './DirectoryPage' ;
99107import { MetadataResourceListPage } from './ResourceListPage' ;
100- import { t } from './i18n' ;
108+ import { t , tFormat } from './i18n' ;
101109
102110beforeEach ( ( ) => {
103111 vi . stubGlobal (
@@ -115,6 +123,8 @@ beforeEach(() => {
115123afterEach ( ( ) => {
116124 cleanup ( ) ;
117125 vi . unstubAllGlobals ( ) ;
126+ vi . restoreAllMocks ( ) ;
127+ EXTRA_SETS . length = 0 ;
118128} ) ;
119129
120130const crmArea = ( readOnly = false ) : MetadataAreaHost => ( {
@@ -236,3 +246,93 @@ describe('the list under the area', () => {
236246 expect ( where ( ) ) . toBe ( '/studio/~all/metadata/permission?scope=environment' ) ;
237247 } ) ;
238248} ) ;
249+
250+ /**
251+ * objectstack#36905 (card item 2) — "New similar" on the rows of a read-only
252+ * base's list of sets: the row action the Access pillar's rail had, on the
253+ * REAL list page, over the REAL clone dialog and clone door (`fetch`).
254+ */
255+ describe ( '"New similar" on a read-only base\'s rows (objectstack#36905)' , ( ) => {
256+ const PAIR = `${ PROJECT } .custom` ;
257+ const LIST_URL = `/studio/${ PROJECT } /metadata/permission?package=${ PROJECT } ` ;
258+ const newSimilar = ( name : string ) => screen . queryByTestId ( `list-new-similar-${ name } ` ) ;
259+
260+ /** The clone door answers `data`; every other read answers as the harness does. */
261+ function serveClone ( data : Record < string , unknown > ) {
262+ const calls : string [ ] = [ ] ;
263+ vi . stubGlobal (
264+ 'fetch' ,
265+ vi . fn ( async ( url : string ) => {
266+ const json = ( status : number , body : unknown ) =>
267+ new Response ( JSON . stringify ( body ) , { status, headers : { 'Content-Type' : 'application/json' } } ) ;
268+ if ( String ( url ) . includes ( '/security/permission-sets/' ) ) {
269+ calls . push ( String ( url ) ) ;
270+ return json ( 200 , { success : true , data } ) ;
271+ }
272+ if ( String ( url ) . startsWith ( '/api/v1/data/sys_metadata_activation' ) ) {
273+ return json ( 200 , { object : 'sys_metadata_activation' , records : [ ] , hasMore : false } ) ;
274+ }
275+ return json ( 404 , { success : false } ) ;
276+ } ) ,
277+ ) ;
278+ return calls ;
279+ }
280+
281+ async function openBaseList ( ) {
282+ EXTRA_SETS . push ( { name : 'crm_rep_copy' , label : 'CRM Rep copy' , _packageId : PAIR , _provenance : 'package' } ) ;
283+ const view = mount ( crmArea ( true ) , LIST_URL , < MetadataResourceListPage type = "permission" /> ) ;
284+ await waitFor ( ( ) => expect ( rowLink ( view . container , 'crm_rep' ) ) . not . toBeNull ( ) ) ;
285+ await waitFor ( ( ) => expect ( rowLink ( view . container , 'crm_rep_copy' ) ) . not . toBeNull ( ) ) ;
286+ return view ;
287+ }
288+
289+ async function copyFrom ( name : string ) {
290+ fireEvent . click ( newSimilar ( name ) ! ) ;
291+ const dialog = await screen . findByRole ( 'dialog' ) ;
292+ fireEvent . change ( within ( dialog ) . getByLabelText ( t ( 'perm.clone.name' , 'en-US' ) ) , { target : { value : 'my_rep' } } ) ;
293+ fireEvent . change ( within ( dialog ) . getByLabelText ( t ( 'perm.clone.label' , 'en-US' ) ) , { target : { value : 'My rep' } } ) ;
294+ fireEvent . click ( within ( dialog ) . getByRole ( 'button' , { name : t ( 'perm.clone.confirm' , 'en-US' ) } ) ) ;
295+ }
296+
297+ const rowLink = ( container : HTMLElement , name : string ) => container . querySelector < HTMLAnchorElement > ( `a[href*="/${ name } ?"]` ) ;
298+
299+ it ( "the base's own row offers it; its pair's row does not" , async ( ) => {
300+ serveClone ( { } ) ;
301+ await openBaseList ( ) ;
302+ expect ( newSimilar ( 'crm_rep' ) ) . toBeInTheDocument ( ) ;
303+ expect ( newSimilar ( 'crm_rep_copy' ) ) . toBeNull ( ) ;
304+ } ) ;
305+
306+ it ( 'the copy opens at its metadata address in the package the clone door names' , async ( ) => {
307+ const calls = serveClone ( { permissionSet : { name : 'my_rep' , label : 'My rep' } , package : PAIR , notice : 'Saved.' } ) ;
308+ await openBaseList ( ) ;
309+ await copyFrom ( 'crm_rep' ) ;
310+ await waitFor ( ( ) =>
311+ expect ( where ( ) ) . toBe ( `/studio/${ PAIR } /metadata/permission/my_rep?package=${ encodeURIComponent ( PAIR ) } ` ) ,
312+ ) ;
313+ expect ( calls ) . toHaveLength ( 1 ) ;
314+ expect ( calls [ 0 ] ) . toContain ( '/security/permission-sets/crm_rep/clone' ) ;
315+ } ) ;
316+
317+ it ( 'an answer naming no package opens nothing and says so' , async ( ) => {
318+ const warning = vi . spyOn ( toast , 'warning' ) ;
319+ serveClone ( { permissionSet : { name : 'my_rep' , label : 'My rep' } , notice : 'Saved.' } ) ;
320+ await openBaseList ( ) ;
321+ await copyFrom ( 'crm_rep' ) ;
322+ await waitFor ( ( ) =>
323+ expect ( warning ) . toHaveBeenCalledWith (
324+ tFormat ( 'engine.studio.access.newSimilar.noPackage' , 'en-US' , { name : 'my_rep' } ) ,
325+ expect . anything ( ) ,
326+ ) ,
327+ ) ;
328+ expect ( where ( ) ) . toBe ( LIST_URL ) ;
329+ } ) ;
330+
331+ it ( 'CONTROL: a writable package offers no New similar, and offers create' , async ( ) => {
332+ serveClone ( { } ) ;
333+ const { container } = mount ( crmArea ( ) , LIST_URL , < MetadataResourceListPage type = "permission" /> ) ;
334+ await waitFor ( ( ) => expect ( rowLink ( container , 'crm_rep' ) ) . not . toBeNull ( ) ) ;
335+ expect ( newSimilar ( 'crm_rep' ) ) . toBeNull ( ) ;
336+ expect ( screen . getByTestId ( 'metadata-list-create' ) ) . toBeInTheDocument ( ) ;
337+ } ) ;
338+ } ) ;
0 commit comments