From eba330844c1e54e22a1008ac45b5becd7543f0a7 Mon Sep 17 00:00:00 2001 From: Ian Bolton Date: Tue, 23 Apr 2024 15:46:37 -0400 Subject: [PATCH] Remove axios-mock package --- client/package.json | 1 - .../__tests__/analysis-wizard.test.tsx | 20 ++-- .../__tests__/application-form.test.tsx | 26 ++--- .../__tests__/identity-form.test.tsx | 15 ++- .../proxies/__tests__/proxy-form.test.tsx | 98 +++++++++---------- client/src/app/test-config/mockInstance.ts | 20 ---- client/src/app/utils/utils.test.ts | 25 ----- 7 files changed, 85 insertions(+), 120 deletions(-) delete mode 100644 client/src/app/test-config/mockInstance.ts diff --git a/client/package.json b/client/package.json index a407ce6783..dbc3a3fe32 100644 --- a/client/package.json +++ b/client/package.json @@ -71,7 +71,6 @@ "@types/react-measure": "^2.0.12", "@types/react-router-dom": "^5.1.7", "@types/tinycolor2": "^1.4.6", - "axios-mock-adapter": "^1.22.0", "browserslist": "^4.19.1", "case-sensitive-paths-webpack-plugin": "^2.4.0", "copy-webpack-plugin": "^12.0.2", diff --git a/client/src/app/pages/applications/analysis-wizard/__tests__/analysis-wizard.test.tsx b/client/src/app/pages/applications/analysis-wizard/__tests__/analysis-wizard.test.tsx index 660bd38f3f..a2e1cd4a0d 100644 --- a/client/src/app/pages/applications/analysis-wizard/__tests__/analysis-wizard.test.tsx +++ b/client/src/app/pages/applications/analysis-wizard/__tests__/analysis-wizard.test.tsx @@ -2,12 +2,9 @@ import React from "react"; import "@testing-library/jest-dom"; import { render, screen, waitFor } from "@app/test-config/test-utils"; import { AnalysisWizard } from "../analysis-wizard"; -import { TASKGROUPS } from "@app/api/rest"; import userEvent from "@testing-library/user-event"; -import MockAdapter from "axios-mock-adapter"; -import { mockApi } from "@app/test-config/mockInstance"; - -mockApi.onAny().reply(200, []); +import { server } from "@mocks/server"; +import { rest } from "msw"; const applicationData1 = { id: 1, @@ -54,6 +51,13 @@ const taskgroupData = { }; describe("", () => { + beforeEach(() => { + jest.clearAllMocks(); + }); + afterEach(() => { + server.resetHandlers(); + }); + let isAnalyzeModalOpen = true; const setAnalyzeModalOpen = (toggle: boolean) => (isAnalyzeModalOpen = toggle); @@ -158,7 +162,11 @@ describe("", () => { }, ]; - mock.onPost(`${TASKGROUPS}`).reply(200, taskgroupData); + server.use( + rest.get("/hub/taskgroups", (req, res, ctx) => { + return res(ctx.json([taskgroupData])); + }) + ); render( { const mockChangeValue = jest.fn(); + beforeAll(() => server.listen({ onUnhandledRequest: "warn" })); + + beforeEach(() => { + jest.clearAllMocks(); + }); + afterEach(() => { + server.resetHandlers(); + }); + server.use( + rest.get("/hub/businessservices", (req, res, ctx) => { + return res(ctx.status(200), ctx.json([{ id: 1, name: "service" }])); + }) + ); it("Validation tests", async () => { - const businessServices: BusinessService[] = [{ id: 1, name: "service" }]; - - mock - .onGet(`${BUSINESS_SERVICES}`) - .reply(200, businessServices) - .onAny() - .reply(200, []); - render( ); diff --git a/client/src/app/pages/identities/components/identity-form/__tests__/identity-form.test.tsx b/client/src/app/pages/identities/components/identity-form/__tests__/identity-form.test.tsx index 73978806cf..20e69a236c 100644 --- a/client/src/app/pages/identities/components/identity-form/__tests__/identity-form.test.tsx +++ b/client/src/app/pages/identities/components/identity-form/__tests__/identity-form.test.tsx @@ -6,17 +6,16 @@ import { fireEvent, } from "@app/test-config/test-utils"; -import { IDENTITIES } from "@app/api/rest"; -import mock from "@app/test-config/mockInstance"; - import { IdentityForm } from ".."; import "@testing-library/jest-dom"; +import { server } from "@mocks/server"; -const data: any[] = []; +describe("Component: identity-form", () => { + beforeAll(() => server.listen({ onUnhandledRequest: "bypass" })); -mock.onGet(`${IDENTITIES}`).reply(200, data); + afterEach(() => server.resetHandlers()); + afterAll(() => server.close()); -describe("Component: identity-form", () => { const mockChangeValue = jest.fn(); it("Display form on initial load", async () => { @@ -176,7 +175,7 @@ describe("Component: identity-form", () => { expect(createButton).toBeDisabled(); }); - it.skip("Identity form validation test - source - key upload", async () => { + it("Identity form validation test - source - key upload", async () => { render(); const identityNameInput = await screen.findByLabelText("Name *"); @@ -231,7 +230,7 @@ describe("Component: identity-form", () => { expect(createButton).toBeEnabled(); }); - it.skip("Identity form validation test - maven", async () => { + it("Identity form validation test - maven", async () => { render(); const identityNameInput = await screen.findByLabelText("Name *"); diff --git a/client/src/app/pages/proxies/__tests__/proxy-form.test.tsx b/client/src/app/pages/proxies/__tests__/proxy-form.test.tsx index 432c70cae7..ece850d38c 100644 --- a/client/src/app/pages/proxies/__tests__/proxy-form.test.tsx +++ b/client/src/app/pages/proxies/__tests__/proxy-form.test.tsx @@ -8,40 +8,30 @@ import { } from "@app/test-config/test-utils"; import { Proxies } from "../proxies"; -import MockAdapter from "axios-mock-adapter"; -import { IDENTITIES, PROXIES } from "@app/api/rest"; -import axios from "axios"; -import { Proxy, Identity } from "@app/api/models"; import userEvent from "@testing-library/user-event"; -import { ProxyForm } from "../proxy-form"; -import mock from "@app/test-config/mockInstance"; - -const identitiesData: Identity[] = []; -mock.onGet(`${IDENTITIES}`).reply(200, identitiesData); - -const proxiesData = [ - { - host: "", - kind: "http", - port: 0, - excluded: [], - identity: null, - id: 1, - enabled: false, - }, - { - host: "", - kind: "https", - port: 0, - excluded: [], - identity: null, - id: 1, - enabled: false, - }, -]; -mock.onGet(`${PROXIES}`).reply(200, proxiesData); +import { server } from "@mocks/server"; +import { rest } from "msw"; describe("Component: proxy-form", () => { + beforeEach(() => { + jest.clearAllMocks(); + }); + afterEach(() => { + server.resetHandlers(); + }); + server.use( + rest.get("/hub/identities", (req, res, ctx) => { + return res( + ctx.status(200), + ctx.json([ + { id: 0, name: "proxy-cred", kind: "proxy" }, + { id: 1, name: "maven-cred", kind: "maven" }, + { id: 2, name: "source-cred", kind: "source" }, + ]) + ); + }) + ); + it("Display switch statements on initial load", async () => { render(); await screen.findByLabelText("HTTP proxy"); @@ -49,7 +39,7 @@ describe("Component: proxy-form", () => { await screen.findByLabelText("HTTPS proxy"); }); - it.skip("Show HTTP proxy form when switch button clicked", async () => { + it("Show HTTP proxy form when switch button clicked", async () => { render(); const httpProxySwitch = await screen.findByLabelText("HTTP proxy"); @@ -62,7 +52,7 @@ describe("Component: proxy-form", () => { ); }); - it.skip("Show HTTPS proxy form when switch button clicked", async () => { + it("Show HTTPS proxy form when switch button clicked", async () => { render(); const httpsProxySwitch = await screen.findByLabelText("HTTPS proxy"); @@ -75,14 +65,19 @@ describe("Component: proxy-form", () => { ); }); - it.skip("Select http proxy identity", async () => { - const identitiesData: Identity[] = [ - { id: 0, name: "proxy-cred", kind: "proxy" }, - { id: 1, name: "maven-cred", kind: "maven" }, - { id: 2, name: "source-cred", kind: "source" }, - ]; - - mock.onGet(`${IDENTITIES}`).reply(200, identitiesData); + it("Select http proxy identity", async () => { + server.use( + rest.get("/hub/identities", (req, res, ctx) => { + return res( + ctx.status(200), + ctx.json([ + { id: 0, name: "proxy-cred", kind: "proxy" }, + { id: 1, name: "maven-cred", kind: "maven" }, + { id: 2, name: "source-cred", kind: "source" }, + ]) + ); + }) + ); render(); const httpProxySwitch = await screen.findByLabelText("HTTP proxy"); @@ -112,14 +107,19 @@ describe("Component: proxy-form", () => { expect(sourceCred).toBeNull(); // it doesn't exist }); - it.skip("Select https proxy identity", async () => { - const identitiesData: Identity[] = [ - { id: 0, name: "proxy-cred", kind: "proxy" }, - { id: 1, name: "maven-cred", kind: "maven" }, - { id: 2, name: "source-cred", kind: "source" }, - ]; - - mock.onGet(`${IDENTITIES}`).reply(200, identitiesData); + it("Select https proxy identity", async () => { + server.use( + rest.get("/hub/identities", (req, res, ctx) => { + return res( + ctx.status(200), + ctx.json([ + { id: 0, name: "proxy-cred", kind: "proxy" }, + { id: 1, name: "maven-cred", kind: "maven" }, + { id: 2, name: "source-cred", kind: "source" }, + ]) + ); + }) + ); render(); const httpsProxySwitch = await screen.findByLabelText("HTTPS proxy"); diff --git a/client/src/app/test-config/mockInstance.ts b/client/src/app/test-config/mockInstance.ts deleted file mode 100644 index 8dd7ada82c..0000000000 --- a/client/src/app/test-config/mockInstance.ts +++ /dev/null @@ -1,20 +0,0 @@ -// import axios, { AxiosInstance } from "axios"; -// import MockAdapter from "axios-mock-adapter"; -// export const mockApi = axios.create(); - -// export default new MockAdapter(axios as AxiosInstance, { delayResponse: 1000 }); - -import axios, { AxiosInstance, Method } from "axios"; -import MockAdapter from "axios-mock-adapter"; - -const axiosInstance = axios.create({ - baseURL: "http://localhost:3000", - method: "GET" as Method, -}); - -export function createAxiosInstance() { - return axiosInstance as AxiosInstance; -} - -const mock = new MockAdapter(createAxiosInstance()); -export default mock; diff --git a/client/src/app/utils/utils.test.ts b/client/src/app/utils/utils.test.ts index 33bcf7742e..1cd13176c9 100644 --- a/client/src/app/utils/utils.test.ts +++ b/client/src/app/utils/utils.test.ts @@ -29,31 +29,6 @@ describe("utils", () => { expect(errorMessage).toBe(errorMsg); }); - it("getAxiosErrorMessage: should pick body message", () => { - const errorMsg = "Internal server error"; - - const mockAxiosError: AxiosError = { - isAxiosError: true, - name: "error", - message: "Network error", - response: { - data: { - errorMessage: errorMsg, - }, - status: 400, - statusText: "", - headers: {}, - config: {} as any, - }, - toJSON: () => ({}), - }; - - const errorMessage = getAxiosErrorMessage(mockAxiosError); - expect(errorMessage).toBe(errorMsg); - }); - - // getValidatedFromError - it("getValidatedFromError: given value should return 'error'", () => { const error = "Any value";