import { formatHTML } from 'diffable-html-snapshot'
import { describe, expect, it } from 'vitest'
import {
katexRenderer,
mathjaxRenderer,
renderers,
setupTest,
temmlRenderer,
} from './testing.ts'
describe.each(Object.keys(renderers))('createMathBlockView (%s)', (name) => {
const renderer = renderers[name as keyof typeof renderers]
it('renders the math block DOM structure', () => {
const { editor, n } = setupTest(renderer)
editor.set(n.doc(n.mathBlock('x^2')))
const dom = editor.view.dom
const mathBlock = dom.querySelector('.prosemirror-math-block')
expect(mathBlock).toBeTruthy()
expect(mathBlock?.querySelector('.prosemirror-math-source')).toBeTruthy()
expect(mathBlock?.querySelector('.prosemirror-math-display')).toBeTruthy()
})
it('updates display when content changes', () => {
const { editor, n } = setupTest(renderer)
editor.set(n.doc(n.mathBlock('x^2')))
const display = editor.view.dom.querySelector('.prosemirror-math-display')
const initialHTML = display?.innerHTML
// Dispatch a transaction to change content
const { state } = editor.view
const tr = state.tr.insertText(' + y^2', 3)
editor.view.dispatch(tr)
const updatedHTML = display?.innerHTML
expect(updatedHTML).not.toBe(initialHTML)
})
})
describe('createMathBlockView (temml snapshot)', () => {
it('renders Temml output in the display element', () => {
const { editor, n } = setupTest(temmlRenderer)
editor.set(n.doc(n.mathBlock('x^2')))
const display = editor.view.dom.querySelector('.prosemirror-math-display')
const html = formatHTML(display?.innerHTML || '')
expect(html).toMatchInlineSnapshot(`
"
"
`)
})
})
describe('createMathBlockView (katex snapshot)', () => {
it('renders KaTeX output in the display element', () => {
const { editor, n } = setupTest(katexRenderer)
editor.set(n.doc(n.mathBlock('x^2')))
const display = editor.view.dom.querySelector('.prosemirror-math-display')
const html = formatHTML(display?.innerHTML || '')
expect(html).toMatchInlineSnapshot(`
"
x
2
"
`)
})
})
describe('createMathBlockView (mathjax snapshot)', () => {
it('renders MathJax output in the display element', () => {
const { editor, n } = setupTest(mathjaxRenderer)
editor.set(n.doc(n.mathBlock('x^2')))
const display = editor.view.dom.querySelector('.prosemirror-math-display')
const html = formatHTML(display?.innerHTML || '')
expect(html).toMatchInlineSnapshot(`
"
"
`)
})
})