Skip to content
This repository has been archived by the owner on Dec 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #506 from MeasureAuthoringTool/feature/MAT-5981
Browse files Browse the repository at this point in the history
Feature/mat 5981
  • Loading branch information
RohitKandimalla authored Nov 29, 2023
2 parents 15e0f5f + 7f1d6b7 commit e82d960
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 100 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ $header-weight: 500;
}
}

.view-button {
.view-with-dropdown-button {
color: #242424;
border: 1px solid #8c8c8c;
border-radius: 4px;
Expand All @@ -155,14 +155,14 @@ $header-weight: 500;

> div {
border-right: solid 1px #8c8c8c;
padding: 8px 16px;
padding: 8px 20px 8px 4px;
flex-direction: row;
flex-grow: 1;
height: 100%;
}

> svg {
margin: 0 6px;
margin: 0 8px;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {
} from "@tanstack/react-table";
import useQdmExecutionContext from "../../../../../../routes/qdm/useQdmExecutionContext";
import TimingCell from "./TimingCell";
import DatElementActions from "./DatElementActions";
import { generateAttributesToDisplay } from "../../../../../../../util/QdmAttributeHelpers";
import AttributesCell from "./AttributesCell";
import DataElementActions from "./dataElementActions/DataElementActions";

const columnHelper = createColumnHelper<DataElement>();

Expand Down Expand Up @@ -142,13 +142,12 @@ const DataElementTable = ({
cell: (info) => {
const el = info.getValue();
return (
<DatElementActions
<DataElementActions
elementId={el.id}
canEdit={canEdit}
canView={allowedTypes.hasOwnProperty(el._type)}
onDelete={onDelete}
onView={(e) => {
// e.preventDefault();
onView={() => {
onView && onView(el);
}}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import * as React from "react";
import { render, screen, within } from "@testing-library/react";
import DataElementActions from "./DataElementActions";
import userEvent from "@testing-library/user-event";

const mockOnDelete = jest.fn();
const mockOnView = jest.fn();

describe("DatElementActions", () => {
afterEach(() => {
jest.clearAllMocks();
});

it("Should display only View action button for a non owner", () => {
render(
<DataElementActions
elementId={"exampleId"}
canView={true}
onDelete={mockOnDelete}
onView={mockOnView}
canEdit={false}
/>
);

const viewButton = screen.getByRole("button", { name: "View" });
expect(viewButton).toBeInTheDocument();
userEvent.click(viewButton);
expect(mockOnView).toHaveBeenCalledTimes(1);
});

it("Should display View action button along with popover for the owner", async () => {
render(
<DataElementActions
elementId={"exampleId"}
canView={true}
onDelete={mockOnDelete}
onView={mockOnView}
canEdit={true}
/>
);

const viewButton = screen.getByRole("button", { name: "View" });
expect(viewButton).toBeInTheDocument();
userEvent.click(viewButton);
const popOver = await screen.findByTestId("popover-content");
const editButton = within(popOver).getByRole("button", { name: "Edit" });
userEvent.click(editButton);
expect(mockOnView).toHaveBeenCalledTimes(1);
});

it("Should display the delete button if the user is owner and deletes a dataElement when clicked", async () => {
render(
<DataElementActions
elementId={"exampleId"}
canView={true}
onDelete={mockOnDelete}
onView={mockOnView}
canEdit={true}
/>
);

const viewButton = screen.getByRole("button", { name: "View" });
expect(viewButton).toBeInTheDocument();
userEvent.click(viewButton);
const popOver = await screen.findByTestId("popover-content");
const deleteButton = within(popOver).getByRole("button", {
name: "Delete",
});
userEvent.click(deleteButton);
expect(mockOnDelete).toHaveBeenCalledTimes(1);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import React from "react";
import ExpandMoreIcon from "@mui/icons-material/ExpandMore";
import "../DataElementsTable.scss";
import { Button } from "@madie/madie-design-system/dist/react";
import DataElementsTablePopover from "./DataElementsTablePopover";

type DataElementActionsProps = {
elementId: string;
canView: boolean;
onDelete: Function;
onView: Function;
canEdit: boolean;
};

export default function DataElementActions(props: DataElementActionsProps) {
const { elementId, canView, onDelete, onView, canEdit } = props;
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
const open = Boolean(anchorEl);

const handleViewButtonClick = (event: React.MouseEvent<HTMLElement>) => {
if (canEdit) {
event.preventDefault();
setAnchorEl(event.currentTarget);
} else {
onView();
}
};

const handleClose = () => {
setAnchorEl(null);
};

const deleteDataElement = () => {
handleClose();
onDelete(elementId);
};

const deleteElement = canEdit
? {
label: "Delete",
toImplementFunction: deleteDataElement,
dataTestId: `delete-element-${elementId}`,
}
: {};

return (
<div>
{canEdit ? (
<Button
id={`view-element-btn-${elementId}`}
data-testid={`view-element-btn-${elementId}`}
className="view-with-dropdown-button"
aria-controls={open ? `view-element-menu-${elementId}` : undefined}
aria-haspopup="true"
aria-expanded={open ? "true" : undefined}
onClick={handleViewButtonClick}
>
<div>View</div>
<ExpandMoreIcon />
</Button>
) : (
<Button
id={`view-element-btn-${elementId}`}
data-testid={`view-element-btn-${elementId}`}
onClick={handleViewButtonClick}
loading={!canView} //disabled state
variant="primary"
>
View
</Button>
)}
<DataElementsTablePopover
id={`view-element-menu-${elementId}`}
anchorEl={anchorEl}
optionsOpen={open}
handleClose={handleClose}
canEdit={true}
canView={canView}
editSelectOptionProps={{
label: "Edit",
toImplementFunction: () => {
return onView();
},
dataTestId: `edit-element-${elementId}`,
}}
additionalSelectOptionProps={[deleteElement]}
/>
</div>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@ const DataElementsTablePopover = (props: {
handleClose: any;
canView: boolean;
canEdit: boolean;
editViewSelectOptionProps: any;
editSelectOptionProps: any;
additionalSelectOptionProps?: any;
otherSelectOptionProps: any;
}) => {
// canView prop defines if the data Element is still available and is not deleted from CQL
const {
id,
optionsOpen,
anchorEl,
handleClose,
canView,
canEdit,
editViewSelectOptionProps,
editSelectOptionProps,
additionalSelectOptionProps,
otherSelectOptionProps,
} = props;
return (
<div>
Expand Down Expand Up @@ -83,10 +81,11 @@ const DataElementsTablePopover = (props: {
<div className="btn-container">
{canView && (
<button
data-testid={editViewSelectOptionProps.dataTestId}
onClick={editViewSelectOptionProps.toImplementFunction}
key={editSelectOptionProps.dataTestId}
data-testid={editSelectOptionProps.dataTestId}
onClick={editSelectOptionProps.toImplementFunction}
>
{editViewSelectOptionProps.label}
{editSelectOptionProps.label}
</button>
)}
{additionalSelectOptionProps &&
Expand All @@ -101,18 +100,6 @@ const DataElementsTablePopover = (props: {
</button>
);
})}
{canEdit &&
otherSelectOptionProps.map((res) => {
return (
<button
key={res.dataTestId}
data-testid={res.dataTestId}
onClick={res.toImplementFunction}
>
{res.label}
</button>
);
})}
</div>
</div>
</Popover>
Expand Down

0 comments on commit e82d960

Please sign in to comment.