Skip to content

Commit a2d827d

Browse files
committed
Add unit tests for handleGoToFile
1 parent 061e792 commit a2d827d

1 file changed

Lines changed: 106 additions & 0 deletions

File tree

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { Uri } from "vscode";
2+
import { DatabaseUI } from "../../../../src/databases/local-databases-ui";
3+
import { testDisposeHandler } from "../../test-dispose-handler";
4+
import { createMockApp } from "../../../__mocks__/appMock";
5+
import { mockedObject } from "../../utils/mocking.helpers";
6+
import type { DatabaseFetcher } from "../../../../src/databases/database-fetcher";
7+
import type { DatabaseItem } from "../../../../src/databases/local-databases";
8+
import { searchSourceArchiveFiles } from "../../../../src/databases/source-archive-file-search";
9+
10+
jest.mock("../../../../src/databases/source-archive-file-search");
11+
const mockedSearchSourceArchiveFiles = jest.mocked(searchSourceArchiveFiles);
12+
13+
describe("handleGoToFile", () => {
14+
const app = createMockApp({});
15+
const storageDir = "/tmp/test-storage";
16+
17+
afterEach(() => {
18+
jest.restoreAllMocks();
19+
});
20+
21+
describe("when there is no current database", () => {
22+
const databaseUI = new DatabaseUI(
23+
app,
24+
{
25+
databaseItems: [],
26+
onDidChangeDatabaseItem: () => {
27+
/**/
28+
},
29+
onDidChangeCurrentDatabaseItem: () => {
30+
/**/
31+
},
32+
setCurrentDatabaseItem: () => {},
33+
currentDatabaseItem: undefined,
34+
} as any,
35+
mockedObject<DatabaseFetcher>({}),
36+
{
37+
onLanguageContextChanged: () => {
38+
/**/
39+
},
40+
} as any,
41+
{} as any,
42+
storageDir,
43+
storageDir,
44+
);
45+
46+
afterAll(() => {
47+
databaseUI.dispose(testDisposeHandler);
48+
});
49+
50+
it("should show an error message", async () => {
51+
const commands = databaseUI.getCommands();
52+
await commands["codeQL.goToFile"]();
53+
54+
expect(mockedSearchSourceArchiveFiles).not.toHaveBeenCalled();
55+
expect(app.logger.showErrorMessage).toHaveBeenCalledWith(
56+
expect.stringContaining("No CodeQL database selected"),
57+
);
58+
});
59+
});
60+
61+
describe("when there is a current database", () => {
62+
const mockDbItem = mockedObject<DatabaseItem>({
63+
databaseUri: Uri.file("/test/db"),
64+
name: "test-db",
65+
language: "javascript",
66+
sourceArchive: Uri.file("/test/db/src.zip"),
67+
});
68+
69+
const databaseUI = new DatabaseUI(
70+
app,
71+
{
72+
databaseItems: [mockDbItem],
73+
onDidChangeDatabaseItem: () => {
74+
/**/
75+
},
76+
onDidChangeCurrentDatabaseItem: () => {
77+
/**/
78+
},
79+
setCurrentDatabaseItem: () => {},
80+
currentDatabaseItem: mockDbItem,
81+
} as any,
82+
mockedObject<DatabaseFetcher>({}),
83+
{
84+
onLanguageContextChanged: () => {
85+
/**/
86+
},
87+
} as any,
88+
{} as any,
89+
storageDir,
90+
storageDir,
91+
);
92+
93+
afterAll(() => {
94+
databaseUI.dispose(testDisposeHandler);
95+
});
96+
97+
it("should call searchSourceArchiveFiles with the current database", async () => {
98+
mockedSearchSourceArchiveFiles.mockResolvedValue(undefined);
99+
100+
const commands = databaseUI.getCommands();
101+
await commands["codeQL.goToFile"]();
102+
103+
expect(mockedSearchSourceArchiveFiles).toHaveBeenCalledWith(mockDbItem);
104+
});
105+
});
106+
});

0 commit comments

Comments
 (0)