Skip to content

Commit

Permalink
Template Parts: Add unit tests for template part creation functions (#…
Browse files Browse the repository at this point in the history
…47224)

* Add unit tests for template part creation functions.

* Update packages/edit-site/src/utils/test/template-part-create.js

Co-authored-by: Robert Anderson <[email protected]>

Co-authored-by: Robert Anderson <[email protected]>
  • Loading branch information
apeatling and noisysocks authored Jan 18, 2023
1 parent 37bb7c5 commit 3b99268
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions packages/edit-site/src/utils/test/template-part-create.js
Original file line number Diff line number Diff line change
@@ -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 default the slug to wp-custom-part', () => {
const title = '';
expect( getCleanTemplatePartSlug( title ) ).toBe( 'wp-custom-part' );
} );
} );

0 comments on commit 3b99268

Please sign in to comment.