Skip to content

Commit

Permalink
more components tested
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderVanOyen committed May 22, 2024
1 parent 9d13a84 commit 04ecec8
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 30 deletions.
2 changes: 1 addition & 1 deletion frontend/__test__/admin_components/UserList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("UserList", () => {
render(<UserList />);

// BackButton
const backButton = screen.getByRole("button", { name: /back to home page/i }); // Match translated button text partially
const backButton = screen.getByText("back_to home page"); // Match translated button text partially

expect(backButton).toBeInTheDocument();

Expand Down
2 changes: 1 addition & 1 deletion frontend/__test__/course_components/CancelButton.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {render, screen} from "@testing-library/react";
import React from "react";
import CancelButton from "@app/[locale]/components/course_components/CancelButton";

describe("UserList", () => {
describe("CancelButton", () => {
it("renders cancel button and click", async () => {
render(<CancelButton/>);
screen.getByText(/cancel/i).click();
Expand Down
33 changes: 33 additions & 0 deletions frontend/__test__/general/ItemList.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {render, screen, fireEvent} from "@testing-library/react";
import React from "react";
import ItemList from "@app/[locale]/components/general/ItemsList";

jest.mock("react-i18next", () => ({
useTranslation: () => ({
t: (key) => key
}),
}));

const mockCourse =
{
course_id: 1,
name: "Course 1",
description: "Description for Course 1",
year: 2023,
open_course: true,
banner: null,
};



describe("ItemList", () => {

it("renders correctly", async () => {
const mockSetItems = jest.fn();
render(<ItemList items={['test']}
setItems={mockSetItems}
empty_list_placeholder={"Test"}
input_placeholder={"Input"}
button_text={"Button"}/>);
});
});
25 changes: 25 additions & 0 deletions frontend/__test__/general/RequiredFilesList.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {render} from "@testing-library/react";
import React from "react";
import RequiredFilesList from "@app/[locale]/components/general/RequiredFilesList";

jest.mock("react-i18next", () => ({
useTranslation: () => ({
t: (key) => key
}),
}));


describe("RequiredFilesList", () => {

it("renders correctly", async () => {
const mockSetItems = jest.fn();
render(<RequiredFilesList items={['test']}
setItems={mockSetItems}
empty_list_placeholder={"Test"}
input_placeholder={"Input"}
button_text={"Button"}
items_status={["+", "-"]}
setItemsStatus={jest.fn()}
/>);
});
});
11 changes: 11 additions & 0 deletions frontend/__test__/project/edit/BackButton.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {fireEvent, render, screen} from "@testing-library/react";
import React from "react";
import BackButton from "@app/[locale]/components/project_components/BackButton";

describe("BackButton", () => {
it("render", async () => {
render(<BackButton text={"back"} destination={"/home"}/>);
const backButton = screen.getByRole("button", {name: /back/i});
fireEvent.click(backButton);
});
});
46 changes: 18 additions & 28 deletions frontend/__test__/project/edit/Requiredfiles.test.tsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,30 @@
import { render, screen, fireEvent } from "@testing-library/react";
import {render, screen, fireEvent} from "@testing-library/react";
import React from "react";
import RequiredFiles from "@app/[locale]/components/project_components/requiredFiles";
import { useTranslation } from "react-i18next"; // Import directly (optional)

// Mock translations with actual translation logic (consider using a mocking library)
jest.mock("react-i18next", () => ({
useTranslation: () => ({
t: (key) => {
const translations = getTranslations(); // Replace with your translation retrieval logic
return translations[key] || key;
},
}),
useTranslation: () => ({
t: (key) => key
}),
}));

describe("RequiredFiles", () => {
it("renders required files title and list with translations", async () => {
const translations = await getTranslations();
render(
<RequiredFiles
files={["First", "Second"]}
setFiles={jest.fn()}
file_status={["+", "-"]}
setFileStatus={jest.fn()}
translations={translations.en} // Pass specific translations (optional)
/>
);
it("renders required files title and list with translations", async () => {
render(
<RequiredFiles
files={["First", "Second"]}
setFiles={jest.fn()}
file_status={["+", "-"]}
setFileStatus={jest.fn()}
/>
);

const title = screen.getByText(/required_files/i); // Match translated title partially
const fileList = screen.getByRole("list");
const title = screen.getByText(/required_files/i);
const fileList = screen.getByRole("list");

expect(title).toBeInTheDocument();
expect(fileList).toBeInTheDocument();
expect(title).toBeInTheDocument();
expect(fileList).toBeInTheDocument();

// Verify list items using translated data (consider data-testid or custom assertions)
});

// Consider adding tests for RequiredFilesList behavior (adding/removing files, updating status)
// Consider testing tooltip behavior if applicable
});
});
10 changes: 10 additions & 0 deletions frontend/__test__/user_components/CancelButton.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {render, screen} from "@testing-library/react";
import React from "react";
import CancelButton from "@app/[locale]/components/user_components/CancelButton";

describe("CancelButton", () => {
it("renders cancel button and click", async () => {
render(<CancelButton/>);
screen.getByText(/cancel/i).click();
});
});
34 changes: 34 additions & 0 deletions frontend/__test__/user_components/DeleteButton.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {render, screen, fireEvent} from "@testing-library/react";
import React from "react";
import DeleteButton from "@app/[locale]/components/user_components/DeleteButton";

jest.mock("react-i18next", () => ({
useTranslation: () => ({
t: (key) => key
}),
}));


jest.mock("../../lib/api", () => ({
deleteUser: jest.fn(() => Promise.resolve()),
}));

describe("DeleteButton", () => {
it("render and delete", async () => {
render(<DeleteButton userId={1}/>);

const deleteButton = screen.getByRole("button", {name: /delete user/i});
expect(deleteButton).toBeInTheDocument();

fireEvent.click(deleteButton);

const dialogTitle = screen.getByText("Are you sure you want to delete this user?");
const cancelButton = screen.getByRole("button", {name: /cancel/i});
const deleteButtonInDialog = screen.getByRole("button", {name: /delete/i});

fireEvent.click(cancelButton);

fireEvent.click(deleteButton);
fireEvent.click(deleteButtonInDialog);
});
});

0 comments on commit 04ecec8

Please sign in to comment.