-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into allow-skipping-changeset
- Loading branch information
Showing
6 changed files
with
269 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"saleor-dashboard": patch | ||
--- | ||
|
||
App delivery events are now fetched at a 5 minute interval. This means this heavy query is now used sparingly. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
import useLocalStorage from "@dashboard/hooks/useLocalStorage"; | ||
import { renderHook } from "@testing-library/react-hooks"; | ||
import { useState } from "react"; | ||
import { act } from "react-dom/test-utils"; | ||
|
||
import { useIntervalActionWithState } from "./useIntervalActionWithState"; | ||
|
||
jest.mock("@dashboard/hooks/useLocalStorage", () => ({ | ||
__esModule: true, | ||
default: jest.fn(() => { | ||
const [value, setValue] = useState(0); | ||
|
||
return [value, setValue]; | ||
}), | ||
})); | ||
|
||
const TEST_KEY = "test-key"; | ||
|
||
describe("useIntervalActionWithState", () => { | ||
beforeEach(() => { | ||
jest.useFakeTimers(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
it("should execute action immediately if interval has passed", () => { | ||
// Arrange | ||
const action = jest.fn(); | ||
|
||
// Act | ||
renderHook(() => | ||
useIntervalActionWithState({ | ||
action, | ||
interval: 1000, | ||
key: TEST_KEY, | ||
}), | ||
); | ||
|
||
// Assert | ||
expect(action).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("should execute action after interval", () => { | ||
// Arrange | ||
const action = jest.fn(); | ||
|
||
// Act | ||
renderHook(() => | ||
useIntervalActionWithState({ | ||
action, | ||
interval: 1000, | ||
key: TEST_KEY, | ||
}), | ||
); | ||
|
||
act(() => { | ||
jest.advanceTimersByTime(1000); | ||
}); | ||
|
||
// Assert | ||
expect(action).toHaveBeenCalledTimes(2); // Once on mount, once after interval | ||
}); | ||
|
||
it("should clear timeout on unmount", () => { | ||
// Arrange | ||
const action = jest.fn(); | ||
|
||
// Act | ||
const { unmount } = renderHook(() => | ||
useIntervalActionWithState({ | ||
action, | ||
interval: 1000, | ||
key: TEST_KEY, | ||
}), | ||
); | ||
|
||
unmount(); | ||
|
||
act(() => { | ||
jest.advanceTimersByTime(1000); | ||
}); | ||
|
||
// Assert | ||
expect(action).toHaveBeenCalledTimes(1); // Only initial call | ||
}); | ||
|
||
it("should handle multiple intervals", () => { | ||
// Arrange | ||
const action = jest.fn(); | ||
|
||
// Act | ||
renderHook(() => | ||
useIntervalActionWithState({ | ||
action, | ||
interval: 1000, | ||
key: TEST_KEY, | ||
}), | ||
); | ||
|
||
act(() => { | ||
jest.advanceTimersByTime(2500); | ||
}); | ||
|
||
// Assert | ||
expect(action).toHaveBeenCalledTimes(3); // Initial + 2 intervals | ||
}); | ||
|
||
it("should handle component rerenders", () => { | ||
// Arrange | ||
const action = jest.fn(); | ||
|
||
// Act | ||
const { rerender } = renderHook(() => | ||
useIntervalActionWithState({ | ||
action, | ||
interval: 1000, | ||
key: TEST_KEY, | ||
}), | ||
); | ||
|
||
rerender(); | ||
|
||
act(() => { | ||
jest.advanceTimersByTime(1000); | ||
}); | ||
|
||
// Assert | ||
expect(action).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it("should calculate correct delay based on last invocation", () => { | ||
// Arrange | ||
const action = jest.fn(); | ||
const mockTime = new Date().getTime(); | ||
|
||
(useLocalStorage as jest.Mock).mockReturnValue([mockTime - 500, jest.fn()]); | ||
|
||
// Act | ||
renderHook(() => | ||
useIntervalActionWithState({ | ||
action, | ||
interval: 1000, | ||
key: TEST_KEY, | ||
}), | ||
); | ||
|
||
expect(action).not.toHaveBeenCalled(); | ||
|
||
act(() => { | ||
jest.advanceTimersByTime(500); | ||
}); | ||
|
||
// Assert | ||
expect(action).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("should skip execution if skip is true", () => { | ||
// Arrange | ||
const action = jest.fn(); | ||
|
||
// Act | ||
renderHook(() => | ||
useIntervalActionWithState({ | ||
action, | ||
interval: 1000, | ||
key: TEST_KEY, | ||
skip: true, | ||
}), | ||
); | ||
|
||
act(() => { | ||
jest.advanceTimersByTime(1000); | ||
}); | ||
|
||
// Assert | ||
expect(action).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import useLocalStorage from "@dashboard/hooks/useLocalStorage"; | ||
import { useEffect, useRef } from "react"; | ||
|
||
type Timeout = ReturnType<typeof setTimeout>; | ||
|
||
interface UseIntervalActionWithState { | ||
action: () => void; | ||
key: string; | ||
interval?: number; | ||
skip?: boolean; | ||
} | ||
|
||
export const useIntervalActionWithState = ({ | ||
action, | ||
key, | ||
interval = 5_000, | ||
skip = false, | ||
}: UseIntervalActionWithState) => { | ||
const savedAction = useRef(action); | ||
const timeout = useRef<Timeout | null>(null); | ||
const [lastInvocation, setLastInvocation] = useLocalStorage(key, new Date().getTime()); | ||
|
||
useEffect(() => { | ||
const cleanup = () => { | ||
if (timeout.current) { | ||
clearTimeout(timeout.current); | ||
} | ||
}; | ||
|
||
if (skip) { | ||
return cleanup; | ||
} | ||
|
||
const timeNow = new Date().getTime(); | ||
const timePassed = timeNow - lastInvocation; | ||
const nextDelay = Math.max(0, interval - timePassed); | ||
|
||
const hasPassedInterval = timePassed >= interval; | ||
|
||
const action = () => { | ||
savedAction.current(); | ||
setLastInvocation(new Date().getTime()); | ||
}; | ||
|
||
if (hasPassedInterval) { | ||
action(); | ||
timeout.current = setTimeout(() => action(), interval); | ||
} else { | ||
timeout.current = setTimeout(() => action(), nextDelay); | ||
} | ||
|
||
return cleanup; | ||
}, [lastInvocation, interval, key, setLastInvocation, skip]); | ||
}; |