import { flushPromises, mount } from '@vue/test-utils';
|
|
import { describe, expect, it, vi } from 'vitest';
|
|
import FormulaPreview from '../FormulaPreview.vue';
|
import previewSource from '../FormulaPreview.vue?raw';
|
|
const katexMocks = vi.hoisted(() => ({ render: vi.fn() }));
|
|
vi.mock('katex', () => ({ default: { render: katexMocks.render } }));
|
|
describe('eLN formula preview', () => {
|
it('renders the current valid formula immediately without an expand action', async () => {
|
const wrapper = mount(FormulaPreview, {
|
props: { validation: { diagnostics: [], latex: 'x^2' } },
|
global: { stubs: { 'a-alert': true, 'a-empty': true } },
|
});
|
await flushPromises();
|
|
await vi.waitFor(() =>
|
expect(katexMocks.render).toHaveBeenCalledWith('x^2', expect.any(HTMLElement), expect.objectContaining({ displayMode: true, trust: false })),
|
);
|
expect(wrapper.find('button').exists()).toBe(false);
|
expect(previewSource).toContain("await import('katex')");
|
expect(previewSource).not.toMatch(/^import .* from 'katex'/m);
|
});
|
|
it('only accepts an error-free current latex response with restrictive render options', async () => {
|
const wrapper = mount(FormulaPreview, {
|
props: { validation: { diagnostics: [], latex: 'x^2' } },
|
global: { stubs: { 'a-alert': { props: ['message'], template: '<div>{{ message }}</div>' }, 'a-empty': true } },
|
});
|
await flushPromises();
|
const replaceChildren = vi.spyOn(wrapper.find('.formula-preview__surface').element, 'replaceChildren');
|
|
await wrapper.setProps({ validation: { diagnostics: [{ end: 1, message: '括号未闭合', severity: 'ERROR', start: 0 }], latex: 'old' } });
|
await flushPromises();
|
expect(wrapper.text()).toContain('括号未闭合');
|
expect(replaceChildren).toHaveBeenCalled();
|
expect(previewSource).toContain('hasValidationError');
|
expect(previewSource).toContain("await import('katex/dist/katex.min.css')");
|
expect(previewSource).toContain("strict: 'warn'");
|
expect(previewSource).toContain('throwOnError: false');
|
expect(previewSource).toContain('trust: false');
|
expect(previewSource).not.toContain('v-html');
|
});
|
});
|