improv: test that correct range is used for definition call

This commit is contained in:
tomasz-io 2024-11-11 12:02:46 -08:00
parent 37a50d674a
commit da62797415
2 changed files with 29 additions and 25 deletions

View file

@ -7,7 +7,10 @@ describe("RootPathContextService", () => {
"typescript",
"file1.ts",
{ start: { line: 3, character: 2 }, end: { line: 3, character: 24 } },
["EXPECTED SNIPPET 1", "EXPECTED SNIPPET 2"],
[
{ row: 2, column: 34 },
{ row: 2, column: 44 },
],
);
});
@ -16,7 +19,10 @@ describe("RootPathContextService", () => {
"typescript",
"file1.ts",
{ start: { line: 14, character: 4 }, end: { line: 14, character: 30 } },
["EXPECTED SNIPPET 1", "EXPECTED SNIPPET 2"],
[
{ row: 13, column: 33 },
{ row: 13, column: 43 },
],
);
});
});

View file

@ -7,11 +7,7 @@ import { testIde } from "../../../../test/util/fixtures";
import { getAst, getTreePathAtCursor } from "../../../util/ast";
import { ImportDefinitionsService } from "../../ImportDefinitionsService";
import { RootPathContextService } from "../RootPathContextService";
const MOCK_HARDCODED_RANGE = {
start: { line: 0, character: 0 },
end: { line: 0, character: 0 },
};
import Parser from "web-tree-sitter";
function splitTextAtRange(fileContent: string, range: Range): [string, string] {
const lines = fileContent.split("\n");
@ -47,28 +43,20 @@ export async function testRootPathContext(
folderName: string,
relativeFilepath: string,
rangeToFill: Range,
expectedSnippets: string[],
expectedDefinitionPositions: Parser.Point[],
) {
// Create a mocked instance of RootPathContextService
const ide = testIde;
const importDefinitionsService = new ImportDefinitionsService(ide);
const service = new RootPathContextService(importDefinitionsService, ide);
expectedSnippets.forEach((snippet) => {
jest
// @ts-ignore
.spyOn(service, "getSnippets")
// @ts-ignore
.mockImplementationOnce(async (_filepath, _endPosition) => {
return [
{
range: MOCK_HARDCODED_RANGE,
filepath: "NOT USED",
contents: snippet,
},
];
});
});
const getSnippetsMock = jest
// @ts-ignore
.spyOn(service, "getSnippets")
// @ts-ignore
.mockImplementation(async (_filepath, _endPosition) => {
return [];
});
// Copy the folder to the test directory
const folderPath = path.join(
@ -99,7 +87,17 @@ export async function testRootPathContext(
}
const treePath = await getTreePathAtCursor(ast, prefix.length);
const snippets = await service.getContextForPath(startPath, treePath);
await service.getContextForPath(startPath, treePath);
expect(snippets.map((snippet) => snippet.contents)).toEqual(expectedSnippets);
expect(getSnippetsMock).toHaveBeenCalledTimes(
expectedDefinitionPositions.length,
);
expectedDefinitionPositions.forEach((position, index) => {
expect(getSnippetsMock).toHaveBeenNthCalledWith(
index + 1,
expect.any(String), // filepath argument
position,
);
});
}