Skip to content

Commit

Permalink
Merge branch 'main' into 1534-tests-use-curl-instead-of-pagegoto-in-w…
Browse files Browse the repository at this point in the history
…eb-link-validation
  • Loading branch information
michaelvlach authored Jan 28, 2025
2 parents 2ecf866 + b9d3dd7 commit 303794a
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 26 deletions.
19 changes: 10 additions & 9 deletions agdb_studio/src/components/base/table/AgdbCellMenu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@ import { mount } from "@vue/test-utils";
import AgdbCellMenu from "./AgdbCellMenu.vue";
import { describe, beforeEach, vi, it, expect } from "vitest";
import { dbActions } from "@/composables/db/dbConfig";
import { INJECT_KEY_ROW } from "@/composables/table/constants";
import {
INJECT_KEY_ROW,
INJECT_KEY_TABLE_NAME,
} from "@/composables/table/constants";
import useModal from "@/composables/modal/modal";
import { convertArrayOfStringsToContent } from "@/composables/content/utils";
import DropdownContent from "../dropdown/DropdownContent.vue";
import type { TRow } from "@/composables/table/types";
const { fetchDatabases } = vi.hoisted(() => {
const { fetchData } = vi.hoisted(() => {
return {
fetchDatabases: vi.fn(),
fetchData: vi.fn(),
};
});
const { modalIsVisible, onConfirm, modal, closeModal } = useModal();

vi.mock("@/composables/db/dbStore", () => {
vi.mock("@/composables/table/tableConfig", () => {
return {
useDbStore: () => {
return {
fetchDatabases,
};
},
fetchData,
};
});
describe("AgdbCellMenu", () => {
Expand Down Expand Up @@ -89,6 +88,7 @@ describe("AgdbCellMenu", () => {
await action.trigger("click");
await wrapper.vm.$nextTick();
expect(dropdown.isVisible()).toBe(false);
expect(fetchData).toHaveBeenCalledOnce();
});
it("should open the modal on click when confirmation is required", async () => {
const deleteAction = vi.fn();
Expand Down Expand Up @@ -122,6 +122,7 @@ describe("AgdbCellMenu", () => {
backup: 0,
},
},
[INJECT_KEY_TABLE_NAME]: "databases",
},
},
});
Expand Down
18 changes: 12 additions & 6 deletions agdb_studio/src/components/base/table/AgdbCellMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,38 @@ import type { TRow } from "@/composables/table/types";
import AgdbDropdown from "../dropdown/AgdbDropdown.vue";
import { MdRoundMenu } from "@kalimahapps/vue-icons";
import { computed, inject, type PropType, type Ref } from "vue";
import { INJECT_KEY_ROW } from "@/composables/table/constants";
import {
INJECT_KEY_ROW,
INJECT_KEY_TABLE_NAME,
} from "@/composables/table/constants";
import AgdbMenu from "../menu/AgdbMenu.vue";
import { useDbStore } from "@/composables/db/dbStore";
import useModal from "@/composables/modal/modal";
import { fetchData } from "@/composables/table/tableConfig";
const props = defineProps({
actions: { type: Array as PropType<Action<TRow>[]>, required: true },
});
const row = inject<Ref<TRow>>(INJECT_KEY_ROW);
const { fetchDatabases } = useDbStore();
const tableName = inject<Ref<string | symbol>>(INJECT_KEY_TABLE_NAME);
const { openModal } = useModal();
const mapActions = (actions: Action<TRow>[]): Action<undefined>[] => {
if (!row) return [];
return actions.map((action) => {
const runAction: ActionFn<undefined, ActionReturn> | undefined =
action.action
? ({ event }: ActionProps<undefined>): ActionReturn => {
? async ({
event,
}: ActionProps<undefined>): Promise<boolean | void> => {
/* v8 ignore next */
if (!action.action) return false;
const result = action.action({
const result = await action.action({
event,
params: row?.value,
});
fetchDatabases();
await fetchData(tableName?.value);
return result;
}
: undefined;
Expand Down
8 changes: 6 additions & 2 deletions agdb_studio/src/components/base/table/AgdbTable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ import AgdbTable from "./AgdbTable.vue";
import { addTable, clearTables } from "@/composables/table/tableConfig";
import { setTableData } from "@/composables/table/tableData";
import { TABLE_NAME, tableConfig, tableData } from "@/tests/tableMocks";
import { describe, beforeEach, it, expect } from "vitest";
import { describe, beforeEach, it, expect, vi } from "vitest";

describe("AgdbTable", () => {
beforeEach(() => {
clearTables();
});
it("should render for correct data", () => {
addTable({ name: TABLE_NAME, columns: tableConfig });
addTable({
name: TABLE_NAME,
columns: tableConfig,
fetchData: vi.fn(),
});
setTableData(TABLE_NAME, tableData);

const wrapper = mount(AgdbTable, {
Expand Down
3 changes: 3 additions & 0 deletions agdb_studio/src/components/base/table/AgdbTableRow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ vi.mock("@/composables/db/dbUsersStore", () => {
});

describe("TableRow", () => {
const fetchDataMock = vi.fn();
addTable({
name: TABLE_NAME,
columns: tableConfig,
rowDetailsComponent: "DbDetails",
fetchData: fetchDataMock,
});

it("should render", () => {
Expand Down Expand Up @@ -96,6 +98,7 @@ describe("TableRow", () => {
addTable({
name: "table_without_row_details",
columns: tableConfig,
fetchData: fetchDataMock,
});
const wrapper = mount(AgdbTableRow, {
props: {
Expand Down
3 changes: 2 additions & 1 deletion agdb_studio/src/components/db/DbTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { setTableData } from "@/composables/table/tableData";
import { watchEffect } from "vue";
import { dbColumns } from "@/composables/db/dbConfig";
const { databases, getDbName } = useDbStore();
const { databases, getDbName, fetchDatabases } = useDbStore();
const TABLE_KEY = Symbol("databases");
Expand All @@ -16,6 +16,7 @@ addTable({
rowDetailsComponent: "DbDetails",
uniqueKey: (row) =>
getDbName({ owner: row.owner.toString(), db: row.db.toString() }),
fetchData: fetchDatabases,
});
watchEffect(() => {
Expand Down
3 changes: 2 additions & 1 deletion agdb_studio/src/components/user/UserTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import { useUserStore } from "@/composables/user/userStore";
import { watchEffect } from "vue";
import AgdbTable from "../base/table/AgdbTable.vue";
const { users } = useUserStore();
const { users, fetchUsers } = useUserStore();
const TABLE_KEY = Symbol("users");
addTable({
name: TABLE_KEY,
columns: userColumns,
uniqueKey: "username",
fetchData: fetchUsers,
});
watchEffect(() => {
Expand Down
49 changes: 45 additions & 4 deletions agdb_studio/src/composables/table/tableConfig.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,29 @@ import {
getTableColumnsArray,
removeTable,
tableExists,
fetchData,
} from "./tableConfig";
import { TABLE_NAME, tableConfig, columnsMap } from "@/tests/tableMocks";
import { describe, beforeEach, it, expect } from "vitest";
import { describe, beforeEach, it, expect, vi } from "vitest";

describe("tableData", () => {
const fetchDataMock = vi.fn();
beforeEach(() => {
vi.clearAllMocks();
clearTables();
});
it("should return table configs", () => {
addTable({ name: TABLE_NAME, columns: tableConfig });
addTable({
name: TABLE_NAME,
columns: tableConfig,
fetchData: fetchDataMock,
});
const table = getTable(TABLE_NAME);
expect(table).toEqual({
name: TABLE_NAME,
columns: columnsMap,
data: new Map(),
fetchData: fetchDataMock,
});
const columns = getTableColumns(TABLE_NAME);
expect(columns).toEqual(columnsMap);
Expand All @@ -41,7 +49,11 @@ describe("tableData", () => {
});

it("should remove table", () => {
addTable({ name: TABLE_NAME, columns: tableConfig });
addTable({
name: TABLE_NAME,
columns: tableConfig,
fetchData: fetchDataMock,
});
const table = getTable(TABLE_NAME);
expect(table).toBeDefined();
removeTable(TABLE_NAME);
Expand All @@ -50,8 +62,37 @@ describe("tableData", () => {
});

it("should check if table exists", () => {
addTable({ name: TABLE_NAME, columns: tableConfig });
addTable({
name: TABLE_NAME,
columns: tableConfig,
fetchData: fetchDataMock,
});
expect(tableExists(TABLE_NAME)).toBeTruthy();
expect(tableExists("non_existent_table")).toBeFalsy();
});

it("should fetch data", async () => {
addTable({
name: TABLE_NAME,
columns: tableConfig,
fetchData: fetchDataMock,
});
await fetchData(TABLE_NAME);
expect(fetchDataMock).toHaveBeenCalled();
});

it("should fetch data for all tables", async () => {
addTable({
name: TABLE_NAME,
columns: tableConfig,
fetchData: fetchDataMock,
});
addTable({
name: "table2",
columns: tableConfig,
fetchData: fetchDataMock,
});
await fetchData(undefined);
expect(fetchDataMock).toHaveBeenCalledTimes(2);
});
});
17 changes: 17 additions & 0 deletions agdb_studio/src/composables/table/tableConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ type AddTableProps<T extends TRow> = {
columns: Column<T>[];
rowDetailsComponent?: AsyncComponent;
uniqueKey?: string | ((row: T) => string);
fetchData: () => Promise<void>;
};

const addTable = ({
name,
columns,
rowDetailsComponent,
uniqueKey,
fetchData,
}: AddTableProps<TRow>): void => {
const columnMap = new Map<string, Column<TRow>>();
columns.forEach((column) => {
Expand All @@ -28,6 +30,7 @@ const addTable = ({
data: new Map(),
rowDetailsComponent,
uniqueKey,
fetchData,
});
};

Expand Down Expand Up @@ -63,6 +66,19 @@ const clearTables = (): void => {
tables.value.clear();
};

const fetchData = async (name: symbol | string | undefined): Promise<void> => {
if (!name) {
tables.value.forEach(async (table) => {
await table.fetchData();
});
return;
}
const table = getTable(name);
if (table) {
await table.fetchData();
}
};

export {
getTable,
addTable,
Expand All @@ -71,4 +87,5 @@ export {
clearTables,
getTableColumns,
getTableColumnsArray,
fetchData,
};
6 changes: 5 additions & 1 deletion agdb_studio/src/composables/table/tableData.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import {
} from "./tableData";
import { TABLE_NAME, tableConfig, tableData } from "@/tests/tableMocks";
import { addFilter, getTableFilter, setSort } from "./tableFilter";
import { describe, beforeEach, it, expect } from "vitest";
import { describe, beforeEach, it, expect, vi } from "vitest";
import type { TRow } from "./types";

describe("tableData", () => {
const fetchDataMock = vi.fn();
addTable({
name: TABLE_NAME,
columns: tableConfig,
uniqueKey: (row: TRow) =>
`${row.owner.toString()}/${row.db.toString()}`,
fetchData: fetchDataMock,
});

beforeEach(() => {
Expand All @@ -41,6 +43,7 @@ describe("tableData", () => {
addTable({
name: "table_without_unique_key",
columns: tableConfig,
fetchData: fetchDataMock,
});
setTableData("table_without_unique_key", tableData);
const table = getTable("table_without_unique_key");
Expand All @@ -54,6 +57,7 @@ describe("tableData", () => {
{ key: "value", title: "Value" },
],
uniqueKey: "key",
fetchData: fetchDataMock,
});
setTableData("table_with_string_unique_key", [
{ key: "key1", value: "value1" },
Expand Down
4 changes: 2 additions & 2 deletions agdb_studio/src/composables/table/tableFilter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import {
removeFilter,
setSort,
} from "./tableFilter";
import { describe, beforeEach, it, expect } from "vitest";
import { describe, beforeEach, it, expect, vi } from "vitest";

describe("tableFilter", () => {
addTable({ name: TABLE_NAME, columns: tableConfig });
addTable({ name: TABLE_NAME, columns: tableConfig, fetchData: vi.fn() });
setTableData(TABLE_NAME, tableData);

beforeEach(() => {
Expand Down
1 change: 1 addition & 0 deletions agdb_studio/src/composables/table/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ export type Table<T extends TRow> = {
data?: Map<string, T>;
rowDetailsComponent?: AsyncComponent;
uniqueKey?: string | ((row: T) => string);
fetchData: () => Promise<void>;
};

0 comments on commit 303794a

Please sign in to comment.