|
| 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 | +} ); |
0 commit comments