/** * Tests for renderMath and finishRenderMath stub functions. * * These are stubs for Obsidian API compatibility. MathJax is not bundled * in Reams, so: * - renderMath returns a placeholder element with the raw LaTeX source * - finishRenderMath is a no-op that immediately resolves * * Plugins that call these functions will not crash, but actual math * rendering is not supported. */ import { describe, it, expect, vi, beforeEach } from 'vitest' import { renderMath, finishRenderMath, _resetWarnedStubs } from '../obsidian-stubs' beforeEach(() => { _resetWarnedStubs() vi.restoreAllMocks() }) describe('renderMath', () => { it('returns an HTMLElement with the source as text content', () => { vi.spyOn(console, 'warn').mockImplementation(() => {}) const el = renderMath('x^2 + y^2 = z^2', false) expect(el).toBeInstanceOf(HTMLElement) expect(el.textContent).toBe('x^2 + y^2 = z^2') }) it('returns a for inline math (display=false)', () => { vi.spyOn(console, 'warn').mockImplementation(() => {}) const el = renderMath('E = mc^2', false) expect(el.tagName).toBe('SPAN') expect(el.classList.contains('math')).toBe(true) expect(el.classList.contains('math-inline')).toBe(true) }) it('returns a
for display math (display=true)', () => { vi.spyOn(console, 'warn').mockImplementation(() => {}) const el = renderMath('\\int_0^\\infty e^{-x^2} dx', true) expect(el.tagName).toBe('DIV') expect(el.classList.contains('math')).toBe(true) expect(el.classList.contains('math-block')).toBe(true) }) it('emits a console.warn on first call', () => { const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}) renderMath('x', false) expect(warnSpy).toHaveBeenCalledOnce() expect(warnSpy).toHaveBeenCalledWith( expect.stringContaining('[Reams] renderMath') ) }) it('emits console.warn only once per page-load (warn-once)', () => { const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}) renderMath('a', false) renderMath('b', true) renderMath('c', false) expect(warnSpy).toHaveBeenCalledOnce() }) it('handles empty source string', () => { vi.spyOn(console, 'warn').mockImplementation(() => {}) const el = renderMath('', false) expect(el.textContent).toBe('') }) }) describe('finishRenderMath', () => { it('returns a resolved Promise', async () => { const result = await finishRenderMath() expect(result).toBeUndefined() }) it('does not emit a warning (no-op)', async () => { const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}) await finishRenderMath() // finishRenderMath should not warn - renderMath already warns expect(warnSpy).not.toHaveBeenCalled() }) it('can be called multiple times without error', async () => { expect(await finishRenderMath()).toBeUndefined() expect(await finishRenderMath()).toBeUndefined() expect(await finishRenderMath()).toBeUndefined() }) })