Skip to content

Commit 0bccdbb

Browse files
committed
Add tests for adding and removing item to expanded state
1 parent 611f6e3 commit 0bccdbb

2 files changed

Lines changed: 65 additions & 2 deletions

File tree

extensions/ql-vscode/src/databases/db-manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import { DbConfigValidationError } from "./db-validation-errors";
2525

2626
export class DbManager {
2727
public readonly onDbItemsChanged: AppEvent<void>;
28+
public static readonly DB_EXPANDED_STATE_KEY = "db_expanded";
2829
private readonly onDbItemsChangesEventEmitter: AppEventEmitter<void>;
29-
private static readonly DB_EXPANDED_STATE_KEY = "db_expanded";
3030

3131
constructor(
3232
private readonly app: App,

extensions/ql-vscode/test/unit-tests/databases/db-manager.test.ts

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@ import {
1818
RemoteRepoDbItem,
1919
VariantAnalysisUserDefinedListDbItem,
2020
} from "../../../src/databases/db-item";
21+
import {
22+
ExpandedDbItem,
23+
ExpandedDbItemKind,
24+
VariantAnalysisUserDefinedListExpandedDbItem,
25+
} from "../../../src/databases/db-item-expansion";
2126
import { DbManager } from "../../../src/databases/db-manager";
2227
import {
2328
createDbConfig,
2429
createLocalDbConfigItem,
2530
} from "../../factories/db-config-factories";
31+
import { createVariantAnalysisUserDefinedListDbItem } from "../../factories/db-item-factories";
2632
import { createMockApp } from "../../__mocks__/appMock";
2733

2834
// Note: Although these are "unit tests" (i.e. not integrating with VS Code), they do
@@ -32,12 +38,13 @@ describe("db manager", () => {
3238
let dbConfigStore: DbConfigStore;
3339
let tempWorkspaceStoragePath: string;
3440
let dbConfigFilePath: string;
41+
let app: ReturnType<typeof createMockApp>;
3542

3643
beforeEach(async () => {
3744
tempWorkspaceStoragePath = join(__dirname, "db-manager-test-workspace");
3845

3946
const extensionPath = join(__dirname, "../../..");
40-
const app = createMockApp({
47+
app = createMockApp({
4148
extensionPath,
4249
workspaceStoragePath: tempWorkspaceStoragePath,
4350
});
@@ -362,6 +369,62 @@ describe("db manager", () => {
362369
});
363370
});
364371

372+
describe("expanded behaviours", () => {
373+
it("should add item to expanded state", async () => {
374+
// Add item to config
375+
const listName = "my-list-1";
376+
const dbConfig = createDbConfig({
377+
remoteLists: [{ name: listName, repositories: [] }],
378+
});
379+
380+
await saveDbConfig(dbConfig);
381+
382+
// Add item to expanded state
383+
const dbItem = createVariantAnalysisUserDefinedListDbItem({
384+
listName,
385+
});
386+
387+
await dbManager.addDbItemToExpandedState(dbItem);
388+
const expandedItems = await app.workspaceState.get<ExpandedDbItem[]>(
389+
DbManager.DB_EXPANDED_STATE_KEY,
390+
);
391+
392+
expect(expandedItems?.length).toEqual(1);
393+
const expandedItem =
394+
expandedItems![0] as VariantAnalysisUserDefinedListExpandedDbItem;
395+
expect(expandedItem.listName).toEqual(listName);
396+
});
397+
398+
it("should remove item from expanded state", async () => {
399+
const listName = "my-list-2";
400+
const variantAnalysisList = {
401+
kind: ExpandedDbItemKind.RemoteUserDefinedList,
402+
listName,
403+
};
404+
405+
// Add item to expanded state
406+
await app.workspaceState.update(DbManager.DB_EXPANDED_STATE_KEY, [
407+
variantAnalysisList,
408+
]);
409+
let expandedItems = await app.workspaceState.get<ExpandedDbItem[]>(
410+
DbManager.DB_EXPANDED_STATE_KEY,
411+
);
412+
expect(expandedItems?.length).toEqual(1);
413+
414+
// Remove item from expanded state
415+
const dbItem = createVariantAnalysisUserDefinedListDbItem({
416+
listName,
417+
});
418+
419+
await dbManager.removeDbItemFromExpandedState(dbItem);
420+
expandedItems = await app.workspaceState.get<ExpandedDbItem[]>(
421+
DbManager.DB_EXPANDED_STATE_KEY,
422+
);
423+
424+
expect(expandedItems?.length).toEqual(0);
425+
});
426+
});
427+
365428
async function saveDbConfig(dbConfig: DbConfig): Promise<void> {
366429
await writeJson(dbConfigFilePath, dbConfig);
367430

0 commit comments

Comments
 (0)