import { describe, expect, it } from 'vitest';
import { attr } from 'hpq';
import type { BlockAttribute, BlockType } from '../../../types';
import {
getBlockAttribute,
getBlockAttributes,
parseWithAttributeSchema,
toBooleanAttributeMatcher,
isOfType,
isOfTypes,
isValidByType,
isValidByEnum,
} from '../get-block-attributes';
import { registerBlockType } from '../../registration';
describe( 'attributes parsing', () => {
describe( 'toBooleanAttributeMatcher()', () => {
const originalMatcher = attr( 'disabled' );
const enhancedMatcher = toBooleanAttributeMatcher( originalMatcher );
it( 'should return a matcher returning false on unset attribute', () => {
const node = document.createElement( 'input' );
expect( originalMatcher( node ) ).toBe( undefined );
expect( enhancedMatcher( node ) ).toBe( false );
} );
it( 'should return a matcher returning true on implicit empty string attribute value', () => {
const node = document.createElement( 'input' );
node.disabled = true;
expect( originalMatcher( node ) ).toBe( '' );
expect( enhancedMatcher( node ) ).toBe( true );
} );
it( 'should return a matcher returning true on explicit empty string attribute value', () => {
const node = document.createElement( 'input' );
node.setAttribute( 'disabled', '' );
expect( originalMatcher( node ) ).toBe( '' );
expect( enhancedMatcher( node ) ).toBe( true );
} );
it( 'should return a matcher returning true on explicit string attribute value', () => {
const node = document.createElement( 'input' );
node.setAttribute( 'disabled', 'disabled' );
expect( originalMatcher( node ) ).toBe( 'disabled' );
expect( enhancedMatcher( node ) ).toBe( true );
} );
} );
describe( 'isOfType()', () => {
it( 'gracefully handles unhandled type', () => {
expect( isOfType( 5, '__UNHANDLED__' ) ).toBe( true );
} );
it( 'returns expected result of type', () => {
expect( isOfType( '5', 'string' ) ).toBe( true );
expect( isOfType( 5, 'string' ) ).toBe( false );
expect( isOfType( 5, 'integer' ) ).toBe( true );
expect( isOfType( '5', 'integer' ) ).toBe( false );
expect( isOfType( 5, 'number' ) ).toBe( true );
expect( isOfType( '5', 'number' ) ).toBe( false );
expect( isOfType( true, 'boolean' ) ).toBe( true );
expect( isOfType( false, 'boolean' ) ).toBe( true );
expect( isOfType( '5', 'boolean' ) ).toBe( false );
expect( isOfType( 0, 'boolean' ) ).toBe( false );
expect( isOfType( null, 'null' ) ).toBe( true );
expect( isOfType( 0, 'null' ) ).toBe( false );
expect( isOfType( [], 'array' ) ).toBe( true );
expect( isOfType( {}, 'object' ) ).toBe( true );
expect( isOfType( null, 'object' ) ).toBe( false );
} );
} );
describe( 'isOfTypes', () => {
it( 'returns false if value is not one of types', () => {
expect( isOfTypes( null, [ 'string' ] ) ).toBe( false );
} );
it( 'returns true if value is one of types', () => {
expect( isOfTypes( null, [ 'string', 'null' ] ) ).toBe( true );
} );
} );
describe( 'isValidByType', () => {
it( 'returns true if type undefined', () => {
expect( isValidByType( null, undefined ) ).toBe( true );
} );
it( 'returns false if value is not one of types array', () => {
expect( isValidByType( null, [ 'string' ] ) ).toBe( false );
} );
it( 'returns true if value is one of types array', () => {
expect( isValidByType( null, [ 'string', 'null' ] ) ).toBe( true );
} );
it( 'returns false if value is not of type string', () => {
expect( isValidByType( null, 'string' ) ).toBe( false );
} );
it( 'returns true if value is type string', () => {
expect( isValidByType( null, 'null' ) ).toBe( true );
} );
} );
describe( 'isValidByEnum', () => {
it( 'returns true if enum set undefined', () => {
expect( isValidByEnum( 2, undefined ) ).toBe( true );
} );
it( 'returns false if value is not of enum set', () => {
expect( isValidByEnum( 2, [ 1, 3 ] ) ).toBe( false );
} );
it( 'returns true if value is of enum set', () => {
expect( isValidByEnum( 2, [ 1, 2, 3 ] ) ).toBe( true );
} );
} );
describe( 'parseWithAttributeSchema', () => {
it( 'should return the matcher’s attribute value', () => {
const value = parseWithAttributeSchema( '
chicken
', {
type: 'string',
source: 'text',
selector: 'div',
} );
expect( value ).toBe( 'chicken' );
} );
it( 'should return the matcher’s string attribute value', () => {
const value = parseWithAttributeSchema( '