Skip to content

Commit 7928980

Browse files
committed
Create new selection test file and copy paste tests
1 parent e1bc8ae commit 7928980

2 files changed

Lines changed: 185 additions & 127 deletions

File tree

extensions/ql-vscode/test/vscode-tests/minimal-workspace/databases/db-panel.test.ts

Lines changed: 1 addition & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ import { DbManager } from "../../../../src/databases/db-manager";
99
import { DbConfigStore } from "../../../../src/databases/config/db-config-store";
1010
import { DbTreeDataProvider } from "../../../../src/databases/ui/db-tree-data-provider";
1111
import { DbItemKind, DbListKind } from "../../../../src/databases/db-item";
12-
import {
13-
DbTreeViewItem,
14-
SELECTED_DB_ITEM_RESOURCE_URI,
15-
} from "../../../../src/databases/ui/db-tree-view-item";
12+
import { DbTreeViewItem } from "../../../../src/databases/ui/db-tree-view-item";
1613
import { ExtensionApp } from "../../../../src/common/vscode/vscode-app";
1714
import { createMockExtensionContext } from "../../../factories/extension-context";
1815
import { createDbConfig } from "../../../factories/db-config-factories";
@@ -51,113 +48,6 @@ describe("db panel", () => {
5148
await remove(workspaceStoragePath);
5249
});
5350

54-
describe("selecting an item", () => {
55-
it("should mark selected remote db list as selected", async () => {
56-
const dbConfig: DbConfig = createDbConfig({
57-
remoteLists: [
58-
{
59-
name: "my-list-1",
60-
repositories: ["owner1/repo1", "owner1/repo2"],
61-
},
62-
{
63-
name: "my-list-2",
64-
repositories: ["owner2/repo1", "owner2/repo2"],
65-
},
66-
],
67-
selected: {
68-
kind: SelectedDbItemKind.VariantAnalysisUserDefinedList,
69-
listName: "my-list-2",
70-
},
71-
});
72-
73-
await saveDbConfig(dbConfig);
74-
75-
const dbTreeItems = await dbTreeDataProvider.getChildren();
76-
77-
expect(dbTreeItems).toBeTruthy();
78-
const items = dbTreeItems!;
79-
80-
const remoteRootNode = items[0];
81-
expect(remoteRootNode.dbItem).toBeTruthy();
82-
expect(remoteRootNode.dbItem?.kind).toEqual(DbItemKind.RootRemote);
83-
84-
const list1 = remoteRootNode.children.find(
85-
(c) =>
86-
c.dbItem?.kind === DbItemKind.VariantAnalysisUserDefinedList &&
87-
c.dbItem?.listName === "my-list-1",
88-
);
89-
const list2 = remoteRootNode.children.find(
90-
(c) =>
91-
c.dbItem?.kind === DbItemKind.VariantAnalysisUserDefinedList &&
92-
c.dbItem?.listName === "my-list-2",
93-
);
94-
95-
expect(list1).toBeTruthy();
96-
expect(list2).toBeTruthy();
97-
expect(isTreeViewItemSelectable(list1!)).toBeTruthy();
98-
expect(isTreeViewItemSelected(list2!)).toBeTruthy();
99-
});
100-
101-
it("should mark selected remote db inside list as selected", async () => {
102-
const dbConfig: DbConfig = createDbConfig({
103-
remoteLists: [
104-
{
105-
name: "my-list-1",
106-
repositories: ["owner1/repo1", "owner1/repo2"],
107-
},
108-
{
109-
name: "my-list-2",
110-
repositories: ["owner1/repo1", "owner2/repo2"],
111-
},
112-
],
113-
remoteRepos: ["owner1/repo1"],
114-
selected: {
115-
kind: SelectedDbItemKind.VariantAnalysisRepository,
116-
repositoryName: "owner1/repo1",
117-
listName: "my-list-2",
118-
},
119-
});
120-
121-
await saveDbConfig(dbConfig);
122-
123-
const dbTreeItems = await dbTreeDataProvider.getChildren();
124-
125-
expect(dbTreeItems).toBeTruthy();
126-
const items = dbTreeItems!;
127-
128-
const remoteRootNode = items[0];
129-
expect(remoteRootNode.dbItem).toBeTruthy();
130-
expect(remoteRootNode.dbItem?.kind).toEqual(DbItemKind.RootRemote);
131-
132-
const list2 = remoteRootNode.children.find(
133-
(c) =>
134-
c.dbItem?.kind === DbItemKind.VariantAnalysisUserDefinedList &&
135-
c.dbItem?.listName === "my-list-2",
136-
);
137-
expect(list2).toBeTruthy();
138-
139-
const repo1Node = list2?.children.find(
140-
(c) =>
141-
c.dbItem?.kind === DbItemKind.RemoteRepo &&
142-
c.dbItem?.repoFullName === "owner1/repo1",
143-
);
144-
expect(repo1Node).toBeTruthy();
145-
expect(isTreeViewItemSelected(repo1Node!)).toBeTruthy();
146-
147-
const repo2Node = list2?.children.find(
148-
(c) =>
149-
c.dbItem?.kind === DbItemKind.RemoteRepo &&
150-
c.dbItem?.repoFullName === "owner2/repo2",
151-
);
152-
expect(repo2Node).toBeTruthy();
153-
expect(isTreeViewItemSelectable(repo2Node!)).toBeTruthy();
154-
155-
for (const item of remoteRootNode.children) {
156-
expect(isTreeViewItemSelectable(item)).toBeTruthy();
157-
}
158-
});
159-
});
160-
16151
describe("addNewRemoteRepo", () => {
16252
it("should add a new remote repo", async () => {
16353
const dbConfig: DbConfig = createDbConfig({
@@ -498,22 +388,6 @@ describe("db panel", () => {
498388
expect(item.children.length).toBe(0);
499389
}
500390

501-
function isTreeViewItemSelectable(treeViewItem: DbTreeViewItem) {
502-
return (
503-
treeViewItem.resourceUri === undefined &&
504-
treeViewItem.contextValue?.includes("canBeSelected")
505-
);
506-
}
507-
508-
function isTreeViewItemSelected(treeViewItem: DbTreeViewItem) {
509-
return (
510-
treeViewItem.resourceUri?.toString(true) ===
511-
SELECTED_DB_ITEM_RESOURCE_URI &&
512-
(treeViewItem.contextValue === undefined ||
513-
!treeViewItem.contextValue.includes("canBeSelected"))
514-
);
515-
}
516-
517391
async function readDbConfigDirectly(): Promise<DbConfig> {
518392
// Read the workspace databases JSON file directly to check that the new list has been added.
519393
// We can't use the dbConfigStore's `read` function here because it depends on the file watcher
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
import { join } from "path";
2+
import { ensureDir, remove, writeJson } from "fs-extra";
3+
import {
4+
DbConfig,
5+
SelectedDbItemKind,
6+
} from "../../../../src/databases/config/db-config";
7+
import { DbManager } from "../../../../src/databases/db-manager";
8+
import { DbConfigStore } from "../../../../src/databases/config/db-config-store";
9+
import { DbTreeDataProvider } from "../../../../src/databases/ui/db-tree-data-provider";
10+
import { DbItemKind } from "../../../../src/databases/db-item";
11+
import {
12+
DbTreeViewItem,
13+
SELECTED_DB_ITEM_RESOURCE_URI,
14+
} from "../../../../src/databases/ui/db-tree-view-item";
15+
import { ExtensionApp } from "../../../../src/common/vscode/vscode-app";
16+
import { createMockExtensionContext } from "../../../factories/extension-context";
17+
import { createDbConfig } from "../../../factories/db-config-factories";
18+
19+
describe("db panel selection", () => {
20+
const workspaceStoragePath = join(__dirname, "test-workspace-storage");
21+
const globalStoragePath = join(__dirname, "test-global-storage");
22+
const extensionPath = join(__dirname, "../../../../");
23+
const dbConfigFilePath = join(
24+
workspaceStoragePath,
25+
DbConfigStore.databaseConfigFileName,
26+
);
27+
let dbTreeDataProvider: DbTreeDataProvider;
28+
let dbManager: DbManager;
29+
let dbConfigStore: DbConfigStore;
30+
31+
beforeAll(async () => {
32+
const extensionContext = createMockExtensionContext({
33+
extensionPath,
34+
globalStoragePath,
35+
workspaceStoragePath,
36+
});
37+
await ensureDir(workspaceStoragePath);
38+
39+
const app = new ExtensionApp(extensionContext);
40+
41+
dbConfigStore = new DbConfigStore(app, false);
42+
dbManager = new DbManager(app, dbConfigStore);
43+
});
44+
45+
beforeEach(async () => {
46+
await ensureDir(workspaceStoragePath);
47+
});
48+
49+
afterEach(async () => {
50+
await remove(workspaceStoragePath);
51+
});
52+
53+
it("should mark selected remote db list as selected", async () => {
54+
const dbConfig: DbConfig = createDbConfig({
55+
remoteLists: [
56+
{
57+
name: "my-list-1",
58+
repositories: ["owner1/repo1", "owner1/repo2"],
59+
},
60+
{
61+
name: "my-list-2",
62+
repositories: ["owner2/repo1", "owner2/repo2"],
63+
},
64+
],
65+
selected: {
66+
kind: SelectedDbItemKind.VariantAnalysisUserDefinedList,
67+
listName: "my-list-2",
68+
},
69+
});
70+
71+
await saveDbConfig(dbConfig);
72+
73+
const dbTreeItems = await dbTreeDataProvider.getChildren();
74+
75+
expect(dbTreeItems).toBeTruthy();
76+
const items = dbTreeItems!;
77+
78+
const remoteRootNode = items[0];
79+
expect(remoteRootNode.dbItem).toBeTruthy();
80+
expect(remoteRootNode.dbItem?.kind).toEqual(DbItemKind.RootRemote);
81+
82+
const list1 = remoteRootNode.children.find(
83+
(c) =>
84+
c.dbItem?.kind === DbItemKind.VariantAnalysisUserDefinedList &&
85+
c.dbItem?.listName === "my-list-1",
86+
);
87+
const list2 = remoteRootNode.children.find(
88+
(c) =>
89+
c.dbItem?.kind === DbItemKind.VariantAnalysisUserDefinedList &&
90+
c.dbItem?.listName === "my-list-2",
91+
);
92+
93+
expect(list1).toBeTruthy();
94+
expect(list2).toBeTruthy();
95+
expect(isTreeViewItemSelectable(list1!)).toBeTruthy();
96+
expect(isTreeViewItemSelected(list2!)).toBeTruthy();
97+
});
98+
99+
it("should mark selected remote db inside list as selected", async () => {
100+
const dbConfig: DbConfig = createDbConfig({
101+
remoteLists: [
102+
{
103+
name: "my-list-1",
104+
repositories: ["owner1/repo1", "owner1/repo2"],
105+
},
106+
{
107+
name: "my-list-2",
108+
repositories: ["owner1/repo1", "owner2/repo2"],
109+
},
110+
],
111+
remoteRepos: ["owner1/repo1"],
112+
selected: {
113+
kind: SelectedDbItemKind.VariantAnalysisRepository,
114+
repositoryName: "owner1/repo1",
115+
listName: "my-list-2",
116+
},
117+
});
118+
119+
await saveDbConfig(dbConfig);
120+
121+
const dbTreeItems = await dbTreeDataProvider.getChildren();
122+
123+
expect(dbTreeItems).toBeTruthy();
124+
const items = dbTreeItems!;
125+
126+
const remoteRootNode = items[0];
127+
expect(remoteRootNode.dbItem).toBeTruthy();
128+
expect(remoteRootNode.dbItem?.kind).toEqual(DbItemKind.RootRemote);
129+
130+
const list2 = remoteRootNode.children.find(
131+
(c) =>
132+
c.dbItem?.kind === DbItemKind.VariantAnalysisUserDefinedList &&
133+
c.dbItem?.listName === "my-list-2",
134+
);
135+
expect(list2).toBeTruthy();
136+
137+
const repo1Node = list2?.children.find(
138+
(c) =>
139+
c.dbItem?.kind === DbItemKind.RemoteRepo &&
140+
c.dbItem?.repoFullName === "owner1/repo1",
141+
);
142+
expect(repo1Node).toBeTruthy();
143+
expect(isTreeViewItemSelected(repo1Node!)).toBeTruthy();
144+
145+
const repo2Node = list2?.children.find(
146+
(c) =>
147+
c.dbItem?.kind === DbItemKind.RemoteRepo &&
148+
c.dbItem?.repoFullName === "owner2/repo2",
149+
);
150+
expect(repo2Node).toBeTruthy();
151+
expect(isTreeViewItemSelectable(repo2Node!)).toBeTruthy();
152+
153+
for (const item of remoteRootNode.children) {
154+
expect(isTreeViewItemSelectable(item)).toBeTruthy();
155+
}
156+
});
157+
158+
async function saveDbConfig(dbConfig: DbConfig): Promise<void> {
159+
await writeJson(dbConfigFilePath, dbConfig);
160+
161+
// Ideally we would just initialise the db config store at the start
162+
// of each test and then rely on the file watcher to update the config.
163+
// However, this requires adding sleep to the tests to allow for the
164+
// file watcher to catch up, so we instead initialise the config store here.
165+
await dbConfigStore.initialize();
166+
dbTreeDataProvider = new DbTreeDataProvider(dbManager);
167+
}
168+
169+
function isTreeViewItemSelectable(treeViewItem: DbTreeViewItem) {
170+
return (
171+
treeViewItem.resourceUri === undefined &&
172+
treeViewItem.contextValue?.includes("canBeSelected")
173+
);
174+
}
175+
176+
function isTreeViewItemSelected(treeViewItem: DbTreeViewItem) {
177+
return (
178+
treeViewItem.resourceUri?.toString(true) ===
179+
SELECTED_DB_ITEM_RESOURCE_URI &&
180+
(treeViewItem.contextValue === undefined ||
181+
!treeViewItem.contextValue.includes("canBeSelected"))
182+
);
183+
}
184+
});

0 commit comments

Comments
 (0)