Skip to content

Commit

Permalink
feat: add optional params to redactable value (#112)
Browse files Browse the repository at this point in the history
* feat: add optional params to redactable value

* fix: lint

* chore: add tests

---------

Co-authored-by: Lucas <lucas_yap@tech.gov.sg>
  • Loading branch information
yapyuyou and yapyuyou committed Sep 4, 2023
1 parent 69b85c1 commit 0bd96ea
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
52 changes: 51 additions & 1 deletion src/components/common/RedactableValue.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { RedactableValue } from "./RedactableValue";
import { DEFAULT_NO_VALUE_MSG, DEFAULT_REDACTED_MSG, RedactableValue } from "./RedactableValue";
import { screen, render, fireEvent } from "@testing-library/react";

describe("redactablevalue component", () => {
Expand Down Expand Up @@ -43,4 +43,54 @@ describe("redactablevalue component", () => {
fireEvent.click(screen.getByText("foobar"));
expect(callback).toHaveBeenCalledTimes(1);
});

it("should display default redacted message when no redacted message is specified", () => {
const callback = jest.fn();
render(
<RedactableValue value="Text to display" onRedactionRequested={callback} editable iconRedact={<>foobar</>} />
);

fireEvent.click(screen.getByText("foobar"));
expect(screen.getByText(DEFAULT_REDACTED_MSG)).toBeInTheDocument();
});

it("should display custom redacted message when redacted message is provided", () => {
const callback = jest.fn();
const CUSTOM_REDACTED_MSG = "custom redacted message";
render(
<RedactableValue
value="Text to display"
onRedactionRequested={callback}
editable
iconRedact={<>foobar</>}
redactedMessage={CUSTOM_REDACTED_MSG}
/>
);

fireEvent.click(screen.getByText("foobar"));
expect(screen.getByText(CUSTOM_REDACTED_MSG)).toBeInTheDocument();
});

it("should display default no value message when no custom message is specified", () => {
const callback = jest.fn();
render(<RedactableValue value={undefined} onRedactionRequested={callback} editable iconRedact={<>foobar</>} />);

expect(screen.getByText(DEFAULT_NO_VALUE_MSG)).toBeInTheDocument();
});

it("should display custom no value message when provided", () => {
const callback = jest.fn();
const CUSTOM_NO_VALUE_MSG = "custom no value message";
render(
<RedactableValue
value={undefined}
onRedactionRequested={callback}
editable
iconRedact={<>foobar</>}
noValueMessage={CUSTOM_NO_VALUE_MSG}
/>
);

expect(screen.getByText(CUSTOM_NO_VALUE_MSG)).toBeInTheDocument();
});
});
21 changes: 19 additions & 2 deletions src/components/common/RedactableValue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ export interface RedactValueProps {
onRedactionRequested?: () => void;
editable?: boolean;
iconRedact?: React.ReactElement;
redactedMessage?: string;
noValueMessage?: string;
}

export const DEFAULT_REDACTED_MSG = "**Redacted**";
export const DEFAULT_NO_VALUE_MSG = "**Field value does not exist**";

/**
* RedactableValue component is almost a duplicate of ObfuscatableValue component
* ObfuscatableValue component started from OpenCerts, and may be in use on existing certificates, hence we do not want to meddle with the existing functionality
Expand All @@ -38,11 +43,23 @@ export const RedactableValue: FunctionComponent<RedactValueProps> = ({
onRedactionRequested = noop,
editable = false,
iconRedact = <IconRedact />,
redactedMessage,
noValueMessage,
}) => {
const [isRedacted, setRedacted] = useState(false);

if (isRedacted) return <span style={{ display: "inline-block", color: "#454B50" }}>**Redacted**</span>;
if (!value) return <span style={{ display: "inline-block", color: "#454B50" }}>**Field value does not exists**</span>;
if (isRedacted)
return (
<span style={{ display: "inline-block", color: "#454B50" }}>
{redactedMessage ? redactedMessage : DEFAULT_REDACTED_MSG}
</span>
);
if (!value)
return (
<span style={{ display: "inline-block", color: "#454B50" }}>
{noValueMessage ? noValueMessage : DEFAULT_NO_VALUE_MSG}
</span>
);

return (
<>
Expand Down

0 comments on commit 0bd96ea

Please sign in to comment.