Skip to content

Commit 7a3d5c1

Browse files
committed
Add unit tests
1 parent 18c3ce2 commit 7a3d5c1

File tree

2 files changed

+109
-1
lines changed

2 files changed

+109
-1
lines changed

extensions/ql-vscode/src/remote-queries/repository-selection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export async function getRepositorySelection(
4141
if (selectedDbItem) {
4242
switch (selectedDbItem.kind) {
4343
case DbItemKind.LocalDatabase || DbItemKind.LocalList:
44-
throw new Error("Local databases and lists are not supported");
44+
throw new Error("Local databases and lists are not supported yet.");
4545
case DbItemKind.RemoteSystemDefinedList:
4646
return { repositoryLists: [selectedDbItem.listName] };
4747
case DbItemKind.RemoteUserDefinedList:

extensions/ql-vscode/src/vscode-tests/no-workspace/remote-queries/repository-selection.test.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,116 @@ import { UserCancellationException } from "../../../commandRunner";
44

55
import * as config from "../../../config";
66
import { getRepositorySelection } from "../../../remote-queries/repository-selection";
7+
import { DbManager } from "../../../databases/db-manager";
8+
import { DbItemKind } from "../../../databases/db-item";
79

810
describe("repository selection", () => {
11+
describe("newQueryRunExperience true", () => {
12+
beforeEach(() => {
13+
jest
14+
.spyOn(config, "isNewQueryRunExperienceEnabled")
15+
.mockReturnValue(true);
16+
});
17+
18+
it("should throw error when no database item is selected", async () => {
19+
const dbManager = {
20+
getSelectedDbItem: jest.fn(() => undefined),
21+
} as any as DbManager;
22+
23+
await expect(getRepositorySelection(dbManager)).rejects.toThrow(
24+
Error("Please select a remote database item to run the query against."),
25+
);
26+
});
27+
28+
it("should throw error when local database item is selected", async () => {
29+
const dbManager = {
30+
getSelectedDbItem: jest.fn(() => {
31+
return {
32+
kind: DbItemKind.LocalDatabase,
33+
};
34+
}),
35+
} as any as DbManager;
36+
37+
await expect(getRepositorySelection(dbManager)).rejects.toThrow(
38+
Error("Local databases and lists are not supported yet."),
39+
);
40+
});
41+
42+
it("should return correct selection when remote system defined list is selected", async () => {
43+
const dbManager = {
44+
getSelectedDbItem: jest.fn(() => {
45+
return {
46+
kind: DbItemKind.RemoteSystemDefinedList,
47+
listName: "top_10",
48+
};
49+
}),
50+
} as any as DbManager;
51+
52+
const repoSelection = await getRepositorySelection(dbManager);
53+
54+
expect(repoSelection.repositoryLists).toEqual(["top_10"]);
55+
expect(repoSelection.owners).toBeUndefined();
56+
expect(repoSelection.repositories).toBeUndefined();
57+
});
58+
59+
it("should return correct selection when remote user defined list is selected", async () => {
60+
const dbManager = {
61+
getSelectedDbItem: jest.fn(() => {
62+
return {
63+
kind: DbItemKind.RemoteUserDefinedList,
64+
repos: [
65+
{ repoFullName: "owner1/repo1" },
66+
{ repoFullName: "owner1/repo2" },
67+
],
68+
};
69+
}),
70+
} as any as DbManager;
71+
72+
const repoSelection = await getRepositorySelection(dbManager);
73+
74+
expect(repoSelection.repositoryLists).toBeUndefined();
75+
expect(repoSelection.owners).toBeUndefined();
76+
expect(repoSelection.repositories).toEqual([
77+
"owner1/repo1",
78+
"owner1/repo2",
79+
]);
80+
});
81+
82+
it("should return correct selection when remote owner is selected", async () => {
83+
const dbManager = {
84+
getSelectedDbItem: jest.fn(() => {
85+
return {
86+
kind: DbItemKind.RemoteOwner,
87+
ownerName: "owner2",
88+
};
89+
}),
90+
} as any as DbManager;
91+
92+
const repoSelection = await getRepositorySelection(dbManager);
93+
94+
expect(repoSelection.repositoryLists).toBeUndefined();
95+
expect(repoSelection.owners).toEqual(["owner2"]);
96+
expect(repoSelection.repositories).toBeUndefined();
97+
});
98+
99+
it("should return correct selection when remote repo is selected", async () => {
100+
const dbManager = {
101+
getSelectedDbItem: jest.fn(() => {
102+
return {
103+
kind: DbItemKind.RemoteRepo,
104+
repoFullName: "owner1/repo2",
105+
};
106+
}),
107+
} as any as DbManager;
108+
109+
const repoSelection = await getRepositorySelection(dbManager);
110+
111+
expect(repoSelection.repositoryLists).toBeUndefined();
112+
expect(repoSelection.owners).toBeUndefined();
113+
expect(repoSelection.repositories).toEqual(["owner1/repo2"]);
114+
});
115+
});
116+
9117
describe("newQueryRunExperience false", () => {
10118
let quickPickSpy: jest.SpiedFunction<typeof window.showQuickPick>;
11119
let showInputBoxSpy: jest.SpiedFunction<typeof window.showInputBox>;

0 commit comments

Comments
 (0)