From 0c55e9983c990ef795b2d68b4d2617470b32df25 Mon Sep 17 00:00:00 2001 From: Gang Li Date: Mon, 13 Nov 2023 21:18:00 +0800 Subject: [PATCH] Add unit test for CommonPageWidget * test for LocalURLSection * test for CapabilitiesSection * also add package type apis for mocking server --- .../webui/src/app/components/CompUtils.js | 36 --------------- .../src/app/components/ComponentConstants.js | 5 +- .../components/content/CommonPageWidget.jsx | 1 + .../content/CommonPageWidget.test.js | 46 +++++++++++++++++++ src/main/webui/src/server/app.js | 4 ++ 5 files changed, 54 insertions(+), 38 deletions(-) create mode 100644 src/main/webui/src/app/components/content/CommonPageWidget.test.js diff --git a/src/main/webui/src/app/components/CompUtils.js b/src/main/webui/src/app/components/CompUtils.js index 2086b43..c07f654 100644 --- a/src/main/webui/src/app/components/CompUtils.js +++ b/src/main/webui/src/app/components/CompUtils.js @@ -14,8 +14,6 @@ * limitations under the License. */ -import {jsonGet} from '../RestClient.js'; - export const Utils = { remoteOptions: store => { let options = []; @@ -139,39 +137,5 @@ export const Utils = { let allParams = [message]; params.forEach(p => allParams.push(p)); Reflect.apply(console.log, undefined, allParams); - }, - // TODO: not used? - getDisTimeouts: (state, setState, storeType) => { - jsonGet({ - url: '/api/admin/schedule/store/all/disable-timeout', - done: response => { - let disabledMap = Utils.setDisableMap(response, state.listing); - setState({ - disabledMap - }); - }, - fail: () => { - Utils.logMessage(`disable timeout get failed in ${storeType} listing.`); - } - }); - }, - // TODO: Not used? - getStores: (state, setState, storeType) => { - let url = `/api/admin/stores/_all/${storeType}`; - jsonGet({ - url, - done: response => { - setState({ - listing: response.items, - rawListing: response.items - }); - Utils.getDisTimeouts(state, setState); - }, - fail: errorText => { - setState({ - message: JSON.parse(errorText).error - }); - } - }); } }; diff --git a/src/main/webui/src/app/components/ComponentConstants.js b/src/main/webui/src/app/components/ComponentConstants.js index 6039e76..cfbf6dd 100644 --- a/src/main/webui/src/app/components/ComponentConstants.js +++ b/src/main/webui/src/app/components/ComponentConstants.js @@ -13,19 +13,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -// mock data: options +// TODO: mock data: options. Need to use CompUtils. const remoteOptionLegend = [ {icon: "S", title: "Snapshots allowed"}, {icon: "R", title: "Releases allowed"} ]; +// TODO: mock data: options. Need to use CompUtils. const hostedOptionLegend = [ {icon: 'S', title: 'Snapshots allowed'}, {icon: 'R', title: 'Releases allowed'}, {icon: 'D', title: 'Deployment allowed'} ]; -// TODO this should be fetched from backend +// TODO: this should be fetched from backend const PACKAGE_TYPES = ["maven", "generic","npm"]; diff --git a/src/main/webui/src/app/components/content/CommonPageWidget.jsx b/src/main/webui/src/app/components/content/CommonPageWidget.jsx index 2d3fe5d..e2dcd3a 100644 --- a/src/main/webui/src/app/components/content/CommonPageWidget.jsx +++ b/src/main/webui/src/app/components/content/CommonPageWidget.jsx @@ -28,6 +28,7 @@ LocalURLSection.propTypes = { storeKey: PropTypes.string }; +// For options, see CompUtils.remoteOptions|hostedOptions const CapabilitiesSection = ({options}) =>
{' '} { diff --git a/src/main/webui/src/app/components/content/CommonPageWidget.test.js b/src/main/webui/src/app/components/content/CommonPageWidget.test.js new file mode 100644 index 0000000..0b33d2b --- /dev/null +++ b/src/main/webui/src/app/components/content/CommonPageWidget.test.js @@ -0,0 +1,46 @@ +/** + * 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. + */ + +import React from "react"; +import {render, screen, cleanup} from '@testing-library/react'; +import '@testing-library/jest-dom'; +import {LocalURLSection, + CapabilitiesSection} from "./CommonPageWidget.jsx"; +import {hostedOptionLegend} from '../ComponentConstants.js'; + +// const mockData = {}; + +afterEach(() => { + cleanup(); +}); + +describe('CommonPageWidget tests', () => { + it("LocalURLSection", () => { + render(); + const keyLink = screen.getByRole("link"); + expect(keyLink).toBeInTheDocument(); + const urlPat = /https?:\/\/.*\/maven\/remote\/central$/ui; + expect(keyLink.href).toMatch(urlPat); + expect(screen.getByText(urlPat)).toBeInTheDocument(); + }); + + it("CapabilitiesSection",()=>{ + render(); + expect(screen.getByText(/\s*S\s*/u)).toBeInTheDocument(); + expect(screen.getByText(/\s*R\s*/u)).toBeInTheDocument(); + expect(screen.getByText(/\s*D\s*/u)).toBeInTheDocument(); + }); +}); diff --git a/src/main/webui/src/server/app.js b/src/main/webui/src/server/app.js index 4807a54..2810460 100644 --- a/src/main/webui/src/server/app.js +++ b/src/main/webui/src/server/app.js @@ -22,6 +22,10 @@ app.get([Config.APP_ROOT, `${Config.APP_ROOT}/*`, '/'], (req, res) => { res.sendFile(indexHtml); }); +app.get('/api/stats/package-type/keys', (req, res)=>{ + res.status(200).json(["generic-http", "maven", "npm"]); +}); + app.get('/api/admin/stores/_all/remote', (req, res) => { const remoteList = require('./mock/list/FakeRemoteList.json'); res.status(200).json(remoteList);