Skip to content

Commit

Permalink
added tests for BreakTimeNotificationOverview
Browse files Browse the repository at this point in the history
  • Loading branch information
DerKatsche committed Oct 18, 2024
1 parent f90908e commit f51183f
Show file tree
Hide file tree
Showing 3 changed files with 407 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function BreakTimeNotificationThumbnail({

return (
<div>
<StyledButton onClick={onClick}>
<StyledButton onClick={onClick} data-testid={notification.titleKey}>
<img
src={notification.images[0]}
alt={translate(notification.titleKey) + "Thumbnail"}
Expand Down Expand Up @@ -62,8 +62,8 @@ export default function BreakTimeNotificationOverview() {
IBreakTimeNotificationOverviewController
>(BUILDER_TYPES.IBreakTimeNotificationOverviewBuilder);

const [showModal] = useObservable(viewModel.showModal);
const [selectedNotification] = useObservable(viewModel.selectedNotification);
const [showModal] = useObservable(viewModel?.showModal);
const [selectedNotification] = useObservable(viewModel?.selectedNotification);

const { t: translate } = useTranslation("breakTime");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { act, fireEvent, render } from "@testing-library/react";
import React from "react";
import BreakTimeNotificationOverview from "../../../../../Core/Presentation/React/GeneralComponents/BreakTimeNotificationOverview/BreakTimeNotificationOverview";
import { Provider } from "inversify-react";
import CoreDIContainer from "../../../../../Core/DependencyInjection/CoreDIContainer";
import useBuilderMock from "../../ReactRelated/CustomHooks/useBuilder/useBuilderMock";
import BreakTimeNotificationOverviewViewModel from "../../../../../Core/Presentation/React/GeneralComponents/BreakTimeNotificationOverview/BreakTimeNotificationOverviewViewModel";
import "@testing-library/jest-dom";
import IBreakTimeNotificationOverviewController from "../../../../../Core/Presentation/React/GeneralComponents/BreakTimeNotificationOverview/IBreakTimeNotificationOverviewController";
import mock from "jest-mock-extended/lib/Mock";
import { get } from "http";

describe("BreakTimeNotificationOverview", () => {
test("should render", () => {
const viewModel = new BreakTimeNotificationOverviewViewModel();
viewModel.showModal.Value = true;
useBuilderMock([
viewModel,
mock<IBreakTimeNotificationOverviewController>(),
]);

const renderResult = render(
<Provider container={CoreDIContainer}>
<BreakTimeNotificationOverview />
</Provider>,
);

expect(renderResult.container).not.toBeEmptyDOMElement();
expect(renderResult.container).toMatchSnapshot();
});

test("should not render without view model", () => {
useBuilderMock([undefined, undefined]);

const renderResult = render(
<Provider container={CoreDIContainer}>
<BreakTimeNotificationOverview />
</Provider>,
);

expect(renderResult.container.firstChild).toBeNull();
});

test("should not render without view model show modal value", () => {
const viewModel = new BreakTimeNotificationOverviewViewModel();
viewModel.showModal.Value = false;
useBuilderMock([
viewModel,
mock<IBreakTimeNotificationOverviewController>(),
]);

const renderResult = render(
<Provider container={CoreDIContainer}>
<BreakTimeNotificationOverview />
</Provider>,
);

expect(renderResult.container.firstChild).toBeNull();
});

test("should not render without controller", () => {
const viewModel = new BreakTimeNotificationOverviewViewModel();
viewModel.showModal.Value = true;
useBuilderMock([viewModel, undefined]);

const renderResult = render(
<Provider container={CoreDIContainer}>
<BreakTimeNotificationOverview />
</Provider>,
);

expect(renderResult.container.firstChild).toBeNull();
});

test("calls controller with clicked notification", () => {
const viewModel = new BreakTimeNotificationOverviewViewModel();
viewModel.showModal.Value = true;
const controllerMock = mock<IBreakTimeNotificationOverviewController>();
useBuilderMock([viewModel, controllerMock]);

const renderResult = render(
<Provider container={CoreDIContainer}>
<BreakTimeNotificationOverview />
</Provider>,
);

const notificationElement = renderResult.getByTestId(
viewModel.shortBreakTimeNotifications[0].titleKey,
);
act(() => {
fireEvent.click(notificationElement);
});

expect(controllerMock.selectNotification).toHaveBeenCalledTimes(1);
expect(controllerMock.selectNotification).toHaveBeenCalledWith(
viewModel.shortBreakTimeNotifications[0],
);
});
});
Loading

0 comments on commit f51183f

Please sign in to comment.