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-7676 Base64Binary component #714

Merged
merged 3 commits into from
Dec 27, 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
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;
Loading