import { EditorSelection } from '@codemirror/state'; import createTestEditor from '../testing/createTestEditor'; import jumpToHash from './jumpToHash'; describe('jumpToHash', () => { test.each([ { doc: 'This is an anchor: Test', expectedCursorLocation: 'This is an anchor: '.length, waitForTags: ['HTMLTag'], }, { doc: 'HTML block: This is an anchor: Test', expectedCursorLocation: 'HTML block: This is an anchor: '.length, waitForTags: ['HTMLBlock'], }, ])('should support jumping to elements with ID set to "test" (case %#)', async ({ doc: docText, expectedCursorLocation, waitForTags }) => { const editor = await createTestEditor( docText, EditorSelection.cursor(1), waitForTags, ); expect(jumpToHash(editor, 'test')).toBe(true); const cursorPosition = editor.state.selection.main.anchor; expect( editor.state.sliceDoc(0, cursorPosition), ).toBe( editor.state.sliceDoc(0, expectedCursorLocation), ); }); test('should jump to Markdown headers', async () => { const editor = await createTestEditor( 'Line 1\n## Line 2', EditorSelection.cursor(0), ['ATXHeading2'], ); expect(jumpToHash(editor, 'line-2')).toBe(true); expect(editor.state.selection.main.anchor).toBe(editor.state.doc.length); }); test('should return false when no link target is found', async () => { const editor = await createTestEditor( 'Line 1', EditorSelection.cursor(0), [], ); expect(jumpToHash(editor, 'does-not-exist')).toBe(false); }); });