import { TextDocument } from 'vscode-languageserver-textdocument'; import { Location } from 'vscode-languageserver/node'; import { onDefinition } from '../../server/src/providers/definition'; function createMockDocuments(content: string) { const doc = TextDocument.create('file:///test.html', 'html', 1, content); return { get: (uri: string) => uri === doc.uri ? doc : undefined, doc, }; } function getDefinition(content: string, offset: number): Location | null { const mock = createMockDocuments(content); const handler = onDefinition(mock as any); const position = mock.doc.positionAt(offset); return handler({ textDocument: { uri: mock.doc.uri }, position, }); } describe('DefinitionProvider', () => { it('navigates from use="id" to '.length; expect(result.uri).toBe('file:///test.html'); // The range should cover the template element const startOffset = result.range.start.line === 0 ? result.range.start.character : -1; expect(startOffset).toBe(templateStart); } }); it('navigates from then="id" to template', () => { const content = '
'; const thenIdx = content.indexOf('detail-tpl'); const result = getDefinition(content, thenIdx); expect(result).not.toBeNull(); }); it('navigates from $refs.name to ref="name"', () => { const content = '
'; // Cursor on "myInput" after "$refs." const refsIdx = content.indexOf('$refs.myInput', content.indexOf('bind=')); const result = getDefinition(content, refsIdx + 6); // on "myInput" expect(result).not.toBeNull(); if (result) { const refElement = content.indexOf(' { const content = '

'; // Cursor on "cart" after "$store." const storeIdx = content.indexOf('$store.cart'); const result = getDefinition(content, storeIdx + 7); // on "cart" expect(result).not.toBeNull(); if (result) { const storeElement = content.indexOf('
{ const content = '
'; const useIdx = content.indexOf('nonexistent'); const result = getDefinition(content, useIdx); expect(result).toBeNull(); }); it('returns null when cursor is on non-definition attribute', () => { const content = '
'; const result = getDefinition(content, 6); // on "state" expect(result).toBeNull(); }); it('navigates from loading="id" to template', () => { const content = '
'; const loadingIdx = content.indexOf('spin-tpl'); const result = getDefinition(content, loadingIdx); expect(result).not.toBeNull(); }); });