Skip to content

Commit

Permalink
revert: "fix: Remove console.warn in SSR (#1030)" (#1136)
Browse files Browse the repository at this point in the history
This reverts commit 84d1828.
  • Loading branch information
imbhargav5 authored Jul 2, 2022
1 parent 8f69ce7 commit b5c723a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
12 changes: 12 additions & 0 deletions src/__tests__/generalSsrTests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { useFullscreen } from "../hooks/useFullscreen";
import { useIntervalWhen } from "../hooks/useIntervalWhen";
import { useLocalstorage } from "../hooks/useLocalstorage";
import { useLocalstorageState } from "../hooks/useLocalstorageState";
import { useOnWindowResize } from "../hooks/useOnWindowResize";
import { useOnWindowScroll } from "../hooks/useOnWindowScroll";
import { useOnline } from "../hooks/useOnline";
import { useSessionstorage } from "../hooks/useSessionstorage";
import { useThrottle } from "../hooks/useThrottle";
Expand All @@ -21,6 +23,16 @@ describe("when window is undefined", () => {
consoleSpy.mockClear();
});

it("useOnWindowResize logs warning", () => {
renderHook(() => useOnWindowResize(mockCallback));
expect(consoleSpy).toHaveBeenCalledTimes(1);
});

it("useOnWindowScroll logs warning", () => {
renderHook(() => useOnWindowScroll(mockCallback));
expect(consoleSpy).toHaveBeenCalledTimes(1);
});

it("useIntervalWhen logs warning", () => {
renderHook(() => useIntervalWhen(mockCallback));
expect(consoleSpy).toHaveBeenCalledTimes(1);
Expand Down
22 changes: 14 additions & 8 deletions src/hooks/useOnWindowResize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@ function useOnWindowResize(
when: boolean = true,
isLayoutEffect: boolean = false
) {
useGlobalObjectEventListener(
window,
"resize",
callback,
{ passive: true },
when,
isLayoutEffect
);
if (typeof window !== "undefined") {
useGlobalObjectEventListener(
window,
"resize",
callback,
{ passive: true },
when,
isLayoutEffect
);
} else {
console.warn(
"useOnWindowResize can't attach an event listener as window is undefined."
);
}
}

export { useOnWindowResize };
22 changes: 14 additions & 8 deletions src/hooks/useOnWindowScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ function useOnWindowScroll(
when: boolean = true,
isLayoutEffect: boolean = false
): void {
useGlobalObjectEventListener(
window,
"scroll",
callback,
{ passive: true },
when,
isLayoutEffect
);
if (typeof window !== "undefined") {
useGlobalObjectEventListener(
window,
"scroll",
callback,
{ passive: true },
when,
isLayoutEffect
);
} else {
console.warn(
"useOnWindowScroll can't attach an event listener as window is undefined."
);
}
}

export { useOnWindowScroll };

0 comments on commit b5c723a

Please sign in to comment.