Skip to content

Commit

Permalink
Merge branch 'develop' into MAT-7797_editFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
sb-cecilialiu authored Dec 30, 2024
2 parents 638eead + df921db commit d999590
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as React from "react";
import { render, screen } from "@testing-library/react";
import OidComponent from "./OidComponent";
import userEvent from "@testing-library/user-event";

describe("OidComponent", () => {
it("Should render OidComponent", async () => {
const handleChange = jest.fn();

render(
<OidComponent
value={null}
label="OID"
canEdit={true}
fieldRequired={false}
onChange={handleChange}
structureDefinition={null}
/>
);

expect(screen.getByText("OID")).toBeInTheDocument();
expect(screen.getByTestId("field-input-OID")).toBeInTheDocument();
});

it("Should validate input", async () => {
const handleChange = jest.fn();

render(
<OidComponent
value={null}
label="OID"
canEdit={true}
fieldRequired={false}
onChange={handleChange}
structureDefinition={null}
/>
);

const oidInput = screen.getByTestId("field-input-OID") as HTMLInputElement;

// valid oidInput
userEvent.type(oidInput, "urn:oid:1.2.3.4.5");
expect(oidInput.value).toBe("urn:oid:1.2.3.4.5");
expect(
screen.getByTestId("field-input-helper-text-OID")
).not.toHaveTextContent("Please enter a valid OID");

// invalid oidInput
userEvent.clear(oidInput);
userEvent.type(oidInput, "invalid OID.");
expect(oidInput).toHaveValue("invalid OID.");
expect(screen.getByTestId("field-input-helper-text-OID")).toHaveTextContent(
"Please enter a valid OID"
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React, { useState } from "react";
import { FormHelperText } from "@mui/material";
import { TypeComponentProps } from "./TypeComponentProps";
import { TextField } from "@madie/madie-design-system/dist/react";
import _ from "lodash";

const OidComponent = ({
canEdit,
fieldRequired,
label = "OID",
value,
onChange,
structureDefinition,
}: TypeComponentProps) => {
const oidRegex = /urn:oid:[0-2](\.(0|[1-9][0-9]*))+/;
const [isValid, setValid] = useState<boolean>(
value ? value.match(oidRegex) : true
);

const handleChange = (oid) => {
setValid(true);
if (oid.match(oidRegex)) {
onChange(oid);
} else if (!_.isEmpty(oid)) {
setValid(false);
}
};
return (
<TextField
label={`${label}`}
required={fieldRequired}
disabled={!canEdit}
inputProps={{
"data-testid": `field-input-${label}`,
"aria-describedby": `field-input-helper-text-${label}`,
required: fieldRequired,
"aria-required": fieldRequired,
}}
size="small"
fullWidth
value={value}
onChange={(e) => handleChange(e.target.value)}
helperText={
<FormHelperText
data-testid={`field-input-helper-text-${label}`}
error={!isValid}
>
{isValid ? "" : "Please enter a valid OID"}
</FormHelperText>
}
/>
);
};

export default OidComponent;

0 comments on commit d999590

Please sign in to comment.