Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAT-7796: Edit dialog layout #411

Merged
merged 5 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/CqlBuilderPanel/CqlBuilderPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
} else {
setLoading(false);
}
}, [measureModel, measureStoreCql]);

Check warning on line 128 in src/CqlBuilderPanel/CqlBuilderPanel.tsx

View workflow job for this annotation

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

React Hook useEffect has missing dependencies: 'fhirElmTranslationServiceApi' and 'qdmElmTranslationServiceApi'. Either include them or remove the dependency array

return (
<div className="right-panel">
Expand Down Expand Up @@ -247,6 +247,7 @@
loading={loading}
cql={measureStoreCql}
isCQLUnchanged={isCQLUnchanged}
resetCql={resetCql}
/>
)}
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as React from "react";
import { parseArgumentsFromLogicString } from "./EditFunctionDialog";

describe("parseArgumentsFromLogicString", () => {
test("Can parse out empty arguments", () => {
const logicString = `define function "Empty Arguments" (): true`;
const result = parseArgumentsFromLogicString(logicString);
expect(result).toEqual([]);
});
test("Can parse out multiple arguments", () => {
const logicString2 = `define function "Function name here" (arg1 "Integer", arg2 "Integer", arg3 "Date"):\n true`;
const result = parseArgumentsFromLogicString(logicString2);

expect(result).toEqual([
{ argumentName: "arg1", dataType: "Integer" },
{ argumentName: "arg2", dataType: "Integer" },
{ argumentName: "arg3", dataType: "Date" },
]);
});
test("Can parse out arguments with commas embedded in dataType", () => {
const logicString3 = `define fluent function "Numerator Observation"(Encounter "Encounter, Performed" ):
duration in hours of Encounter.relevantPeriod`;
const result = parseArgumentsFromLogicString(logicString3);
expect(result).toEqual([
{ argumentName: "Encounter", dataType: "Encounter, Performed" },
]);
});
});
89 changes: 89 additions & 0 deletions src/CqlBuilderPanel/functionsSection/EditFunctionDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import React from "react";
import { MadieDialog } from "@madie/madie-design-system/dist/react";
import FunctionBuilder from "./functionBuilder/FunctionBuilder";

interface PropTypes {
open: boolean;
onClose: () => void;
cqlBuilderLookupsTypes?: any;
funct?: any;
setEditFunctionDialogOpen: Function;
handleApplyFunction: Function;
handleFunctionEdit: Function;
}

export const parseArgumentsFromLogicString = (logicString) => {
// `s` flag for multiline content
const argumentListRegex = /\(([^)]*)\)/s;
const match = logicString.match(argumentListRegex);

// Nobody in parenthesis
if (!match) {
return [];
}

const argumentsString = match[1].trim();
// no args
if (!argumentsString) {
return [];
}

// Regex to match argument and data type pairs
const argumentRegex = /([\w]+)\s+"([^"]+)"/g;
const results = [];

let argumentMatch;
while ((argumentMatch = argumentRegex.exec(argumentsString)) !== null) {
const [, argumentName, dataType] = argumentMatch;
results.push({ argumentName, dataType });
}
return results;
};

const EditFunctionDialog = ({
funct,
handleFunctionEdit,
onClose,
open,
setEditFunctionDialogOpen,
cqlBuilderLookupsTypes,
handleApplyFunction,
}: PropTypes) => {
// a property is passed called argument names that does not seem to work for anything with commas.
// the following is regex to grab arguments and dataTypes using a matcher and add them to the function to edit.

const updatedFunction = {
...funct,
fluentFunction: funct?.isFluent === true ? true : false,
functionsArguments: parseArgumentsFromLogicString(
funct?.logic ? funct?.logic : ""
),
expressionEditorValue: funct?.logic,
};

return (
<MadieDialog
title="Edit"
dialogProps={{
open,
onClose: onClose,
fullWidth: true,
maxWidth: "md",
"data-testid": "edit-parameter-dialog",
}}
>
<FunctionBuilder
cqlBuilderLookupsTypes={cqlBuilderLookupsTypes}
canEdit={true}
handleApplyFunction={handleApplyFunction}
funct={updatedFunction}
operation="edit"
handleFunctionEdit={handleFunctionEdit}
onClose={onClose}
setEditFunctionDialogOpen={setEditFunctionDialogOpen}
/>
</MadieDialog>
);
};

export default EditFunctionDialog;
115 changes: 115 additions & 0 deletions src/CqlBuilderPanel/functionsSection/FunctionsSection.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import userEvent from "@testing-library/user-event";
import { mockMeasureStoreCql } from "../__mocks__/MockMeasureStoreCql";
import { cqlBuilderLookup } from "../__mocks__/MockCqlBuilderLookupsTypes";

const resetCql = jest.fn();

const props = {
canEdit: true,
loading: false,
handleApplyFunction: jest.fn(),
cql: mockMeasureStoreCql,
isCQLUnchanged: false,
cqlBuilderLookupsTypes: cqlBuilderLookup,
resetCql,
};

describe("FunctionsSection", () => {
Expand Down Expand Up @@ -62,4 +65,116 @@ describe("FunctionsSection", () => {
expect(funct).toBeInTheDocument();
expect(savedfunctions).toBeInTheDocument();
});

it("Should open a confirmation dialog on click", async () => {
render(<FunctionsSection {...props} isCQLUnchanged={false} />);
const funct = await screen.findByTestId("function-tab");
const savedfunctions = await screen.findByText("Saved Functions (2)");
expect(funct).toBeInTheDocument();
expect(savedfunctions).toBeInTheDocument();
await waitFor(() => {
expect(funct).toHaveAttribute("aria-selected", "true");
});
await waitFor(() => {
expect(savedfunctions).toHaveAttribute("aria-selected", "false");
});
userEvent.click(savedfunctions);
await waitFor(() => {
expect(savedfunctions).toHaveAttribute("aria-selected", "true");
});
const editButon0 = screen.getByTestId("edit-button-0");
userEvent.click(editButon0);
expect(screen.getByTestId("discard-dialog")).toBeInTheDocument();
expect(screen.getByText("Discard Changes?")).toBeInTheDocument();
const cancelBtn = screen.getByTestId("discard-dialog-cancel-button");
const discardBtn = screen.getByTestId("discard-dialog-continue-button");
expect(cancelBtn).toBeInTheDocument();
expect(discardBtn).toBeInTheDocument();

userEvent.click(discardBtn);
await waitFor(() => {
expect(screen.getByText("Edit")).toBeInTheDocument();
});
});

it("Should open edit dialog on click", async () => {
render(<FunctionsSection {...props} isCQLUnchanged={true} />);
const funct = await screen.findByTestId("function-tab");
const savedfunctions = await screen.findByText("Saved Functions (2)");
expect(funct).toBeInTheDocument();
expect(savedfunctions).toBeInTheDocument();
await waitFor(() => {
expect(funct).toHaveAttribute("aria-selected", "true");
});
await waitFor(() => {
expect(savedfunctions).toHaveAttribute("aria-selected", "false");
});
userEvent.click(savedfunctions);
await waitFor(() => {
expect(savedfunctions).toHaveAttribute("aria-selected", "true");
});
const editButon0 = screen.getByTestId("edit-button-0");
userEvent.click(editButon0);
await waitFor(() => {
expect(screen.getByText("Edit")).toBeInTheDocument();
});
});

it("Should close discard dialog on click", async () => {
render(<FunctionsSection {...props} isCQLUnchanged={false} />);
const funct = await screen.findByTestId("function-tab");
const savedfunctions = await screen.findByText("Saved Functions (2)");
expect(funct).toBeInTheDocument();
expect(savedfunctions).toBeInTheDocument();
await waitFor(() => {
expect(funct).toHaveAttribute("aria-selected", "true");
});
await waitFor(() => {
expect(savedfunctions).toHaveAttribute("aria-selected", "false");
});
userEvent.click(savedfunctions);
await waitFor(() => {
expect(savedfunctions).toHaveAttribute("aria-selected", "true");
});
const editButon0 = screen.getByTestId("edit-button-0");
userEvent.click(editButon0);
expect(screen.getByTestId("discard-dialog")).toBeInTheDocument();
expect(screen.getByText("Discard Changes?")).toBeInTheDocument();
const cancelBtn = screen.getByTestId("discard-dialog-cancel-button");
expect(cancelBtn).toBeInTheDocument();
userEvent.click(cancelBtn);

await waitFor(() => {
expect(screen.queryByTestId("discard-dialog")).not.toBeInTheDocument();
});
});

it("Should close edit dialog on click", async () => {
render(<FunctionsSection {...props} isCQLUnchanged={true} />);
const funct = await screen.findByTestId("function-tab");
const savedfunctions = await screen.findByText("Saved Functions (2)");
expect(funct).toBeInTheDocument();
expect(savedfunctions).toBeInTheDocument();
await waitFor(() => {
expect(funct).toHaveAttribute("aria-selected", "true");
});
await waitFor(() => {
expect(savedfunctions).toHaveAttribute("aria-selected", "false");
});
userEvent.click(savedfunctions);
await waitFor(() => {
expect(savedfunctions).toHaveAttribute("aria-selected", "true");
});
const editButon0 = screen.getByTestId("edit-button-0");
userEvent.click(editButon0);
await waitFor(() => {
expect(screen.getByText("Edit")).toBeInTheDocument();
});
const closeButton = screen.getByRole("button", { name: "Close" });
expect(closeButton).toBeInTheDocument();
userEvent.click(closeButton);
await waitFor(() => {
expect(screen.queryByTestId("discard-dialog")).not.toBeInTheDocument();
});
});
});
14 changes: 8 additions & 6 deletions src/CqlBuilderPanel/functionsSection/FunctionsSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import "./Functions.scss";
import FunctionSectionNavTabs from "./FunctionSectionNavTabs";
import Functions from "./functions/Functions";
import FunctionBuilder from "./functionBuilder/FunctionBuilder";
import {
CqlBuilderLookup,
FunctionLookup,
} from "../..//model/CqlBuilderLookup";
import { CqlBuilderLookup, FunctionLookup } from "../../model/CqlBuilderLookup";
import * as _ from "lodash";
import { CqlAntlr } from "@madie/cql-antlr-parser/dist/src";

Expand All @@ -18,7 +15,9 @@ export interface FunctionProps {
cql: string;
isCQLUnchanged: boolean;
functions?: FunctionLookup[];
resetCql?: Function;
}

const getArgumentNames = (logic: string) => {
const args = logic.substring(logic.indexOf("(") + 1, logic.indexOf(")"));
return args.split(",");
Expand All @@ -27,10 +26,11 @@ const getArgumentNames = (logic: string) => {
export default function FunctionsSection({
canEdit,
handleApplyFunction,
loading,
cql,
isCQLUnchanged,
cqlBuilderLookupsTypes,
resetCql,
loading,
}: FunctionProps) {
const [activeTab, setActiveTab] = useState<string>("function");

Expand Down Expand Up @@ -70,7 +70,6 @@ export default function FunctionsSection({
}) || []
);
functionLookups = _.sortBy(functionLookups, (o) => o.name?.toLowerCase());

return (
<>
<FunctionSectionNavTabs
Expand All @@ -89,11 +88,14 @@ export default function FunctionsSection({
)}
{activeTab === "saved-functions" && (
<Functions
cqlBuilderLookupsTypes={cqlBuilderLookupsTypes}
canEdit={canEdit}
loading={loading}
functions={functionLookups}
isCQLUnchanged={isCQLUnchanged}
cql={cql}
resetCql={resetCql}
handleApplyFunction={handleApplyFunction}
/>
)}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,20 @@ describe("CQL Function Builder Tests", () => {

const functionArgumentTable = screen.getByTestId("function-argument-tbl");
expect(functionArgumentTable).toBeInTheDocument();
await waitFor(() => {
expect(
screen.getByTestId("function-builder-success")
).toBeInTheDocument();
});
const tableRow = functionArgumentTable.querySelector("tbody").children[0];
expect(tableRow.children[1].textContent).toEqual("Test");
const closeButton = screen.getByTestId(
"function-builder-toast-close-button"
);
userEvent.click(closeButton);
await waitFor(() => {
expect(closeButton).not.toBeInTheDocument();
});
});

it("Should delete argument from the table", async () => {
Expand Down
Loading
Loading