Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
64 changes: 64 additions & 0 deletions __tests__/components/cards/TranslationBox.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import "@testing-library/jest-dom";
import { fireEvent, screen, waitFor } from "@testing-library/react";
import TranslationBox from "@/components/cards/TranslationBox";
import { renderWithRedux } from "../../../__mocks__/renderWithRedux";
import { Translate } from "@/utilities/Translate";

jest.mock("next/router", () => ({
useRouter() {
return {
route: "/",
pathname: "",
query: "",
locale: "hr",
};
},
}));

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

jest.mock("../../../src/utilities/Translate", () => ({
Translate: jest.fn(),
}));

describe("TranslationBOX", () => {
const mockTranslate = Translate as jest.Mock;
it("should render the componet", () => {
renderWithRedux(<TranslationBox text="test text" defaultLocale="en" />);
expect(screen.getByText("test text")).toBeInTheDocument();
});

it("should show loading state during translation", async () => {
renderWithRedux(<TranslationBox text="test text" defaultLocale="en" />);

fireEvent.click(await screen.findByRole("button", { name: "Click me" }));
expect(Translate).toHaveBeenCalled();
expect(screen.getByText("Translating...")).toBeInTheDocument();
});

it("Should translate the text when translate button is clicked", async () => {
mockTranslate.mockResolvedValue('Translated Text');
renderWithRedux(<TranslationBox text="test text" defaultLocale="en" />);
expect(screen.getByText("test text")).toBeInTheDocument();
fireEvent.click(await screen.findByRole("button", { name: "Click me" }));
expect(screen.getByText("Translating...")).toBeInTheDocument();
await waitFor(()=> expect(screen.queryByText("Translating...")).toBe(null))
expect(Translate).toHaveBeenCalled();
expect(screen.getByText("Translated Text")).toBeInTheDocument();

});

it("Should revert the text back to its original language when revert is clicked", async () => {
mockTranslate.mockResolvedValueOnce("Hallo");
renderWithRedux(<TranslationBox text="Hello" defaultLocale="en" />);
fireEvent.click(await screen.findByText((content) => content.includes("ui.translate")));
expect(mockTranslate).toHaveBeenCalled();
expect(screen.getByText("Hallo")).toBeInTheDocument();
fireEvent.click(await screen.findByText((content) => content.startsWith("ui.translation.action.original")));
expect(screen.getByText("Hello")).toBeInTheDocument();
});
});
3 changes: 1 addition & 2 deletions src/components/cards/TranslationBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ export default function TranslationBox({ text, defaultLocale, disabled, textCont
newLocale: currentLocale,
text,
});

setCurrentText(translatedText);
setLocale(currentLocale);
} catch (e) {
Expand Down Expand Up @@ -145,7 +144,7 @@ export default function TranslationBox({ text, defaultLocale, disabled, textCont
) : (
<div className="flex divide-x divide-gray-200">
<div className={`pr-3 ${!translated ? "cursor-pointer underline" : ""}`} onClick={translate}>
<span>
<span role="button" aria-label="Click me">
{description} {getLocaleName(currentLocale)}
</span>
</div>
Expand Down