import {test, expect} from '../../fixtures/test' // editor.formatter.formatChanged() registers a callback that fires when the // format state changes as the cursor moves. Canvas toolbar buttons use this // to update their active/inactive appearance — the Bold button highlights // when the cursor is inside a bold element. Regressions here break toolbar UX. test.describe('editor.formatter.formatChanged() callback API', () => { test.beforeEach(async ({page, rcePage}) => { await page.goto('/scenarios/basic') await rcePage.waitForEditor() }) test('formatChanged returns an object with unbind method', async ({page}) => { const result = await page.evaluate(() => { // @ts-expect-error -- TinyMCE global const ed = window.tinymce.activeEditor const registration = ed.formatter.formatChanged('bold', () => {}) const hasUnbind = registration && typeof registration.unbind === 'function' if (hasUnbind) registration.unbind() return hasUnbind }) expect(result).toBe(true) }) test('formatChanged callback is a function receiving boolean state', async ({page}) => { // Verify the callback signature is correct without manually firing NodeChange // (which crashes TinyMCE when called without a proper selection element) const callbackAccepted = await page.evaluate(() => { // @ts-expect-error -- TinyMCE global const ed = window.tinymce.activeEditor let callbackType: string = 'not-called' const reg = ed.formatter.formatChanged('bold', (isActive: boolean) => { callbackType = typeof isActive }) const result = reg && typeof reg.unbind === 'function' if (result) reg.unbind() return result }) expect(callbackAccepted).toBe(true) }) test('multiple formatChanged registrations — all can be unbound', async ({page}) => { const success = await page.evaluate(() => { // @ts-expect-error -- TinyMCE global const ed = window.tinymce.activeEditor try { const r1 = ed.formatter.formatChanged('bold', () => {}) const r2 = ed.formatter.formatChanged('italic', () => {}) const r3 = ed.formatter.formatChanged('underline', () => {}) if (r1?.unbind) r1.unbind() if (r2?.unbind) r2.unbind() if (r3?.unbind) r3.unbind() return true } catch { return false } }) expect(success).toBe(true) }) test('formatChanged for h2 format — registration succeeds', async ({page}) => { const hasUnbind = await page.evaluate(() => { // @ts-expect-error -- TinyMCE global const ed = window.tinymce.activeEditor const reg = ed.formatter.formatChanged('h2', () => {}) const result = reg && typeof reg.unbind === 'function' if (result) reg.unbind() return result }) expect(hasUnbind).toBe(true) }) })