-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(#2332): fix inconsistend display of pending delegation DRep card
- Loading branch information
1 parent
ce48b09
commit 852443c
Showing
5 changed files
with
84 additions
and
9 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { renderHook } from "@testing-library/react"; | ||
import { describe, it, expect } from "vitest"; | ||
import usePrevious from "./usePrevious"; | ||
|
||
describe("usePrevious hook", () => { | ||
it("should return undefined on the initial render", () => { | ||
const { result } = renderHook(() => usePrevious(0)); | ||
|
||
expect(result.current).toBeUndefined(); | ||
}); | ||
|
||
it("should return the previous value after the state changes", () => { | ||
let value = 0; | ||
|
||
const { result, rerender } = renderHook(() => usePrevious(value)); | ||
|
||
expect(result.current).toBeUndefined(); | ||
|
||
value = 10; | ||
rerender(); | ||
|
||
expect(result.current).toBe(0); | ||
|
||
value = 20; | ||
rerender(); | ||
|
||
expect(result.current).toBe(10); | ||
}); | ||
|
||
it("should handle non-primitive values like objects", () => { | ||
let value = { count: 0 }; | ||
|
||
const { result, rerender } = renderHook(() => usePrevious(value)); | ||
|
||
expect(result.current).toBeUndefined(); | ||
|
||
value = { count: 1 }; | ||
rerender(); | ||
|
||
expect(result.current).toEqual({ count: 0 }); | ||
|
||
value = { count: 2 }; | ||
rerender(); | ||
|
||
expect(result.current).toEqual({ count: 1 }); | ||
}); | ||
}); |
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,18 @@ | ||
import { useEffect, useRef } from "react"; | ||
|
||
/** | ||
* Custom React hook to get the previous value of a prop or state. | ||
* @param value The current value to track. | ||
* @returns The previous value of the given input. | ||
*/ | ||
function usePrevious<T>(value: T): T | undefined { | ||
const ref = useRef<T>(); | ||
|
||
useEffect(() => { | ||
ref.current = value; // Update the ref value to the current value after render | ||
}, [value]); // Only run if the value changes | ||
|
||
return ref.current; // Return the previous value (ref.current holds the old value) | ||
} | ||
|
||
export default usePrevious; |
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