From dd505b87eb917a6fcef61e6413bd4fb2c8c87201 Mon Sep 17 00:00:00 2001 From: davelopez <46503462+davelopez@users.noreply.github.com> Date: Thu, 1 Aug 2024 14:30:34 +0200 Subject: [PATCH] Refactor client API mocks and imports --- client/src/api/__mocks__/index.ts | 7 ------- client/src/api/client/__mocks__/index.ts | 17 +++++++++++++++++ client/src/api/{ => client}/__mocks__/node.ts | 0 client/src/api/client/index.ts | 18 ++++++++++++++++++ .../api/{schema => client}/mockClient.test.ts | 8 ++++++-- client/src/api/index.ts | 7 +++++-- client/src/api/schema/index.ts | 6 ------ client/tests/jest/jest.setup.js | 2 +- 8 files changed, 47 insertions(+), 18 deletions(-) delete mode 100644 client/src/api/__mocks__/index.ts create mode 100644 client/src/api/client/__mocks__/index.ts rename client/src/api/{ => client}/__mocks__/node.ts (100%) create mode 100644 client/src/api/client/index.ts rename client/src/api/{schema => client}/mockClient.test.ts (90%) diff --git a/client/src/api/__mocks__/index.ts b/client/src/api/__mocks__/index.ts deleted file mode 100644 index f0e1e01cdcc8..000000000000 --- a/client/src/api/__mocks__/index.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { createOpenApiHttp } from "openapi-msw"; - -import { type GalaxyApiPaths } from "@/api"; - -const clientMock = createOpenApiHttp(); - -export { clientMock }; diff --git a/client/src/api/client/__mocks__/index.ts b/client/src/api/client/__mocks__/index.ts new file mode 100644 index 000000000000..9c14a46d6426 --- /dev/null +++ b/client/src/api/client/__mocks__/index.ts @@ -0,0 +1,17 @@ +import { createOpenApiHttp } from "openapi-msw"; + +import { type GalaxyApiPaths } from "@/api/schema"; + +function createApiClientMock() { + return createOpenApiHttp(); +} + +let clientMock: ReturnType; + +export function useClientApiMock() { + if (!clientMock) { + clientMock = createApiClientMock(); + } + + return { clientMock }; +} diff --git a/client/src/api/__mocks__/node.ts b/client/src/api/client/__mocks__/node.ts similarity index 100% rename from client/src/api/__mocks__/node.ts rename to client/src/api/client/__mocks__/node.ts diff --git a/client/src/api/client/index.ts b/client/src/api/client/index.ts new file mode 100644 index 000000000000..255e687b5478 --- /dev/null +++ b/client/src/api/client/index.ts @@ -0,0 +1,18 @@ +import createClient from "openapi-fetch"; + +import { type GalaxyApiPaths } from "@/api/schema"; +import { getAppRoot } from "@/onload/loadConfig"; + +function createApiClient() { + return createClient({ baseUrl: getAppRoot(undefined, true) }); +} + +let client: ReturnType; + +export function useClientApi() { + if (!client) { + client = createApiClient(); + } + + return { client }; +} diff --git a/client/src/api/schema/mockClient.test.ts b/client/src/api/client/mockClient.test.ts similarity index 90% rename from client/src/api/schema/mockClient.test.ts rename to client/src/api/client/mockClient.test.ts index ac3c80716109..13b0d9160b75 100644 --- a/client/src/api/schema/mockClient.test.ts +++ b/client/src/api/client/mockClient.test.ts @@ -1,5 +1,9 @@ -import { client, type HistoryDetailed, type HistorySummary, type MessageException } from "@/api"; -import { clientMock } from "@/api/__mocks__"; +import { type HistoryDetailed, type HistorySummary, type MessageException } from "@/api"; +import { useClientApi } from "@/api/client"; +import { useClientApiMock } from "@/api/client/__mocks__"; + +const { clientMock } = useClientApiMock(); +const { client } = useClientApi(); const TEST_HISTORY_SUMMARY: HistorySummary = { model_class: "History", diff --git a/client/src/api/index.ts b/client/src/api/index.ts index 2f59986b27a0..5ab4228b7b68 100644 --- a/client/src/api/index.ts +++ b/client/src/api/index.ts @@ -1,8 +1,11 @@ /** Contains type alias and definitions related to Galaxy API models. */ -import { client, type components, type GalaxyApiPaths } from "@/api/schema"; +import { useClientApi } from "@/api/client"; +import { type components, type GalaxyApiPaths } from "@/api/schema"; -export { client, type components, type GalaxyApiPaths }; +export { type components, type GalaxyApiPaths }; + +export const { client } = useClientApi(); /** * Contains minimal information about a History. diff --git a/client/src/api/schema/index.ts b/client/src/api/schema/index.ts index b9850fa31e6d..7adf8ad41bb7 100644 --- a/client/src/api/schema/index.ts +++ b/client/src/api/schema/index.ts @@ -1,9 +1,3 @@ -import createClient from "openapi-fetch"; - -import { getAppRoot } from "@/onload/loadConfig"; - import { type components, type paths as GalaxyApiPaths } from "./schema"; export { type components, type GalaxyApiPaths }; - -export const client = createClient({ baseUrl: getAppRoot(undefined, true) }); diff --git a/client/tests/jest/jest.setup.js b/client/tests/jest/jest.setup.js index de98a7498fed..48908b6fd4bb 100644 --- a/client/tests/jest/jest.setup.js +++ b/client/tests/jest/jest.setup.js @@ -3,7 +3,7 @@ import "@testing-library/jest-dom/jest-globals"; import Vue from "vue"; -import { server } from "@/api/__mocks__/node"; +import { server } from "@/api/client/__mocks__/node"; // Set Vue to suppress production / devtools / etc. warnings Vue.config.productionTip = false;