Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template Parts: Add unit tests for template part creation functions #47224

Merged
merged 2 commits into from
Jan 18, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 return a slug of wp-custom-part', () => {
apeatling marked this conversation as resolved.
Show resolved Hide resolved
const title = '';
expect( getCleanTemplatePartSlug( title ) ).toBe( 'wp-custom-part' );
} );
} );