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 fc6a41e + 379efa9 commit 638eead
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import * as React from "react";
import { render, screen } from "@testing-library/react";
import Base64BinaryComponent from "./Base64BinaryComponent";
import userEvent from "@testing-library/user-event";

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

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

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

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

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

const base64BinaryInput = screen.getByTestId(
"field-input-Base64BinaryComponent"
) as HTMLInputElement;

// valid base64BinaryInput
userEvent.type(base64BinaryInput, "abcd");
expect(base64BinaryInput.value).toBe("abcd");
expect(
screen.getByTestId("field-input-helper-text-Base64BinaryComponent")
).toBeEmptyDOMElement();

// invalid base64BinaryInput
userEvent.clear(base64BinaryInput);
userEvent.type(base64BinaryInput, "invalid base64BinaryInput.");
expect(base64BinaryInput).toHaveValue("invalid base64BinaryInput.");
expect(
screen.getByTestId("field-input-helper-text-Base64BinaryComponent")
).toHaveTextContent("Please enter a valid Base64Binary");
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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 isBase64 = (str) => {
try {
const decoded = atob(str);
return decoded && new RegExp(/(\s*([0-9a-zA-Z\+\=]){4}\s*)+/).test(str);
} catch (err) {
return false;
}
};

const Base64BinaryComponent = ({
canEdit,
fieldRequired,
label = "Base64Binary",
value,
onChange,
structureDefinition,
}: TypeComponentProps) => {
const [isValid, setValid] = useState<boolean>(value ? isBase64(value) : true);

const handleChange = (base64BinaryString) => {
setValid(true);
if (isBase64(base64BinaryString)) {
onChange(base64BinaryString);
} else if (!_.isEmpty(base64BinaryString)) {
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 Base64Binary"}
</FormHelperText>
}
/>
);
};

export default Base64BinaryComponent;

0 comments on commit 638eead

Please sign in to comment.