Skip to content

Commit

Permalink
MAT-7905 Adding discardDialog for MeasureActionCenter.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
RohitKandimalla committed Nov 18, 2024
1 parent e54c837 commit 4499b46
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from "react";
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
import * as React from "react";
import { render, screen } from "@testing-library/react";
import MeasureActionCenter from "./MeasureActionCenter";
import { Measure } from "@madie/madie-models";
import userEvent from "@testing-library/user-event";

const draftMeasure = {
id: "measure ID",
Expand All @@ -23,19 +24,10 @@ describe("MeasureActionCenter Component", () => {
expect(screen.getByTestId("action-center")).toBeInTheDocument();
});

it("should open action center on button click", () => {
render(<MeasureActionCenter canEdit={true} measure={draftMeasure} />);
const actionCenterButton = screen.getByTestId("action-center-button");
fireEvent.click(actionCenterButton);
expect(screen.getByTestId("DeleteMeasure")).toBeInTheDocument();
expect(screen.getByTestId("VersionMeasure")).toBeInTheDocument();
expect(screen.getByTestId("ExportMeasure")).toBeInTheDocument();
});

it("should open action center on button click", () => {
render(<MeasureActionCenter canEdit={true} measure={versionedMeasure} />);
const actionCenterButton = screen.getByTestId("action-center-button");
fireEvent.click(actionCenterButton);
const actionCenterButton = screen.getByLabelText("Measure action center");
userEvent.click(actionCenterButton);
expect(screen.queryByTestId("DeleteMeasure")).not.toBeInTheDocument();
expect(screen.queryByTestId("VersionMeasure")).not.toBeInTheDocument();
expect(screen.getByTestId("DraftMeasure")).toBeInTheDocument();
Expand All @@ -44,13 +36,11 @@ describe("MeasureActionCenter Component", () => {

it("should trigger delete-measure event when 'Delete Measure' action is clicked", () => {
const dispatchEventSpy = jest.spyOn(window, "dispatchEvent");

render(<MeasureActionCenter canEdit={true} measure={draftMeasure} />);

const actionCenterButton = screen.getByTestId("action-center-button");
fireEvent.click(actionCenterButton);
const actionCenterButton = screen.getByLabelText("Measure action center");
userEvent.click(actionCenterButton);
const deleteMeasureButton = screen.getByTestId("DeleteMeasure");
fireEvent.click(deleteMeasureButton);
userEvent.click(deleteMeasureButton);

expect(dispatchEventSpy).toHaveBeenCalledWith(
expect.objectContaining({
Expand All @@ -60,12 +50,26 @@ describe("MeasureActionCenter Component", () => {
});
it("should trigger export-measure event when 'Export Measure' action is clicked", () => {
const dispatchEventSpy = jest.spyOn(window, "dispatchEvent");
render(<MeasureActionCenter />);
render(<MeasureActionCenter canEdit={true} measure={draftMeasure} />);
const actionCenterButton = screen.getByLabelText("Measure action center");
userEvent.click(actionCenterButton);
const exportMeasureButton = screen.getByTestId("ExportMeasure");
userEvent.click(exportMeasureButton);

const actionCenterButton = screen.getByTestId("action-center-button");
fireEvent.click(actionCenterButton);
expect(dispatchEventSpy).toHaveBeenCalledWith(
expect.objectContaining({
type: "export-measure",
})
);
});

it("should display discard dialog when a user has unsaved changes", () => {
const dispatchEventSpy = jest.spyOn(window, "dispatchEvent");
render(<MeasureActionCenter canEdit={true} measure={draftMeasure} />);
const actionCenterButton = screen.getByLabelText("Measure action center");
userEvent.click(actionCenterButton);
const exportMeasureButton = screen.getByTestId("ExportMeasure");
fireEvent.click(exportMeasureButton);
userEvent.click(exportMeasureButton);

expect(dispatchEventSpy).toHaveBeenCalledWith(
expect.objectContaining({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import React, { useState, useEffect } from "react";

import { SpeedDial, SpeedDialAction } from "@mui/material";

import DeleteOutlinedIcon from "@mui/icons-material/DeleteOutlined";

import EditCalendarOutlinedIcon from "@mui/icons-material/EditCalendarOutlined";
import FileUploadOutlinedIcon from "@mui/icons-material/FileUploadOutlined";
import AccountTreeOutlinedIcon from "@mui/icons-material/AccountTreeOutlined";

import { MadieDiscardDialog } from "@madie/madie-design-system/dist/react";
import { Measure } from "@madie/madie-models";
import { blue, red } from "@mui/material/colors";
import { RouteHandlerState, routeHandlerStore } from "@madie/madie-util";

interface PropTypes {
canEdit: boolean;
Expand All @@ -19,49 +17,78 @@ interface PropTypes {
const MeasureActionCenter = (props: PropTypes) => {
const [open, setOpen] = useState(false);
const [actions, setActions] = useState<Array<any>>([]);
const [discardDialogOpen, setDiscardDialogOpen] = useState<boolean>(false);
const [eventToTrigger, setEventToTrigger] = useState<Event | null>(null);

const { updateRouteHandlerState } = routeHandlerStore;
const [routeHandlerState, setRouteHandlerState] = useState<RouteHandlerState>(
routeHandlerStore.state
);

useEffect(() => {
const subscription = routeHandlerStore.subscribe(setRouteHandlerState);
return () => {
subscription.unsubscribe();
};
}, []);

useEffect(() => {
setActions(getActionArray(props.measure, props.canEdit));
}, [props]);
}, [props, routeHandlerState]);

Check warning on line 37 in src/components/PageHeader/MeasureActionCenter/MeasureActionCenter.tsx

View workflow job for this annotation

GitHub Actions / Checkout, install, lint, build and test with coverage

React Hook useEffect has a missing dependency: 'getActionArray'. Either include it or remove the dependency array

const onContinue = () => {
setDiscardDialogOpen(false);

Check warning on line 40 in src/components/PageHeader/MeasureActionCenter/MeasureActionCenter.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/PageHeader/MeasureActionCenter/MeasureActionCenter.tsx#L40

Added line #L40 was not covered by tests
if (eventToTrigger) {
window.dispatchEvent(eventToTrigger);

Check warning on line 42 in src/components/PageHeader/MeasureActionCenter/MeasureActionCenter.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/PageHeader/MeasureActionCenter/MeasureActionCenter.tsx#L42

Added line #L42 was not covered by tests
}
updateRouteHandlerState({

Check warning on line 44 in src/components/PageHeader/MeasureActionCenter/MeasureActionCenter.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/PageHeader/MeasureActionCenter/MeasureActionCenter.tsx#L44

Added line #L44 was not covered by tests
canTravel: true,
pendingRoute: "",
});
setEventToTrigger(null);

Check warning on line 48 in src/components/PageHeader/MeasureActionCenter/MeasureActionCenter.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/PageHeader/MeasureActionCenter/MeasureActionCenter.tsx#L48

Added line #L48 was not covered by tests
};

const onClose = () => {
setDiscardDialogOpen(false);
setEventToTrigger(null);

Check warning on line 53 in src/components/PageHeader/MeasureActionCenter/MeasureActionCenter.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/PageHeader/MeasureActionCenter/MeasureActionCenter.tsx#L52-L53

Added lines #L52 - L53 were not covered by tests
};

const handleActionClick = (event: Event) => {
if (routeHandlerState?.canTravel) {
window.dispatchEvent(event);
} else {
setEventToTrigger(event);
setDiscardDialogOpen(true);

Check warning on line 61 in src/components/PageHeader/MeasureActionCenter/MeasureActionCenter.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/PageHeader/MeasureActionCenter/MeasureActionCenter.tsx#L60-L61

Added lines #L60 - L61 were not covered by tests
}
};

const getActionArray = (measure: Measure, canEdit: boolean): any[] => {
const actions: any[] = [
{
icon: <FileUploadOutlinedIcon sx={{ color: blue[500] }} />,
name: "Export Measure",
onClick: () => {
const eventExport = new Event("export-measure");
window.dispatchEvent(eventExport);
},
onClick: () => handleActionClick(new Event("export-measure")),
},
];

if (canEdit) {
if (measure?.measureMetaData.draft) {
actions.push({
icon: <DeleteOutlinedIcon sx={{ color: red[500] }} />,
name: "Delete Measure",
onClick: () => {
const event = new Event("delete-measure");
window.dispatchEvent(event);
},
onClick: () => handleActionClick(new Event("delete-measure")),
});
actions.push({
icon: <AccountTreeOutlinedIcon sx={{ color: blue[500] }} />,
name: "Version Measure",
onClick: () => {
const event = new Event("version-measure");
window.dispatchEvent(event);
},
onClick: () => handleActionClick(new Event("version-measure")),

Check warning on line 84 in src/components/PageHeader/MeasureActionCenter/MeasureActionCenter.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/PageHeader/MeasureActionCenter/MeasureActionCenter.tsx#L84

Added line #L84 was not covered by tests
});
}
if (!measure?.measureMetaData.draft) {
actions.push({
icon: <EditCalendarOutlinedIcon sx={{ color: blue[500] }} />,
name: "Draft Measure",
onClick: () => {
const event = new Event("draft-measure");
window.dispatchEvent(event);
},
onClick: () => handleActionClick(new Event("draft-measure")),

Check warning on line 91 in src/components/PageHeader/MeasureActionCenter/MeasureActionCenter.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/PageHeader/MeasureActionCenter/MeasureActionCenter.tsx#L91

Added line #L91 was not covered by tests
});
}
}
Expand Down Expand Up @@ -133,6 +160,11 @@ const MeasureActionCenter = (props: PropTypes) => {
/>
))}
</SpeedDial>
<MadieDiscardDialog
open={discardDialogOpen}
onContinue={onContinue}
onClose={onClose}
/>
</div>
);
};
Expand Down

0 comments on commit 4499b46

Please sign in to comment.