From d93e77fdbb1ea94a0d6b953cfecdab9902472dee Mon Sep 17 00:00:00 2001 From: apeatling Date: Tue, 17 Jan 2023 11:32:07 -0800 Subject: [PATCH 1/2] Add unit tests for template part creation functions. --- .../src/utils/test/template-part-create.js | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 packages/edit-site/src/utils/test/template-part-create.js diff --git a/packages/edit-site/src/utils/test/template-part-create.js b/packages/edit-site/src/utils/test/template-part-create.js new file mode 100644 index 00000000000000..2948ad2577e2e8 --- /dev/null +++ b/packages/edit-site/src/utils/test/template-part-create.js @@ -0,0 +1,63 @@ +/** + * Internal dependencies + */ +import { + getUniqueTemplatePartTitle, + getCleanTemplatePartSlug, +} from '../template-part-create'; + +describe( 'getUniqueTemplatePartTitle', () => { + it( 'should return the title if it is unique', () => { + const title = 'My Template Part'; + const templateParts = [ + { + title: { + rendered: 'Template Part With Another Title', + }, + }, + ]; + + expect( getUniqueTemplatePartTitle( title, templateParts ) ).toBe( + title + ); + } ); + + it( 'should return the title with a suffix if it is not unique', () => { + const title = 'My Template Part'; + const templateParts = [ + { + title: { + rendered: 'My Template Part', + }, + }, + { + title: { + rendered: 'My Template Part 2', + }, + }, + ]; + + expect( getUniqueTemplatePartTitle( title, templateParts ) ).toBe( + 'My Template Part 3' + ); + } ); +} ); + +describe( 'getCleanTemplatePartSlug', () => { + it( 'should return a slug with only latin chars', () => { + const title = 'Myɶ Template Partɮ'; + expect( getCleanTemplatePartSlug( title ) ).toBe( 'my-template-part' ); + } ); + + it( 'should return a slug with only latin chars and numbers', () => { + const title = 'My Template Part 2'; + expect( getCleanTemplatePartSlug( title ) ).toBe( + 'my-template-part-2' + ); + } ); + + it( 'should return a slug of wp-custom-part', () => { + const title = ''; + expect( getCleanTemplatePartSlug( title ) ).toBe( 'wp-custom-part' ); + } ); +} ); From cef314b5147272bb716f0a13e2793d402b1002fb Mon Sep 17 00:00:00 2001 From: Andy Peatling Date: Tue, 17 Jan 2023 15:52:51 -0800 Subject: [PATCH 2/2] Update packages/edit-site/src/utils/test/template-part-create.js Co-authored-by: Robert Anderson --- packages/edit-site/src/utils/test/template-part-create.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/edit-site/src/utils/test/template-part-create.js b/packages/edit-site/src/utils/test/template-part-create.js index 2948ad2577e2e8..e39513ccf7e4f4 100644 --- a/packages/edit-site/src/utils/test/template-part-create.js +++ b/packages/edit-site/src/utils/test/template-part-create.js @@ -56,7 +56,7 @@ describe( 'getCleanTemplatePartSlug', () => { ); } ); - it( 'should return a slug of wp-custom-part', () => { + it( 'should default the slug to wp-custom-part', () => { const title = ''; expect( getCleanTemplatePartSlug( title ) ).toBe( 'wp-custom-part' ); } );