Skip to content

Commit 79d5a64

Browse files
committed
Add more tests coverage
1 parent 7fff15c commit 79d5a64

3 files changed

Lines changed: 81 additions & 1 deletion

File tree

packages/block-library/src/navigation/edit/overlay-template-part-selector.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ export default function OverlayTemplatePartSelector( {
143143
} );
144144
}
145145
} catch ( error ) {
146+
// Error handling pattern matches CreateTemplatePartModalContents.
147+
// See: packages/fields/src/components/create-template-part-modal/index.tsx
148+
// The 'unknown_error' code check ensures generic error codes don't show
149+
// potentially confusing technical messages, instead showing a user-friendly fallback.
146150
const errorMessage =
147151
error instanceof Error &&
148152
'code' in error &&
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Internal dependencies
3+
*/
4+
import { getUniqueTemplatePartTitle, getCleanTemplatePartSlug } from '../utils';
5+
6+
describe( 'getUniqueTemplatePartTitle', () => {
7+
it( 'should return the title if it is unique', () => {
8+
const title = 'My Template Part';
9+
const templateParts = [
10+
{
11+
title: {
12+
rendered: 'Template Part With Another Title',
13+
},
14+
},
15+
];
16+
17+
expect( getUniqueTemplatePartTitle( title, templateParts ) ).toBe(
18+
title
19+
);
20+
} );
21+
22+
it( 'should return the title with a suffix if it is not unique', () => {
23+
const title = 'My Template Part';
24+
const templateParts = [
25+
{
26+
title: {
27+
rendered: 'My Template Part',
28+
},
29+
},
30+
];
31+
32+
expect( getUniqueTemplatePartTitle( title, templateParts ) ).toBe(
33+
'My Template Part 2'
34+
);
35+
} );
36+
37+
it( 'should return the title with correct suffix when multiple titles exist', () => {
38+
const title = 'My Template Part';
39+
const templateParts = [
40+
{
41+
title: {
42+
rendered: 'My Template Part',
43+
},
44+
},
45+
{
46+
title: {
47+
rendered: 'My Template Part 2',
48+
},
49+
},
50+
];
51+
52+
expect( getUniqueTemplatePartTitle( title, templateParts ) ).toBe(
53+
'My Template Part 3'
54+
);
55+
} );
56+
} );
57+
58+
describe( 'getCleanTemplatePartSlug', () => {
59+
it( 'should return a slug with only latin chars', () => {
60+
const title = 'Myɶ Template Partɮ';
61+
expect( getCleanTemplatePartSlug( title ) ).toBe( 'my-template-part' );
62+
} );
63+
64+
it( 'should return a slug with only latin chars and numbers', () => {
65+
const title = 'My Template Part 2';
66+
expect( getCleanTemplatePartSlug( title ) ).toBe(
67+
'my-template-part-2'
68+
);
69+
} );
70+
71+
it( 'should default the slug to wp-custom-part', () => {
72+
const title = '';
73+
expect( getCleanTemplatePartSlug( title ) ).toBe( 'wp-custom-part' );
74+
} );
75+
} );

packages/block-library/src/navigation/edit/use-create-overlay.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import { getUniqueTemplatePartTitle, getCleanTemplatePartSlug } from './utils';
1515
* Hook to create a new overlay template part.
1616
*
1717
* @param {Array} overlayTemplateParts Array of existing overlay template parts.
18-
* @return {Function} Function to create a new overlay template part.
18+
* @return {function(): Promise<Object>} Function to create a new overlay template part.
19+
* The function returns a Promise that resolves to the created template part object.
1920
*/
2021
export default function useCreateOverlayTemplatePart( overlayTemplateParts ) {
2122
const { saveEntityRecord } = useDispatch( coreStore );

0 commit comments

Comments
 (0)