|
| 1 | +import { jest } from "@jest/globals"; |
1 | 2 | import fs from "fs";
|
2 | 3 | import path from "path";
|
3 | 4 |
|
4 |
| -import { Range } from "../../.."; |
5 |
| -import { testIde } from "../../../test/util/fixtures"; |
6 |
| -import { getAst, getTreePathAtCursor } from "../../util/ast"; |
7 |
| -import { ImportDefinitionsService } from "../ImportDefinitionsService"; |
8 |
| - |
9 |
| -import { RootPathContextService } from "./RootPathContextService"; |
| 5 | +import Parser from "web-tree-sitter"; |
| 6 | +import { Range } from "../../../.."; |
| 7 | +import { testIde } from "../../../../test/util/fixtures"; |
| 8 | +import { getAst, getTreePathAtCursor } from "../../../util/ast"; |
| 9 | +import { ImportDefinitionsService } from "../../ImportDefinitionsService"; |
| 10 | +import { RootPathContextService } from "../RootPathContextService"; |
10 | 11 |
|
11 | 12 | function splitTextAtRange(fileContent: string, range: Range): [string, string] {
|
12 | 13 | const lines = fileContent.split("\n");
|
@@ -42,12 +43,21 @@ export async function testRootPathContext(
|
42 | 43 | folderName: string,
|
43 | 44 | relativeFilepath: string,
|
44 | 45 | rangeToFill: Range,
|
45 |
| - expectedSnippets: string[], |
| 46 | + expectedDefinitionPositions: Parser.Point[], |
46 | 47 | ) {
|
| 48 | + // Create a mocked instance of RootPathContextService |
47 | 49 | const ide = testIde;
|
48 | 50 | const importDefinitionsService = new ImportDefinitionsService(ide);
|
49 | 51 | const service = new RootPathContextService(importDefinitionsService, ide);
|
50 | 52 |
|
| 53 | + const getSnippetsMock = jest |
| 54 | + // @ts-ignore |
| 55 | + .spyOn(service, "getSnippets") |
| 56 | + // @ts-ignore |
| 57 | + .mockImplementation(async (_filepath, _endPosition) => { |
| 58 | + return []; |
| 59 | + }); |
| 60 | + |
51 | 61 | // Copy the folder to the test directory
|
52 | 62 | const folderPath = path.join(
|
53 | 63 | __dirname,
|
@@ -77,23 +87,17 @@ export async function testRootPathContext(
|
77 | 87 | }
|
78 | 88 |
|
79 | 89 | const treePath = await getTreePathAtCursor(ast, prefix.length);
|
80 |
| - const snippets = await service.getContextForPath(startPath, treePath); |
| 90 | + await service.getContextForPath(startPath, treePath); |
81 | 91 |
|
82 |
| - expectedSnippets.forEach((expectedSnippet) => { |
83 |
| - const found = snippets.find((snippet) => |
84 |
| - snippet.contents.includes(expectedSnippet), |
85 |
| - ); |
86 |
| - expect(found).toBeDefined(); |
87 |
| - }); |
88 |
| -} |
| 92 | + expect(getSnippetsMock).toHaveBeenCalledTimes( |
| 93 | + expectedDefinitionPositions.length, |
| 94 | + ); |
89 | 95 |
|
90 |
| -describe("RootPathContextService", () => { |
91 |
| - it.skip("should be true", async () => { |
92 |
| - await testRootPathContext( |
93 |
| - "typescript", |
94 |
| - "file1.ts", |
95 |
| - { start: { line: 3, character: 2 }, end: { line: 3, character: 24 } }, |
96 |
| - ["export interface Person", "export interface Address"], |
| 96 | + expectedDefinitionPositions.forEach((position, index) => { |
| 97 | + expect(getSnippetsMock).toHaveBeenNthCalledWith( |
| 98 | + index + 1, |
| 99 | + expect.any(String), // filepath argument |
| 100 | + position, |
97 | 101 | );
|
98 | 102 | });
|
99 |
| -}); |
| 103 | +} |
0 commit comments