Skip to content

Commit

Permalink
test: enhance utils tests and add MatrixActions component tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hdinia committed Oct 17, 2024
1 parent 8a0d4c9 commit 5a84cfc
Show file tree
Hide file tree
Showing 3 changed files with 420 additions and 36 deletions.
164 changes: 164 additions & 0 deletions webapp/src/components/common/MatrixGrid/MatrixActions.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/**
* Copyright (c) 2024, RTE (https://www.rte-france.com)
*
* See AUTHORS.txt
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* SPDX-License-Identifier: MPL-2.0
*
* This file is part of the Antares project.
*/

import React from "react";
import { render, screen, fireEvent } from "@testing-library/react";
import { describe, it, expect, vi, beforeEach } from "vitest";
import MatrixActions from "./MatrixActions";

vi.mock("../buttons/SplitButton", () => ({
default: ({
children,
onClick,
disabled,
}: {
children: React.ReactNode;
onClick: () => void;
disabled?: boolean;
}) => (
<button onClick={onClick} disabled={disabled}>
{children}
</button>
),
}));

vi.mock("../buttons/DownloadMatrixButton", () => ({
default: ({ disabled, label }: { disabled: boolean; label?: string }) => (
<button disabled={disabled}>{label || "global.export"}</button>
),
}));

describe("MatrixActions", () => {
const defaultProps = {
onImport: vi.fn(),
onSave: vi.fn(),
studyId: "study1",
path: "/path/to/matrix",
disabled: false,
pendingUpdatesCount: 0,
isSubmitting: false,
undo: vi.fn(),
redo: vi.fn(),
canUndo: true,
canRedo: true,
};

beforeEach(() => {
vi.clearAllMocks();
});

it("renders all buttons and controls", () => {
render(<MatrixActions {...defaultProps} />);

expect(screen.getByLabelText("global.undo")).toBeDefined();
expect(screen.getByLabelText("global.redo")).toBeDefined();
expect(screen.getByText("(0)")).toBeDefined();
expect(screen.getByText("global.import")).toBeDefined();
expect(screen.getByText("global.export")).toBeDefined();
});

it("disables undo button when canUndo is false", () => {
render(<MatrixActions {...defaultProps} canUndo={false} />);
expect(
screen.getByLabelText("global.undo").querySelector("button"),
).toBeDisabled();
});

it("disables redo button when canRedo is false", () => {
render(<MatrixActions {...defaultProps} canRedo={false} />);
expect(
screen.getByLabelText("global.redo").querySelector("button"),
).toBeDisabled();
});

it("calls undo function when undo button is clicked", () => {
render(<MatrixActions {...defaultProps} />);
const undoButton = screen
.getByLabelText("global.undo")
.querySelector("button");
if (undoButton) {
fireEvent.click(undoButton);
expect(defaultProps.undo).toHaveBeenCalled();
} else {
throw new Error("Undo button not found");
}
});

it("calls redo function when redo button is clicked", () => {
render(<MatrixActions {...defaultProps} />);
const redoButton = screen
.getByLabelText("global.redo")
.querySelector("button");
if (redoButton) {
fireEvent.click(redoButton);
expect(defaultProps.redo).toHaveBeenCalled();
} else {
throw new Error("Redo button not found");
}
});

it("disables save button when pendingUpdatesCount is 0", () => {
render(<MatrixActions {...defaultProps} />);
expect(screen.getByText("(0)").closest("button")).toBeDisabled();
});

it("enables save button when pendingUpdatesCount is greater than 0", () => {
render(<MatrixActions {...defaultProps} pendingUpdatesCount={1} />);
expect(screen.getByText("(1)").closest("button")).not.toBeDisabled();
});

it("calls onSave function when save button is clicked", () => {
render(<MatrixActions {...defaultProps} pendingUpdatesCount={1} />);
const saveButton = screen.getByText("(1)").closest("button");
if (saveButton) {
fireEvent.click(saveButton);
expect(defaultProps.onSave).toHaveBeenCalled();
} else {
throw new Error("Save button not found");
}
});

it("shows loading state on save button when isSubmitting is true", () => {
render(
<MatrixActions
{...defaultProps}
isSubmitting={true}
pendingUpdatesCount={1}
/>,
);
expect(screen.getByText("(1)").closest("button")).toBeDisabled();
});

it("calls onImport function when import button is clicked", () => {
render(<MatrixActions {...defaultProps} />);
fireEvent.click(screen.getByText("global.import"));
expect(defaultProps.onImport).toHaveBeenCalled();
});

it("disables import button when isSubmitting is true", () => {
render(<MatrixActions {...defaultProps} isSubmitting={true} />);
expect(screen.getByText("global.import")).toBeDisabled();
});

it("passes correct props to DownloadMatrixButton", () => {
const { rerender } = render(<MatrixActions {...defaultProps} />);
expect(screen.getByText("global.export")).not.toBeDisabled();

rerender(<MatrixActions {...defaultProps} disabled={true} />);
expect(screen.getByText("global.export")).toBeDisabled();

rerender(<MatrixActions {...defaultProps} isSubmitting={true} />);
expect(screen.getByText("global.export")).toBeDisabled();
});
});
34 changes: 0 additions & 34 deletions webapp/src/components/common/MatrixGrid/useGridCellContent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,6 @@ import { useGridCellContent } from "./useGridCellContent";
import { Column, MatrixAggregates, type EnhancedGridColumn } from "./types";
import { useColumnMapping } from "./useColumnMapping";

// Mocking i18next
vi.mock("i18next", () => {
const i18n = {
language: "fr",
use: vi.fn().mockReturnThis(),
init: vi.fn(),
t: vi.fn((key) => key),
changeLanguage: vi.fn((lang) => {
i18n.language = lang;
return Promise.resolve();
}),
on: vi.fn(),
};
return { default: i18n };
});

// Mocking react-i18next
vi.mock("react-i18next", async (importOriginal) => {
const actual = await importOriginal();
return Object.assign({}, actual, {
useTranslation: () => ({
t: vi.fn((key) => key),
i18n: {
changeLanguage: vi.fn(),
language: "fr",
},
}),
initReactI18next: {
type: "3rdParty",
init: vi.fn(),
},
});
});

function renderGridCellContent(
data: number[][],
columns: EnhancedGridColumn[],
Expand Down
Loading

0 comments on commit 5a84cfc

Please sign in to comment.