This commit is contained in:
Stefan Pejcic
2024-05-08 19:52:27 +02:00
parent 9c8d080b57
commit 80303fadd5
2509 changed files with 0 additions and 594172 deletions

View File

@@ -1,79 +0,0 @@
import React from "react";
import { render, TestWrapper } from "@test";
import { UndoableQueue } from "./index";
import { UndoableQueueContext } from "@contexts/undoableQueue";
const doMutation = jest.fn();
const cancelMutation = jest.fn();
const openMock = jest.fn();
const closeMock = jest.fn();
const notificationDispatch = jest.fn();
const mockNotification = {
id: "1",
resource: "posts",
cancelMutation,
doMutation,
seconds: 5000,
isRunning: true,
isSilent: false,
};
describe("Cancel Notification", () => {
it("should trigger notification open function", async () => {
jest.useFakeTimers();
render(
<UndoableQueueContext.Provider
value={{
notificationDispatch,
notifications: [mockNotification],
}}
>
<UndoableQueue notification={mockNotification} />
</UndoableQueueContext.Provider>,
{
wrapper: TestWrapper({
notificationProvider: {
open: openMock,
close: closeMock,
},
}),
},
);
expect(openMock).toBeCalledTimes(1);
expect(openMock).toBeCalledWith({
cancelMutation: cancelMutation,
key: "1-posts-notification",
message: "You have 5 seconds to undo",
type: "progress",
undoableTimeout: 5,
});
jest.runAllTimers();
expect(notificationDispatch).toBeCalledTimes(1);
expect(notificationDispatch).toBeCalledWith({
payload: {
id: "1",
resource: "posts",
seconds: 5000,
},
type: "DECREASE_NOTIFICATION_SECOND",
});
jest.clearAllTimers();
jest.useRealTimers();
});
it("should call doMutation on seconds zero", async () => {
mockNotification.seconds = 0;
render(<UndoableQueue notification={mockNotification} />);
expect(doMutation).toBeCalledTimes(1);
});
});

View File

@@ -1,68 +0,0 @@
import React, { useEffect, useState } from "react";
import { ActionTypes } from "@contexts/undoableQueue";
import { useCancelNotification, useNotification, useTranslate } from "@hooks";
import { IUndoableQueue } from "../../interfaces";
import { userFriendlySecond } from "@definitions/helpers";
export const UndoableQueue: React.FC<{
notification: IUndoableQueue;
}> = ({ notification }) => {
const translate = useTranslate();
const { notificationDispatch } = useCancelNotification();
const { open } = useNotification();
const [timeoutId, setTimeoutId] = useState<number | undefined>();
const cancelNotification = () => {
if (notification.isRunning === true) {
if (notification.seconds === 0) {
notification.doMutation();
}
if (!notification.isSilent) {
open?.({
key: `${notification.id}-${notification.resource}-notification`,
type: "progress",
message: translate(
"notifications.undoable",
{
seconds: userFriendlySecond(notification.seconds),
},
`You have ${userFriendlySecond(
notification.seconds,
)} seconds to undo`,
),
cancelMutation: notification.cancelMutation,
undoableTimeout: userFriendlySecond(notification.seconds),
});
}
if (notification.seconds > 0) {
if (timeoutId) {
clearTimeout(timeoutId);
}
const newTimeoutId = setTimeout(() => {
notificationDispatch({
type: ActionTypes.DECREASE_NOTIFICATION_SECOND,
payload: {
id: notification.id,
seconds: notification.seconds,
resource: notification.resource,
},
});
}, 1000) as unknown as number;
setTimeoutId(newTimeoutId);
}
}
};
useEffect(() => {
cancelNotification();
}, [notification]);
return null;
};