import { describe, expect, it } from 'vitest'; import type { BlockType } from '../../../types'; import { fixCustomClassname, getHTMLRootElementClasses, } from '../fix-custom-classname'; describe( 'Fix custom className', () => { const blockSettings = { name: 'core/test-block', save: () =>
, category: 'text', title: 'block title', } as unknown as BlockType; const dynamicBlockSettings = { name: 'core/test-block', save: () => null, category: 'text', title: 'block title', } as unknown as BlockType; it( 'should do nothing if the block settings do not define custom className support', () => { const attributes = fixCustomClassname( { className: 'foo' }, { ...blockSettings, supports: { customClassName: false, }, } as unknown as BlockType, '
' ); expect( attributes.className ).toBe( 'foo' ); } ); it( 'should inject the className differences from parsed attributes', () => { const attributes = fixCustomClassname( { className: 'foo' }, blockSettings, '
' ); expect( attributes.className ).toBe( 'foo bar baz' ); } ); it( 'should assign as undefined if there are no classes', () => { const attributes = fixCustomClassname( {}, blockSettings, '
' ); expect( attributes.className ).toBeUndefined(); } ); it( 'should add a custom class name to an element without a class', () => { const attributes = fixCustomClassname( {}, blockSettings, '
' ); expect( attributes.className ).toBe( 'foo' ); } ); it( 'should remove the custom class and retain default class', () => { const attributes = fixCustomClassname( { className: 'custom1 custom2' }, blockSettings, '
' ); expect( attributes.className ).toBe( 'custom1' ); } ); it( 'should remove the custom class from an element originally without a class', () => { const attributes = fixCustomClassname( { className: 'foo' }, blockSettings, '
' ); expect( attributes.className ).toBeUndefined(); } ); it( 'should remove the custom classes and retain default and other custom class', () => { const attributes = fixCustomClassname( { className: 'custom1 custom2 custom3' }, blockSettings, '
' ); expect( attributes.className ).toBe( 'custom1 custom3' ); } ); it( 'should not remove the custom classes for dynamic blocks', () => { const attributes = fixCustomClassname( { className: 'custom1' }, dynamicBlockSettings, null as unknown as string ); expect( attributes.className ).toBe( 'custom1' ); } ); } ); describe( 'getHTMLRootElementClasses', () => { it( 'should return an empty array if there are no classes', () => { const classes = getHTMLRootElementClasses( '
' ); expect( classes ).toEqual( [] ); } ); it( 'return an array of parsed classes from inner HTML', () => { const classes = getHTMLRootElementClasses( '
' ); expect( classes ).toEqual( [ 'foo', 'bar' ] ); } ); } );