From 7d33861495755ee1d56cf3a28795add965286b58 Mon Sep 17 00:00:00 2001 From: Gang Li Date: Thu, 23 Nov 2023 16:28:52 +0800 Subject: [PATCH] Uset jest-fetch-mock instead global jest mock for fetch API mock --- src/main/webui/jest.config.js | 22 ------ src/main/webui/package-lock.json | 75 +++++++++++++++++++ src/main/webui/package.json | 8 ++ src/main/webui/setupTests.js | 3 + .../content/common/PackageTypeSelect.test.jsx | 18 ++--- .../src/app/components/nav/NavFooter.test.jsx | 11 ++- 6 files changed, 99 insertions(+), 38 deletions(-) delete mode 100644 src/main/webui/jest.config.js create mode 100644 src/main/webui/setupTests.js diff --git a/src/main/webui/jest.config.js b/src/main/webui/jest.config.js deleted file mode 100644 index 814309a..0000000 --- a/src/main/webui/jest.config.js +++ /dev/null @@ -1,22 +0,0 @@ -/** - * Copyright (C) 2023 Red Hat, Inc. (https://github.com/Commonjava/indy-ui-service) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/** @type {import('jest').Config} */ -const config = { - testEnvironment: 'jsdom', - verbose: true -}; - -module.exports = config; diff --git a/src/main/webui/package-lock.json b/src/main/webui/package-lock.json index ca9ee31..2bb6fb3 100644 --- a/src/main/webui/package-lock.json +++ b/src/main/webui/package-lock.json @@ -40,6 +40,7 @@ "express": "^4.18.2", "jest": "^29.7.0", "jest-environment-jsdom": "^29.7.0", + "jest-fetch-mock": "^3.0.3", "nodemon": "^3.0.1", "style-loader": "^3.3.3", "url-loader": "^4.1.1", @@ -5525,6 +5526,16 @@ "object-assign": "^4.1.1" } }, + "node_modules/cross-fetch": { + "version": "3.1.8", + "resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.8.tgz", + "integrity": "sha512-cvA+JwZoU0Xq+h6WkMvAUqPEYy92Obet6UdKLfW60qn99ftItKjB5T+BkyWOFWe2pUyfQ+IJHmpOTznqk1M6Kg==", + "dev": true, + "license": "MIT", + "dependencies": { + "node-fetch": "^2.6.12" + } + }, "node_modules/cross-spawn": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", @@ -9323,6 +9334,17 @@ "node": "^14.15.0 || ^16.10.0 || >=18.0.0" } }, + "node_modules/jest-fetch-mock": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/jest-fetch-mock/-/jest-fetch-mock-3.0.3.tgz", + "integrity": "sha512-Ux1nWprtLrdrH4XwE7O7InRY6psIi3GOsqNESJgMJ+M5cv4A8Lh7SN9d2V2kKRZ8ebAfcd1LNyZguAOb6JiDqw==", + "dev": true, + "license": "MIT", + "dependencies": { + "cross-fetch": "^3.0.4", + "promise-polyfill": "^8.1.3" + } + }, "node_modules/jest-get-type": { "version": "29.6.3", "resolved": "https://registry.npmjs.org/jest-get-type/-/jest-get-type-29.6.3.tgz", @@ -10957,6 +10979,52 @@ "semver": "^5.7.0" } }, + "node_modules/node-fetch": { + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", + "integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==", + "dev": true, + "license": "MIT", + "dependencies": { + "whatwg-url": "^5.0.0" + }, + "engines": { + "node": "4.x || >=6.0.0" + }, + "peerDependencies": { + "encoding": "^0.1.0" + }, + "peerDependenciesMeta": { + "encoding": { + "optional": true + } + } + }, + "node_modules/node-fetch/node_modules/tr46": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", + "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==", + "dev": true, + "license": "MIT" + }, + "node_modules/node-fetch/node_modules/webidl-conversions": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", + "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==", + "dev": true, + "license": "BSD-2-Clause" + }, + "node_modules/node-fetch/node_modules/whatwg-url": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", + "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", + "dev": true, + "license": "MIT", + "dependencies": { + "tr46": "~0.0.3", + "webidl-conversions": "^3.0.0" + } + }, "node_modules/node-forge": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/node-forge/-/node-forge-1.3.1.tgz", @@ -11712,6 +11780,13 @@ "integrity": "sha512-MtEC1TqN0EU5nephaJ4rAtThHtC86dNN9qCuEhtshvpVBkAW5ZO7BASN9REnF9eoXGcRub+pFuKEpOHE+HbEMw==", "dev": true }, + "node_modules/promise-polyfill": { + "version": "8.3.0", + "resolved": "https://registry.npmjs.org/promise-polyfill/-/promise-polyfill-8.3.0.tgz", + "integrity": "sha512-H5oELycFml5yto/atYqmjyigJoAo3+OXwolYiH7OfQuYlAqhxNvTfiNMbV9hsC6Yp83yE5r2KTVmtrG6R9i6Pg==", + "dev": true, + "license": "MIT" + }, "node_modules/prompts": { "version": "2.4.2", "resolved": "https://registry.npmjs.org/prompts/-/prompts-2.4.2.tgz", diff --git a/src/main/webui/package.json b/src/main/webui/package.json index 5ff791c..31bd7d2 100644 --- a/src/main/webui/package.json +++ b/src/main/webui/package.json @@ -8,6 +8,13 @@ "imports": { "#utils/*": "./src/app/utils/*" }, + "jest": { + "testEnvironment": "jsdom", + "verbose": true, + "automock": false, + "resetMocks": false, + "setupFiles": ["/setupTests.js"] + }, "dependencies": { "bootstrap": "^5.3.2", "prop-types": "^15.8.1", @@ -40,6 +47,7 @@ "express": "^4.18.2", "jest": "^29.7.0", "jest-environment-jsdom": "^29.7.0", + "jest-fetch-mock": "^3.0.3", "nodemon": "^3.0.1", "style-loader": "^3.3.3", "url-loader": "^4.1.1", diff --git a/src/main/webui/setupTests.js b/src/main/webui/setupTests.js new file mode 100644 index 0000000..ca30ae0 --- /dev/null +++ b/src/main/webui/setupTests.js @@ -0,0 +1,3 @@ +import fetchMock, {FetchMock as fetch} from "jest-fetch-mock"; + +fetchMock.enableMocks(); diff --git a/src/main/webui/src/app/components/content/common/PackageTypeSelect.test.jsx b/src/main/webui/src/app/components/content/common/PackageTypeSelect.test.jsx index 4267d30..aa10140 100644 --- a/src/main/webui/src/app/components/content/common/PackageTypeSelect.test.jsx +++ b/src/main/webui/src/app/components/content/common/PackageTypeSelect.test.jsx @@ -16,20 +16,22 @@ import React from "react"; import {render, screen, cleanup, waitFor} from '@testing-library/react'; +import fetchMock from "jest-fetch-mock"; import userEvent from "@testing-library/user-event"; import '@testing-library/jest-dom'; import {PackageTypeSelect} from "./PackageTypeSelect.jsx"; +beforeEach(() => { + fetchMock.resetMocks(); + fetchMock.mockResponseOnce(JSON.stringify(["maven", "npm", "generic-http"])); +}); + afterEach(() => { cleanup(); }); describe('PackageTypeSelect tests', () => { it("Verify PackageTypeSelect by default", async ()=>{ - global.fetch = jest.fn(() => Promise.resolve({ - ok: true, - json: () => Promise.resolve(["maven", "npm", "generic-http"]), - })); render(); await waitFor(()=>{ expect(screen.getByRole("option", {name: "maven"})).toBeInTheDocument(); @@ -42,10 +44,6 @@ describe('PackageTypeSelect tests', () => { }); it("Verify PackageTypeSelect for npm selected", async ()=>{ - global.fetch = jest.fn(() => Promise.resolve({ - ok: true, - json: () => Promise.resolve(["maven", "npm", "generic-http"]), - })); render(); await waitFor(()=>{ expect(screen.getByRole("option", {name: "maven"})).toBeInTheDocument(); @@ -58,10 +56,6 @@ describe('PackageTypeSelect tests', () => { }); it("Verify PackageTypeSelect for value change", async ()=>{ - global.fetch = jest.fn(() => Promise.resolve({ - ok: true, - json: () => Promise.resolve(["maven", "npm", "generic-http"]), - })); const {selectOptions} = userEvent.setup(); let value = ""; const vauleChangeHandler = e => { diff --git a/src/main/webui/src/app/components/nav/NavFooter.test.jsx b/src/main/webui/src/app/components/nav/NavFooter.test.jsx index a3c7ce9..865d7ca 100644 --- a/src/main/webui/src/app/components/nav/NavFooter.test.jsx +++ b/src/main/webui/src/app/components/nav/NavFooter.test.jsx @@ -17,8 +17,13 @@ import React from "react"; import {render, screen, cleanup, waitFor} from '@testing-library/react'; import '@testing-library/jest-dom'; +import fetchMock from "jest-fetch-mock"; import NavFooter from "./NavFooter.jsx"; +beforeEach(() => { + fetchMock.resetMocks(); +}); + afterEach(() => { cleanup(); }); @@ -31,10 +36,8 @@ describe('Footer tests', () => { builder: "test-builder", timestamp: "2023-10-24 05:54 +0000" }; - global.fetch = jest.fn(() => Promise.resolve({ - ok: true, - json: () => Promise.resolve(mockStats), - })); + fetchMock.mockResponseOnce(JSON.stringify(mockStats)); + render(); expect(screen.getByRole("link", {name: "Docs"})).toHaveAttribute("href", "http://commonjava.github.io/indy/"); expect(screen.getByRole("link", {name: "Issues"})).toHaveAttribute("href", "http://github.com/commonjava/indy/issues");