import { describe, it, expect } from 'vitest';
import { shouldHandleEditorUndo } from './undo-shortcut';

describe( 'shouldHandleEditorUndo', () => {
	const canvas = document.createElement( 'div' );

	it( 'handles the shortcut for targets inside the block canvas', () => {
		const richText = document.createElement( 'p' );
		richText.setAttribute( 'contenteditable', 'true' );
		canvas.appendChild( richText );
		expect( shouldHandleEditorUndo( richText, canvas ) ).toBe( true );
	} );

	it( 'defers to native undo for a text input outside the canvas', () => {
		const altText = document.createElement( 'input' );
		expect( shouldHandleEditorUndo( altText, canvas ) ).toBe( false );
	} );

	it( 'defers to native undo for a textarea outside the canvas', () => {
		const textarea = document.createElement( 'textarea' );
		expect( shouldHandleEditorUndo( textarea, canvas ) ).toBe( false );
	} );

	it( 'defers to native undo for a child of an editable field outside the canvas', () => {
		const field = document.createElement( 'div' );
		field.setAttribute( 'contenteditable', 'true' );
		const child = document.createElement( 'span' );
		field.appendChild( child );
		expect( shouldHandleEditorUndo( child, canvas ) ).toBe( false );
	} );

	it( 'handles the shortcut for non-field chrome (e.g. a toolbar button)', () => {
		const button = document.createElement( 'button' );
		expect( shouldHandleEditorUndo( button, canvas ) ).toBe( true );
	} );

	it( 'handles the shortcut when the target is not an element or canvas is null', () => {
		expect( shouldHandleEditorUndo( null, canvas ) ).toBe( true );
		const button = document.createElement( 'button' );
		expect( shouldHandleEditorUndo( button, null ) ).toBe( true );
		const input = document.createElement( 'input' );
		expect( shouldHandleEditorUndo( input, null ) ).toBe( false );
	} );
} );
