Skip to content

Commit

Permalink
MAT-7372 CQL Library: Unable to draft a library
Browse files Browse the repository at this point in the history
  • Loading branch information
adongare committed Jun 27, 2024
1 parent 5138811 commit 636c8d7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 26 deletions.
41 changes: 29 additions & 12 deletions src/components/createDraftDialog/CreateDraftDialog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,17 @@ const cqlLibrary: CqlLibrary = {
cqlErrors: false,
librarySetId: "",
id: "622e1f46d1fd3729d861e6cb",
cqlLibraryName: "testing1",
cqlLibraryName: "TestLib",
model: Model.QICORE,
createdAt: null,
createdBy: null,
lastModifiedAt: null,
lastModifiedBy: null,
draft: true,
version: "0.0.000",
cql: null,
cql: "library TestLib version '0.0.000'\nusing QICore version '4.1.1'\n",
};

jest.mock("@madie/madie-editor", () => ({
synchingEditorCqlContent: jest.fn().mockResolvedValue({
cql: "library testing1 version '0.0.000'",
}),
}));

describe("Create Draft Dialog component", () => {
beforeEach(() => {
clearAllMocks();
Expand All @@ -43,7 +37,7 @@ describe("Create Draft Dialog component", () => {
expect(screen.getByRole("dialog")).toBeInTheDocument();
expect(
screen.getByRole("textbox", { name: "CQL Library Name" })
).toHaveValue("testing1");
).toHaveValue(cqlLibrary.cqlLibraryName);
});

it("should generate field level error for required Cql Library name", async () => {
Expand Down Expand Up @@ -184,7 +178,27 @@ describe("Create Draft Dialog component", () => {
expect(onCloseFn).toHaveBeenCalled();
});

it("should continue drafting by calling onSubmit", async () => {
it("should not change cql but continue drafting by calling onSubmit when user does not rename library", async () => {
const onSubmitFn = jest.fn();
render(
<CreatDraftDialog
open={true}
onClose={jest.fn()}
onSubmit={onSubmitFn}
cqlLibrary={cqlLibrary}
/>
);
const cqlLibraryNameInput = screen.getByRole("textbox", {
name: "CQL Library Name",
}) as HTMLInputElement;
expect(cqlLibraryNameInput.value).toBe(cqlLibrary.cqlLibraryName);
userEvent.click(screen.getByRole("button", { name: "Continue" }));
await waitFor(() => {
expect(onSubmitFn).toHaveBeenCalledWith(cqlLibrary);
});
});

it("should update the cql and continue drafting by calling onSubmit when user renames the library", async () => {
const onSubmitFn = jest.fn();
render(
<CreatDraftDialog
Expand All @@ -199,10 +213,13 @@ describe("Create Draft Dialog component", () => {
});
userEvent.clear(cqlLibraryNameInput);
userEvent.type(cqlLibraryNameInput, "TestingLibraryName12");
expect(screen.getByRole("button", { name: "Continue" })).not.toBeDisabled();
userEvent.click(screen.getByRole("button", { name: "Continue" }));
await waitFor(() => {
expect(onSubmitFn).toHaveBeenCalled();
expect(onSubmitFn).toHaveBeenCalledWith({
...cqlLibrary,
cql: "library TestingLibraryName12 version '0.0.000'\nusing QICore version '4.1.1'\n",
cqlLibraryName: "TestingLibraryName12",
});
});
});
});
24 changes: 10 additions & 14 deletions src/components/createDraftDialog/CreateDraftDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import { DialogContent, Box, Typography } from "@mui/material";
import { useFormik } from "formik";
import * as Yup from "yup";
import { CqlLibrary } from "@madie/madie-models";
import { synchingEditorCqlContent } from "@madie/madie-editor";
import { MadieDialog, TextField } from "@madie/madie-design-system/dist/react";
import _ from "lodash";

interface CreateDraftDialogProps {
open: boolean;
Expand Down Expand Up @@ -40,21 +38,19 @@ const CreatDraftDialog = ({
});

const submitForm = async (cqlLibraryName: string) => {
const cqlModelInfo: string[] = _.split(cqlLibrary?.model, " v", 2);
const editorContents = await synchingEditorCqlContent(
"",
cqlLibrary?.cql,
cqlLibraryName,
cqlLibrary?.cqlLibraryName,
cqlLibrary?.version,
cqlModelInfo[0],
cqlModelInfo[1],
"draftDialog"
);
let cql = cqlLibrary?.cql;
const previousLibraryName = cqlLibrary?.cqlLibraryName;
// make sure cql is updated with new library name, if it is changed
if (
previousLibraryName !== cqlLibraryName &&
cql?.includes(cqlLibrary.cqlLibraryName)
) {
cql = cql.replace(previousLibraryName, cqlLibraryName);
}
return onSubmit({
...cqlLibrary,
cqlLibraryName,
cql: editorContents?.cql || editorContents,
cql,
});
};

Expand Down

0 comments on commit 636c8d7

Please sign in to comment.