import { EditorSelection } from '@codemirror/state';
import createTestEditor from '../../testing/createTestEditor';
import replaceInlineHtml from './replaceInlineHtml';
import waitFor from '@joplin/lib/testing/waitFor';
const createEditorWithCursor = async (initialMarkdown: string, cursorIndex: number, expectedTags: string[] = ['HTMLTag']) => {
const editor = await createTestEditor(
initialMarkdown,
EditorSelection.cursor(cursorIndex),
expectedTags,
[replaceInlineHtml],
);
return editor;
};
const createEditor = async (initialMarkdown: string, expectedTags: string[] = ['HTMLTag']) => {
return createEditorWithCursor(initialMarkdown, 0, expectedTags);
};
describe('replaceInlineHtml', () => {
jest.retryTimes(2);
test.each([
{ markdown: 'Test', expectedDomTags: 'sup' },
{ markdown: 'Test', expectedDomTags: 'strike' },
{ markdown: 'Test: Test', expectedDomTags: 'span[style]' },
{ markdown: 'Test: Test', expectedDomTags: 'span[style]' },
{
markdown: 'Test *test*...',
expectedDomTags: 'sup',
initialSyntaxTags: ['HTMLTag', 'Emphasis'],
},
])('should render inline HTML (case %#)', async ({ markdown, expectedDomTags: expectedTagsQuery, initialSyntaxTags }) => {
// Add additional newlines: Ensure that the cursor isn't initially on the same line as the content to be rendered:
const editor = await createEditor(`\n\n${markdown}\n\n`, initialSyntaxTags);
// Retry on failure to handle the case where the syntax tree is slow:
await waitFor(() => {
expect(editor.contentDOM.querySelector(expectedTagsQuery)).toBeTruthy();
});
});
test('should keep other inline HTML rendered when cursor is on same line, but not touching tags', async () => {
const markdown = 'A one B two';
const editor = await createEditorWithCursor(markdown, markdown.indexOf('A'));
await waitFor(() => {
expect(editor.contentDOM.querySelectorAll('sub')).toHaveLength(2);
});
});
test('should reveal only the inline HTML touched by the cursor', async () => {
const markdown = 'A one B two';
const cursorAtFirstSubContent = markdown.indexOf('one') + 1;
const editor = await createEditorWithCursor(markdown, cursorAtFirstSubContent);
await waitFor(() => {
expect(editor.contentDOM.querySelectorAll('sub')).toHaveLength(1);
});
});
test('should not hide incomplete inline HTML tags', async () => {
const markdown = 'x';
const editor = await createEditorWithCursor(markdown, markdown.length);
await waitFor(() => {
expect(editor.contentDOM.textContent).toContain('x');
});
});
test('should not style incomplete inline HTML tags', async () => {
const markdown = '';
const editor = await createEditorWithCursor(markdown, markdown.length, []);
await waitFor(() => {
expect(editor.contentDOM.querySelector('strike')).toBeFalsy();
expect(editor.contentDOM.textContent).toContain('');
});
});
});