Skip to content

Commit

Permalink
Merge pull request #96 from ligangty/2.0-refactor
Browse files Browse the repository at this point in the history
Add Unit tests for Header and Footer
  • Loading branch information
ligangty authored Nov 23, 2023
2 parents a4b83a3 + 3e4ceb4 commit 6dfeb1e
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 7 deletions.
48 changes: 48 additions & 0 deletions src/main/webui/src/app/components/nav/NavFooter.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* 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, waitFor} from '@testing-library/react';
import '@testing-library/jest-dom';
import NavFooter from "./NavFooter.jsx";

afterEach(() => {
cleanup();
});

describe('Footer tests', () => {
it("Verify Footer for elements existing", async ()=>{
const mockStats = {
version: "3.3.2",
commitId: "f472176",
builder: "test-builder",
timestamp: "2023-10-24 05:54 +0000"
};
global.fetch = jest.fn(() => Promise.resolve({
ok: true,
json: () => Promise.resolve(mockStats),
}));
render(<NavFooter />);
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");
await waitFor(()=>{
expect(screen.getByText(`Version:${mockStats.version}`)).toBeInTheDocument();
expect(screen.getByRole("link", {name: mockStats.commitId})).toHaveAttribute("href", `http://github.com/commonjava/indy/commit/${mockStats.commitId}`);
expect(screen.getByRole("link", {name: mockStats.builder})).toHaveAttribute("href", `http://github.com/${mockStats.builder}`);
});

});
});
8 changes: 1 addition & 7 deletions src/main/webui/src/app/components/nav/NavHeader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,7 @@ const isUserloggedIn = true;
const username = "mock";

// eslint-disable-next-line max-lines-per-function
export default function IndyNavHeader(){
// TODO: addons will be render based on the backend addons response, this is a mock;
// const addons=[
// <Link key="autoproxy-calc" className="dropdown-item" to="/autoprox/calc">AutoProx Calculator</Link>,
// <Link key="autoproxy-rules" className="dropdown-item" to="/autoprox/rules">AutoProx Rules</Link>,
// <Link key="store-changelog" className="dropdown-item" to="/revisions/changelog/stores">Store Changelogs</Link>
// ];
export default function NavHeader(){
return (
<nav className="navbar fixed-top navbar-expand-lg navbar-light bg-light" role="navigation">
<Link className="navbar-brand" to="">Indy</Link>
Expand Down
55 changes: 55 additions & 0 deletions src/main/webui/src/app/components/nav/NavHeader.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* 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 userEvent from "@testing-library/user-event";
import {MemoryRouter} from 'react-router-dom';
import '@testing-library/jest-dom';
import NavHeader from "./NavHeader.jsx";

afterEach(() => {
cleanup();
});

describe('Header tests', () => {
it("Verify Header for elements existing", async ()=>{
const user = userEvent.setup();
render(<MemoryRouter><NavHeader /></MemoryRouter>);
expect(screen.getByRole("link", {Name: "Indy"})).toBeInTheDocument();
const remoteReposButton = screen.getByRole("button", {name: "Remote Repositories"});
expect(remoteReposButton).toBeInTheDocument();
const hostedReposButton = screen.getByRole("button", {name: "Hosted Repositories"});
expect(hostedReposButton).toBeInTheDocument();
const groupsButton = screen.getByRole("button", {name: "Groups"});
expect(groupsButton).toBeInTheDocument();
expect(screen.getByRole("button", {name: "REST API"})).toBeInTheDocument();
const addonsButton = screen.getByRole("button", {name: "more"});
expect(addonsButton).toBeInTheDocument();
// TODO: test the user login elements later when implemented

await user.click(remoteReposButton);
await user.click(hostedReposButton);
await user.click(groupsButton);
expect(screen.getAllByRole("link", {name: "maven"})).toHaveLength(3);
expect(screen.getAllByRole("link", {name: "generic-http"})).toHaveLength(3);
expect(screen.getAllByRole("link", {name: "npm"})).toHaveLength(3);

await user.click(addonsButton);
expect(screen.getByRole("link", {name: "Not-Found Cache"})).toBeInTheDocument();
expect(screen.getByRole("link", {name: "Delete Cache"})).toBeInTheDocument();
});
});

0 comments on commit 6dfeb1e

Please sign in to comment.