Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests/components tests #285

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/components/BrowseUploadsHeader/index.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright (C) 2024 Nillanshu Saini ([email protected])
SPDX-License-Identifier: GPL-2.0
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

import React from 'react';
import { render, screen } from '@testing-library/react';
import { BrowserRouter as Router } from 'react-router-dom';
import Header from './index';

const { isAuth } = require('shared/authHelper');

jest.mock('shared/authHelper', () => ({
isAuth: jest.fn(),
}));

describe('Header', () => {
it('renders without crashing', () => {
render(<Router><Header /></Router>);
});

it('displays links when user is authenticated', () => {
isAuth.mockReturnValue(true);

render(<Router><Header /></Router>);

expect(screen.getByText('Software Heritage')).toBeInTheDocument();
expect(screen.getByText('License Browser')).toBeInTheDocument();
expect(screen.getByText('File Browser')).toBeInTheDocument();
expect(screen.getByText('Copyright Browser')).toBeInTheDocument();
expect(screen.getByText('ECC')).toBeInTheDocument();
});

it('does not display links when user is not authenticated', () => {
isAuth.mockReturnValue(false);

render(<Router><Header /></Router>);

expect(screen.queryByText('Software Heritage')).not.toBeInTheDocument();
expect(screen.queryByText('License Browser')).not.toBeInTheDocument();
expect(screen.queryByText('File Browser')).not.toBeInTheDocument();
expect(screen.queryByText('Copyright Browser')).not.toBeInTheDocument();
expect(screen.queryByText('ECC')).not.toBeInTheDocument();
});
});
1 change: 1 addition & 0 deletions src/components/DarkThemeToggle/DarkThemeToggle.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const DarkThemeToggle = () => {
<label
htmlFor="checkbox"
className={`${state.theme} toggle-button--label`}
aria-label="checkbox"
>
{state.theme !== "light" ? (
<MoonStarsFill className="toggle-button--moon" size={20} />
Expand Down
72 changes: 72 additions & 0 deletions src/components/DarkThemeToggle/DarkThemeToggle.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright (C) 2024 Nillanshu Saini ([email protected])
SPDX-License-Identifier: GPL-2.0
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

import { render, fireEvent } from '@testing-library/react';
import { GlobalContext } from 'context';
import DarkThemeToggle from './DarkThemeToggle';

describe('DarkThemeToggle', () => {
it('should toggle dark theme on click', () => {
const setTheme = jest.fn();
const state = { theme: 'light' };

const { getByRole } = render(
<GlobalContext.Provider value={{ state, setTheme }}>
<DarkThemeToggle />
</GlobalContext.Provider>
);

const checkbox = getByRole('checkbox');
fireEvent.click(checkbox);

expect(setTheme).toHaveBeenCalledWith('dark');
});

it('should toggle light theme on click', () => {
const setTheme = jest.fn();
const state = { theme: 'dark' };

const { getByRole } = render(
<GlobalContext.Provider value={{ state, setTheme }}>
<DarkThemeToggle />
</GlobalContext.Provider>
);

const checkbox = getByRole('checkbox');
fireEvent.click(checkbox);

expect(setTheme).toHaveBeenCalledWith('light');
});

it('should display correct icon for each theme', () => {
const setTheme = jest.fn();

const { rerender, getByLabelText } = render(
<GlobalContext.Provider value={{ state: { theme: 'light' }, setTheme }}>
<DarkThemeToggle />
</GlobalContext.Provider>
);

expect(getByLabelText("checkbox")).toHaveClass('light toggle-button--label');

rerender(
<GlobalContext.Provider value={{ state: { theme: 'dark' }, setTheme }}>
<DarkThemeToggle />
</GlobalContext.Provider>
);

expect(getByLabelText("checkbox")).toHaveClass('dark toggle-button--label');
});
});
45 changes: 45 additions & 0 deletions src/components/Footer/index.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright (C) 2024 Nillanshu Saini ([email protected])
SPDX-License-Identifier: GPL-2.0
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

import { render, act } from '@testing-library/react';
import { getFossologyVersion } from 'services/info';
import { getSessionStorage, setSessionStorage } from 'shared/storageHelper';
import Footer from './index';

jest.mock('services/info');
jest.mock('shared/storageHelper');

describe('Footer', () => {
it('should display version information', async () => {
const versionInfo = {
version: '3.10.0',
branchName: 'master',
commitHash: 'a1b2c3',
commitDate: '2024-02-14',
buildDate: '2024-02-15',
};

getFossologyVersion.mockResolvedValue(versionInfo);
getSessionStorage.mockReturnValue(null);
setSessionStorage.mockImplementation(() => {});

let getByText;
await act(async () => {
({ getByText } = render(<Footer />));
});

expect(getByText(`Version: [${versionInfo.version}], Branch: [${versionInfo.branchName}], Commit: [#${versionInfo.commitHash}] ${versionInfo.commitDate} built @ ${versionInfo.buildDate}`)).toBeInTheDocument();
});
});
42 changes: 42 additions & 0 deletions src/components/Modals/DeleteConfirmation/index.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright (C) 2024 Nillanshu Saini ([email protected])
SPDX-License-Identifier: GPL-2.0
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

import React from 'react';
import { render, fireEvent, waitFor } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import DeleteConfirmation from './index';

describe('DeleteConfirmation', () => {
it('renders without crashing', () => {
const mockCallBack = jest.fn();
render(<DeleteConfirmation callBack={mockCallBack} loading={false} />);
});

it('calls the callback function when the delete button is clicked', async () => {
const mockCallBack = jest.fn();
const { getByText } = render(<DeleteConfirmation callBack={mockCallBack} loading={false} />);

fireEvent.click(getByText('YES'));

await waitFor(() => expect(mockCallBack).toHaveBeenCalled());
});

it('displays a spinner when loading', () => {
const mockCallBack = jest.fn();
const { getByRole } = render(<DeleteConfirmation callBack={mockCallBack} loading={true} />);

expect(getByRole('status', { hidden: true })).toBeInTheDocument();
});
});
42 changes: 42 additions & 0 deletions src/components/Table/tests/IndeterminateCheckbox.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright (C) 2024 Nillanshu Saini ([email protected])
SPDX-License-Identifier: GPL-2.0
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import IndeterminateCheckbox from '../IndeterminateCheckbox';

describe('IndeterminateCheckbox', () => {
it('renders without crashing', () => {
render(<IndeterminateCheckbox />);
});

it('sets the indeterminate property when indeterminate prop is true', () => {
const { getByRole } = render(<IndeterminateCheckbox indeterminate={true} />);
expect(getByRole('checkbox').indeterminate).toBe(true);
});

it('does not set the indeterminate property when indeterminate prop is false', () => {
const { getByRole } = render(<IndeterminateCheckbox indeterminate={false} />);
expect(getByRole('checkbox').indeterminate).toBe(false);
});

it('toggles the checked property when clicked', () => {
const { getByRole } = render(<IndeterminateCheckbox />);
const checkbox = getByRole('checkbox');
expect(checkbox.checked).toBe(false);
fireEvent.click(checkbox);
expect(checkbox.checked).toBe(true);
});
});
30 changes: 30 additions & 0 deletions src/components/Title/index.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright (C) 2024 Nillanshu Saini ([email protected])
SPDX-License-Identifier: GPL-2.0
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

import React from 'react';
import { render, waitFor } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import Title from './index';

describe('Title', () => {
it('renders without crashing', () => {
render(<Title title="Test" />);
});

it('sets the correct title', async () => {
render(<Title title="Test" />);
await waitFor(() => expect(document.title).toEqual('Test | FOSSology'));
});
});
59 changes: 59 additions & 0 deletions src/components/TreeContainer/index.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright (C) 2024 Nillanshu Saini ([email protected])
SPDX-License-Identifier: GPL-2.0
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

import React from "react";
import { render } from "@testing-library/react";
import TreeContainer from "./index";

describe("TreeContainer", () => {
const mockHandleClick = jest.fn();
const mockData = [
{
id: 1,
name: "Root",
children: [
{
id: 2,
name: "Child 1",
},
{
id: 3,
name: "Child 2",
},
],
},
];

it("renders without crashing", () => {
render(
<TreeContainer
data={mockData}
handleClick={mockHandleClick}
folderCount={3}
/>
);
});

it("renders the correct number of buttons", () => {
const { getAllByRole } = render(
<TreeContainer
data={mockData}
handleClick={mockHandleClick}
folderCount={3}
/>
);
expect(getAllByRole("grid")).toHaveLength(1);
});
});
Loading