diff --git a/.changeset/kind-insects-end.md b/.changeset/kind-insects-end.md new file mode 100644 index 0000000000..71e7bedb09 --- /dev/null +++ b/.changeset/kind-insects-end.md @@ -0,0 +1,5 @@ +--- +"@nextui-org/use-image": patch +--- + +server validation added to the use-image hook diff --git a/packages/hooks/use-image/__tests__/use-image.test.tsx b/packages/hooks/use-image/__tests__/use-image.test.tsx index 7f76d4a7cd..c2e5860c64 100644 --- a/packages/hooks/use-image/__tests__/use-image.test.tsx +++ b/packages/hooks/use-image/__tests__/use-image.test.tsx @@ -1,4 +1,4 @@ -import {renderHook} from "@testing-library/react-hooks"; +import {renderHook, waitFor} from "@testing-library/react"; import {mocks} from "@nextui-org/test-utils"; import {useImage} from "../src"; @@ -14,31 +14,31 @@ describe("use-image hook", () => { }); it("can handle missing src", () => { - const rendered = renderHook(() => useImage({})); + const {result} = renderHook(() => useImage({})); - expect(rendered.result.current).toEqual("pending"); + expect(result.current).toEqual("pending"); }); it("can handle loading image", async () => { - const rendered = renderHook(() => useImage({src: "/test.png"})); + const {result} = renderHook(() => useImage({src: "/test.png"})); - expect(rendered.result.current).toEqual("loading"); + expect(result.current).toEqual("loading"); mockImage.simulate("loaded"); - await rendered.waitForValueToChange(() => rendered.result.current === "loaded"); + await waitFor(() => expect(result.current).toBe("loaded")); }); it("can handle error image", async () => { mockImage.simulate("error"); - const rendered = renderHook(() => useImage({src: "/test.png"})); + const {result} = renderHook(() => useImage({src: "/test.png"})); - expect(rendered.result.current).toEqual("loading"); - await rendered.waitForValueToChange(() => rendered.result.current === "failed"); + expect(result.current).toEqual("loading"); + await waitFor(() => expect(result.current).toBe("failed")); }); it("can handle cached image", async () => { mockImage.simulate("loaded"); - const rendered = renderHook(() => useImage({src: "/test.png"})); + const {result} = renderHook(() => useImage({src: "/test.png"})); - expect(rendered.result.current).toEqual("loaded"); + expect(result.current).toEqual("loaded"); }); }); diff --git a/packages/hooks/use-image/package.json b/packages/hooks/use-image/package.json index 9c2267f575..f33815dd4b 100644 --- a/packages/hooks/use-image/package.json +++ b/packages/hooks/use-image/package.json @@ -34,8 +34,7 @@ "postpack": "clean-package restore" }, "dependencies": { - "@nextui-org/use-safe-layout-effect": "workspace:*", - "@testing-library/react-hooks": "^8.0.1" + "@nextui-org/use-safe-layout-effect": "workspace:*" }, "peerDependencies": { "react": ">=18" diff --git a/packages/hooks/use-image/src/index.ts b/packages/hooks/use-image/src/index.ts index 513d92100e..2c1aab7f4d 100644 --- a/packages/hooks/use-image/src/index.ts +++ b/packages/hooks/use-image/src/index.ts @@ -122,7 +122,7 @@ function setImageAndGetInitialStatus( if (!src) return "pending"; if (ignoreFallback) return "loaded"; - const img = document.createElement("img"); + const img = typeof window !== "undefined" ? new Image() : document.createElement("img"); img.src = src; if (crossOrigin) img.crossOrigin = crossOrigin; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 25fe5137e6..a237491ea8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3515,13 +3515,13 @@ importers: '@nextui-org/use-safe-layout-effect': specifier: workspace:* version: link:../use-safe-layout-effect - '@testing-library/react-hooks': - specifier: ^8.0.1 - version: 8.0.1(@types/react@18.2.8)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) devDependencies: '@nextui-org/test-utils': specifier: workspace:* version: link:../../utilities/test-utils + '@testing-library/react-hooks': + specifier: ^8.0.1 + version: 8.0.1(@types/react@18.2.8)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) clean-package: specifier: 2.2.0 version: 2.2.0 @@ -18235,15 +18235,13 @@ snapshots: transitivePeerDependencies: - '@parcel/core' - '@parcel/cache@2.12.0(@parcel/core@2.12.0(@swc/helpers@0.5.13))(@swc/helpers@0.5.13)': + '@parcel/cache@2.12.0(@parcel/core@2.12.0(@swc/helpers@0.5.13))': dependencies: '@parcel/core': 2.12.0(@swc/helpers@0.5.13) '@parcel/fs': 2.12.0(@parcel/core@2.12.0(@swc/helpers@0.5.13))(@swc/helpers@0.5.13) '@parcel/logger': 2.12.0 '@parcel/utils': 2.12.0 lmdb: 2.8.5 - transitivePeerDependencies: - - '@swc/helpers' '@parcel/codeframe@2.12.0': dependencies: @@ -18303,7 +18301,7 @@ snapshots: '@parcel/core@2.12.0(@swc/helpers@0.5.13)': dependencies: '@mischnic/json-sourcemap': 0.1.1 - '@parcel/cache': 2.12.0(@parcel/core@2.12.0(@swc/helpers@0.5.13))(@swc/helpers@0.5.13) + '@parcel/cache': 2.12.0(@parcel/core@2.12.0(@swc/helpers@0.5.13)) '@parcel/diagnostic': 2.12.0 '@parcel/events': 2.12.0 '@parcel/fs': 2.12.0(@parcel/core@2.12.0(@swc/helpers@0.5.13))(@swc/helpers@0.5.13) @@ -18718,7 +18716,7 @@ snapshots: '@parcel/types@2.12.0(@parcel/core@2.12.0(@swc/helpers@0.5.13))(@swc/helpers@0.5.13)': dependencies: - '@parcel/cache': 2.12.0(@parcel/core@2.12.0(@swc/helpers@0.5.13))(@swc/helpers@0.5.13) + '@parcel/cache': 2.12.0(@parcel/core@2.12.0(@swc/helpers@0.5.13)) '@parcel/diagnostic': 2.12.0 '@parcel/fs': 2.12.0(@parcel/core@2.12.0(@swc/helpers@0.5.13))(@swc/helpers@0.5.13) '@parcel/package-manager': 2.12.0(@parcel/core@2.12.0(@swc/helpers@0.5.13))(@swc/helpers@0.5.13) @@ -23833,7 +23831,7 @@ snapshots: eslint: 7.32.0 eslint-import-resolver-node: 0.3.9 eslint-import-resolver-typescript: 3.6.3(@typescript-eslint/parser@5.62.0(eslint@7.32.0)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0)(eslint@7.32.0) - eslint-plugin-import: 2.31.0(@typescript-eslint/parser@5.62.0(eslint@7.32.0)(typescript@4.9.5))(eslint-import-resolver-typescript@2.7.1(eslint-plugin-import@2.31.0)(eslint@7.32.0))(eslint@7.32.0) + eslint-plugin-import: 2.31.0(@typescript-eslint/parser@5.62.0(eslint@7.32.0)(typescript@4.9.5))(eslint-import-resolver-typescript@2.7.1)(eslint@7.32.0) eslint-plugin-jsx-a11y: 6.10.2(eslint@7.32.0) eslint-plugin-react: 7.37.2(eslint@7.32.0) eslint-plugin-react-hooks: 4.6.2(eslint@7.32.0) @@ -23903,7 +23901,7 @@ snapshots: is-bun-module: 1.2.1 is-glob: 4.0.3 optionalDependencies: - eslint-plugin-import: 2.31.0(@typescript-eslint/parser@5.62.0(eslint@7.32.0)(typescript@4.9.5))(eslint-import-resolver-typescript@2.7.1(eslint-plugin-import@2.31.0)(eslint@7.32.0))(eslint@7.32.0) + eslint-plugin-import: 2.31.0(@typescript-eslint/parser@5.62.0(eslint@7.32.0)(typescript@4.9.5))(eslint-import-resolver-typescript@2.7.1)(eslint@7.32.0) transitivePeerDependencies: - '@typescript-eslint/parser' - eslint-import-resolver-node @@ -23954,35 +23952,6 @@ snapshots: lodash: 4.17.21 string-natural-compare: 3.0.1 - eslint-plugin-import@2.31.0(@typescript-eslint/parser@5.62.0(eslint@7.32.0)(typescript@4.9.5))(eslint-import-resolver-typescript@2.7.1(eslint-plugin-import@2.31.0)(eslint@7.32.0))(eslint@7.32.0): - dependencies: - '@rtsao/scc': 1.1.0 - array-includes: 3.1.8 - array.prototype.findlastindex: 1.2.5 - array.prototype.flat: 1.3.2 - array.prototype.flatmap: 1.3.2 - debug: 3.2.7 - doctrine: 2.1.0 - eslint: 7.32.0 - eslint-import-resolver-node: 0.3.9 - eslint-module-utils: 2.12.0(@typescript-eslint/parser@5.62.0(eslint@7.32.0)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.3(@typescript-eslint/parser@5.62.0(eslint@7.32.0)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0)(eslint@7.32.0))(eslint@7.32.0) - hasown: 2.0.2 - is-core-module: 2.15.1 - is-glob: 4.0.3 - minimatch: 3.1.2 - object.fromentries: 2.0.8 - object.groupby: 1.0.3 - object.values: 1.2.0 - semver: 6.3.1 - string.prototype.trimend: 1.0.8 - tsconfig-paths: 3.15.0 - optionalDependencies: - '@typescript-eslint/parser': 5.62.0(eslint@7.32.0)(typescript@4.9.5) - transitivePeerDependencies: - - eslint-import-resolver-typescript - - eslint-import-resolver-webpack - - supports-color - eslint-plugin-import@2.31.0(@typescript-eslint/parser@5.62.0(eslint@7.32.0)(typescript@4.9.5))(eslint-import-resolver-typescript@2.7.1)(eslint@7.32.0): dependencies: '@rtsao/scc': 1.1.0