This commit is contained in:
Nora
2022-12-02 17:41:46 +01:00
parent ee056ce2b3
commit 9110af80d8
4 changed files with 257 additions and 33 deletions

View File

@@ -0,0 +1,40 @@
import { DbItem, DbItemKind } from "./db-item";
export function getSelectedDbItem(dbItems: DbItem[]): DbItem | undefined {
for (const dbItem of dbItems) {
if (
dbItem.kind === DbItemKind.RootRemote ||
dbItem.kind === DbItemKind.RootLocal
) {
for (const child of dbItem.children) {
switch (child.kind) {
case DbItemKind.LocalList:
if (child.selected) {
return child;
}
for (const database of child.databases) {
if (database.selected) {
return database;
}
}
break;
case DbItemKind.RemoteUserDefinedList:
if (child.selected) {
return child;
}
for (const repo of child.repos) {
if (repo.selected) {
return repo;
}
}
break;
default:
if (child.selected) {
return child;
}
}
}
}
}
return undefined;
}

View File

@@ -124,35 +124,3 @@ const SelectableDbItemKinds = [
DbItemKind.RemoteOwner,
DbItemKind.RemoteRepo,
];
export function getSelectedDbItem(dbItems: DbItem[]): DbItem | undefined {
for (const dbItem of dbItems) {
if (
dbItem.kind === DbItemKind.RootRemote ||
dbItem.kind === DbItemKind.RootLocal
) {
for (const child of dbItem.children) {
switch (child.kind) {
case DbItemKind.LocalList:
for (const database of child.databases) {
if (database.selected) {
return database;
}
}
break;
case DbItemKind.RemoteUserDefinedList:
for (const repo of child.repos) {
if (repo.selected) {
return repo;
}
}
break;
default:
if (child.selected) {
return child;
}
}
}
}
}
}

View File

@@ -2,7 +2,8 @@ import { App } from "../common/app";
import { AppEvent, AppEventEmitter } from "../common/events";
import { ValueResult } from "../common/value-result";
import { DbConfigStore } from "./config/db-config-store";
import { DbItem, getSelectedDbItem } from "./db-item";
import { DbItem } from "./db-item";
import { getSelectedDbItem } from "./db-item-selection";
import { createLocalTree, createRemoteTree } from "./db-tree-creator";
export class DbManager {

View File

@@ -0,0 +1,215 @@
import { DbItem, DbItemKind } from "../../../src/databases/db-item";
import { getSelectedDbItem } from "../../../src/databases/db-item-selection";
describe("db item selection", () => {
it("return undefined if no item is selected", () => {
const dbItems: DbItem[] = [
{
kind: DbItemKind.RootRemote,
children: [
{
kind: DbItemKind.RemoteSystemDefinedList,
listName: "top_10",
listDisplayName: "Top 10 repositories",
listDescription: "Top 10 repositories of a language",
selected: false,
},
{
kind: DbItemKind.RemoteSystemDefinedList,
listName: "top_100",
listDisplayName: "Top 100 repositories",
listDescription: "Top 100 repositories of a language",
selected: false,
},
{
kind: DbItemKind.RemoteSystemDefinedList,
listName: "top_1000",
listDisplayName: "Top 1000 repositories",
listDescription: "Top 1000 repositories of a language",
selected: false,
},
{
kind: DbItemKind.RemoteOwner,
ownerName: "github",
selected: false,
},
{
kind: DbItemKind.RemoteUserDefinedList,
listName: "my list",
repos: [
{
kind: DbItemKind.RemoteRepo,
repoFullName: "owner1/repo2",
selected: false,
},
{
kind: DbItemKind.RemoteRepo,
repoFullName: "owner1/repo3",
selected: false,
},
],
selected: false,
},
],
},
{
kind: DbItemKind.RootLocal,
children: [
{
kind: DbItemKind.LocalList,
listName: "list-1",
databases: [
{
kind: DbItemKind.LocalDatabase,
databaseName: "db1",
dateAdded: 1234,
language: "javascript",
storagePath: "/foo/bar",
selected: false,
},
{
kind: DbItemKind.LocalDatabase,
databaseName: "db2",
dateAdded: 1234,
language: "javascript",
storagePath: "/foo/bar",
selected: false,
},
],
selected: false,
},
{
kind: DbItemKind.LocalDatabase,
databaseName: "db3",
dateAdded: 1234,
language: "javascript",
storagePath: "/foo/bar",
selected: false,
},
],
},
];
expect(getSelectedDbItem(dbItems)).toBeUndefined();
});
it("return correct local database item from DbItem list", () => {
const dbItems: DbItem[] = [
{
kind: DbItemKind.RootLocal,
children: [
{
kind: DbItemKind.LocalList,
listName: "list-1",
databases: [
{
kind: DbItemKind.LocalDatabase,
databaseName: "db1",
dateAdded: 1234,
language: "javascript",
storagePath: "/foo/bar",
selected: false,
},
{
kind: DbItemKind.LocalDatabase,
databaseName: "db2",
dateAdded: 1234,
language: "javascript",
storagePath: "/foo/bar",
selected: true,
},
],
selected: false,
},
{
kind: DbItemKind.LocalDatabase,
databaseName: "db3",
dateAdded: 1234,
language: "javascript",
storagePath: "/foo/bar",
selected: false,
},
],
},
];
expect(getSelectedDbItem(dbItems)).toEqual({
kind: DbItemKind.LocalDatabase,
databaseName: "db2",
dateAdded: 1234,
language: "javascript",
storagePath: "/foo/bar",
selected: true,
});
});
it("return correct remote database list item from DbItem list", () => {
const dbItems: DbItem[] = [
{
kind: DbItemKind.RootRemote,
children: [
{
kind: DbItemKind.RemoteSystemDefinedList,
listName: "top_10",
listDisplayName: "Top 10 repositories",
listDescription: "Top 10 repositories of a language",
selected: false,
},
{
kind: DbItemKind.RemoteSystemDefinedList,
listName: "top_100",
listDisplayName: "Top 100 repositories",
listDescription: "Top 100 repositories of a language",
selected: false,
},
{
kind: DbItemKind.RemoteSystemDefinedList,
listName: "top_1000",
listDisplayName: "Top 1000 repositories",
listDescription: "Top 1000 repositories of a language",
selected: false,
},
{
kind: DbItemKind.RemoteOwner,
ownerName: "github",
selected: false,
},
{
kind: DbItemKind.RemoteUserDefinedList,
listName: "my list",
repos: [
{
kind: DbItemKind.RemoteRepo,
repoFullName: "owner1/repo2",
selected: false,
},
{
kind: DbItemKind.RemoteRepo,
repoFullName: "owner1/repo3",
selected: false,
},
],
selected: true,
},
],
},
];
expect(getSelectedDbItem(dbItems)).toEqual({
kind: DbItemKind.RemoteUserDefinedList,
listName: "my list",
repos: [
{
kind: DbItemKind.RemoteRepo,
repoFullName: "owner1/repo2",
selected: false,
},
{
kind: DbItemKind.RemoteRepo,
repoFullName: "owner1/repo3",
selected: false,
},
],
selected: true,
});
});
});