-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix(use-image): cached image flashing * chore: merged with canary --------- Co-authored-by: Rakha Kanz Kautsar <rkkautsar@gmail.com>
- Loading branch information
Showing
6 changed files
with
282 additions
and
245 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,6 @@ | ||
--- | ||
"@nextui-org/use-image": patch | ||
"@nextui-org/test-utils": patch | ||
--- | ||
|
||
fix cached image flashing due to use-image always returning pending initially. The fix was to check if the image is loaded instantly through HTMLImageElement.complete attribute and use that to initialize the state. |
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,44 @@ | ||
import {renderHook} from "@testing-library/react-hooks"; | ||
import {mocks} from "@nextui-org/test-utils"; | ||
|
||
import {useImage} from "../src"; | ||
|
||
describe("use-image hook", () => { | ||
let mockImage: {restore: any; simulate: (value: "loaded" | "error") => void}; | ||
|
||
beforeEach(() => { | ||
mockImage = mocks.image(); | ||
}); | ||
afterEach(() => { | ||
mockImage.restore(); | ||
}); | ||
|
||
it("can handle missing src", () => { | ||
const rendered = renderHook(() => useImage({})); | ||
|
||
expect(rendered.result.current).toEqual("pending"); | ||
}); | ||
|
||
it("can handle loading image", async () => { | ||
const rendered = renderHook(() => useImage({src: "/test.png"})); | ||
|
||
expect(rendered.result.current).toEqual("loading"); | ||
mockImage.simulate("loaded"); | ||
await rendered.waitForValueToChange(() => rendered.result.current === "loaded"); | ||
}); | ||
|
||
it("can handle error image", async () => { | ||
mockImage.simulate("error"); | ||
const rendered = renderHook(() => useImage({src: "/test.png"})); | ||
|
||
expect(rendered.result.current).toEqual("loading"); | ||
await rendered.waitForValueToChange(() => rendered.result.current === "failed"); | ||
}); | ||
|
||
it("can handle cached image", async () => { | ||
mockImage.simulate("loaded"); | ||
const rendered = renderHook(() => useImage({src: "/test.png"})); | ||
|
||
expect(rendered.result.current).toEqual("loaded"); | ||
}); | ||
}); |
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
Oops, something went wrong.