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

Return toastId when calling toastr #2179

Merged
merged 1 commit into from
May 7, 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
35 changes: 10 additions & 25 deletions src/components/Toastr/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,27 @@ const withUniqueCheck =
customConfig = {},
} = parseToastrConfig(toastrConfig);

let toastId = null;

if (toastrList.add({ type, message, buttonLabel })) {
const config = {
...TOAST_CONFIG,
icon: TOAST_ICON[type],
onClose: () => toastrList.remove({ type, message, buttonLabel }),
...customConfig,
};
toastFunc({ message, buttonLabel, onClick, config });

toastId = toastFunc({ message, buttonLabel, onClick, config });
}

return toastId;
};

const showSuccessToastr = withUniqueCheck(
"success",
({ message, buttonLabel, onClick, config }) =>
toast.success(
<Toast
buttonLabel={buttonLabel}
message={message}
type="success"
onClick={onClick}
/>,
<Toast {...{ buttonLabel, message, onClick }} type="success" />,
config
)
);
Expand All @@ -102,12 +102,7 @@ const showInfoToastr = withUniqueCheck(
"info",
({ message, buttonLabel, onClick, config }) =>
toast.info(
<Toast
buttonLabel={buttonLabel}
message={message}
type="info"
onClick={onClick}
/>,
<Toast {...{ buttonLabel, message, onClick }} type="info" />,
config
)
);
Expand All @@ -116,12 +111,7 @@ const showWarningToastr = withUniqueCheck(
"warning",
({ message, buttonLabel, onClick, config }) =>
toast.warning(
<Toast
buttonLabel={buttonLabel}
message={message}
type="warning"
onClick={onClick}
/>,
<Toast {...{ buttonLabel, message, onClick }} type="warning" />,
config
)
);
Expand Down Expand Up @@ -194,12 +184,7 @@ const withParsedErrorMsg =
const showErrorToastr = withParsedErrorMsg(
withUniqueCheck("error", ({ message, buttonLabel, onClick, config }) =>
toast.error(
<Toast
buttonLabel={buttonLabel}
message={message}
type="error"
onClick={onClick}
/>,
<Toast {...{ buttonLabel, message, onClick }} type="error" />,
config
)
)
Expand Down
14 changes: 10 additions & 4 deletions tests/Toastr.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const renderToastrButton = (
render(
<>
<ToastContainer />
<Button label={`${type} Toastr`} onClick={onClick} />
<Button {...{ onClick }} label={`${type} Toastr`} />
</>
);

Expand All @@ -40,7 +40,7 @@ const renderCustomConfigToastrButton = type => {
render(
<>
<ToastContainer />
<Button label={`${type} Toastr`} onClick={onClick} />
<Button {...{ onClick }} label={`${type} Toastr`} />
</>
);

Expand All @@ -52,7 +52,7 @@ const renderCustomMessageToastrButton = (type, message) => {
render(
<>
<ToastContainer />
<Button label={`${type} Toastr`} onClick={onClick} />
<Button {...{ onClick }} label={`${type} Toastr`} />
</>
);

Expand Down Expand Up @@ -213,7 +213,7 @@ describe("Toastr", () => {
expect(errorToastr).toBeInTheDocument();
});

it("should render Axios Error Toastr when response is undefined", async () => {
it("should render Axios Error Toastr when response is undefined", () => {
const errorResponse = undefined;
const expectedMessage = "Default Message";
testToastrErrorMessages(errorResponse, expectedMessage);
Expand Down Expand Up @@ -374,4 +374,10 @@ describe("Toastr", () => {
const expectedMessage = "This is a Error toastr.";
testToastrErrorMessages(errorResponse, expectedMessage);
});

it("should return toastId when toastr is called", () => {
const successMessage = "This is a success toastr.";
const toastId = Toastr.success(successMessage);
expect(toastId).toBeDefined();
});
});
2 changes: 1 addition & 1 deletion types/Toastr.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const Toastr: {
message: React.ReactNode | Error,
buttonLabel?: React.ReactNode,
onClick?: () => void
) => void;
) => string | null;
warning: ToastrFunction;
};
export default Toastr;
Loading