-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
514 additions
and
2 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,77 @@ | ||
import { act, renderHook } from "@testing-library/react-hooks"; | ||
import useElementIsVisible from "../useElementIsVisible"; | ||
|
||
describe("useElementIsVisible", () => { | ||
let mockObserve: jest.Mock; | ||
let mockDisconnect: jest.Mock; | ||
let mockTriggerIntersect: (entries: IntersectionObserverEntry[]) => void; | ||
|
||
// Mock IntersectionObserver | ||
beforeAll(() => { | ||
mockObserve = jest.fn(); | ||
mockDisconnect = jest.fn(); | ||
|
||
global.IntersectionObserver = jest.fn((callback, opts = {}) => { | ||
mockTriggerIntersect = (entries: IntersectionObserverEntry[]) => | ||
callback(entries, new IntersectionObserver(() => null)); | ||
|
||
return { | ||
root: opts.root ?? null, | ||
rootMargin: opts.rootMargin ?? "", | ||
observe: mockObserve, | ||
disconnect: mockDisconnect, | ||
unobserve: jest.fn(), | ||
thresholds: [], | ||
takeRecords: jest.fn(), | ||
}; | ||
}); | ||
|
||
jest | ||
.spyOn(document, "querySelector") | ||
.mockImplementation((sel: string) => | ||
sel === "#notFound" ? null : document.createElement("div"), | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("initializes with isIntersecting as false", () => { | ||
const { result } = renderHook(() => useElementIsVisible("#testElement")); | ||
expect(result.current).toBe(false); | ||
}); | ||
|
||
it("does nothing if ID not found", () => { | ||
const { result, unmount } = renderHook(() => | ||
useElementIsVisible("#notFound"), | ||
); | ||
expect(result.current).toBe(false); | ||
expect(mockObserve).not.toHaveBeenCalled(); | ||
|
||
unmount(); | ||
|
||
expect(mockDisconnect).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("updates isIntersecting when element becomes visible", () => { | ||
const { result } = renderHook(() => useElementIsVisible("#testElement")); | ||
|
||
act(() => { | ||
mockTriggerIntersect([ | ||
{ isIntersecting: true } as IntersectionObserverEntry, | ||
]); | ||
}); | ||
|
||
expect(result.current).toBe(true); | ||
expect(mockObserve).toHaveBeenCalled(); | ||
}); | ||
|
||
it("disconnects IntersectionObserver on unmount", () => { | ||
const { unmount } = renderHook(() => useElementIsVisible("#testElement")); | ||
|
||
unmount(); | ||
|
||
expect(mockDisconnect).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,52 @@ | ||
import { act, renderHook } from "@testing-library/react-hooks"; | ||
import useFocus from "../useFocus"; | ||
|
||
describe("useFocus", () => { | ||
// Mocking DOM methods | ||
const mockScrollIntoView = jest.fn(); | ||
const mockScrollTo = jest.fn(); | ||
const mockFocus = jest.fn(); | ||
|
||
beforeAll(() => { | ||
jest.spyOn(document, "getElementById").mockImplementation( | ||
(id: string) => | ||
({ | ||
scrollIntoView: mockScrollIntoView, | ||
scrollTo: mockScrollTo, | ||
focus: mockFocus, | ||
id, | ||
}) as any, // Type casting to any to simplify mock structure | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("initializes with default states", () => { | ||
const { result } = renderHook(() => useFocus()); | ||
expect(result.current.setScrollToTop).toBeDefined(); | ||
expect(result.current.setRefocus).toBeDefined(); | ||
}); | ||
|
||
it("handles scrollToTop", () => { | ||
const { result } = renderHook(() => useFocus()); | ||
|
||
act(() => { | ||
result.current.setScrollToTop(true); | ||
}); | ||
|
||
expect(mockScrollIntoView).toHaveBeenCalled(); | ||
expect(mockScrollTo).toHaveBeenCalledWith({ top: 0, behavior: "smooth" }); | ||
}); | ||
|
||
it("handles refocus", () => { | ||
const { result } = renderHook(() => useFocus()); | ||
|
||
act(() => { | ||
result.current.setRefocus(true); | ||
}); | ||
|
||
expect(mockFocus).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,27 @@ | ||
import { act, renderHook } from "@testing-library/react-hooks"; | ||
import { useHotKeysForToggle } from "../useHotKeys"; | ||
|
||
describe("useHotKeysForToggle", () => { | ||
it("initializes with default keyMap and handlers", () => { | ||
const { result } = renderHook(() => useHotKeysForToggle(jest.fn())); | ||
|
||
expect(result.current.keyMap).toEqual({ | ||
TOGGLE_EDITOR: ["meta+shift+enter", "ctrl+shift+enter"], | ||
}); | ||
expect(result.current.handlers).toBeDefined(); | ||
expect(result.current.handlers.TOGGLE_EDITOR).toBeDefined(); | ||
}); | ||
|
||
it("toggles when handler is invoked", () => { | ||
const mockToggle = jest.fn(); | ||
const { result } = renderHook(() => useHotKeysForToggle(mockToggle)); | ||
|
||
// Simulate the handler being called | ||
act(() => { | ||
result.current.handlers.TOGGLE_EDITOR(); | ||
}); | ||
|
||
// The toggle function should be called | ||
expect(mockToggle).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,30 @@ | ||
import { renderHook } from "@testing-library/react-hooks"; | ||
import cookie from "js-cookie"; | ||
import useIsSignedIn from "../useIsSignedIn"; | ||
|
||
jest.mock("js-cookie"); | ||
|
||
describe("useIsSignedIn", () => { | ||
const tokenKey = "authToken"; | ||
|
||
it("initializes as not signed in", () => { | ||
(cookie.get as jest.Mock).mockReturnValue(null); | ||
const { result } = renderHook(() => useIsSignedIn(tokenKey)); | ||
|
||
expect(result.current).toBe(false); | ||
}); | ||
|
||
it("returns true when token is present", () => { | ||
(cookie.get as jest.Mock).mockReturnValue("tokenValue"); | ||
const { result } = renderHook(() => useIsSignedIn(tokenKey)); | ||
|
||
expect(result.current).toBe(true); | ||
}); | ||
|
||
it("returns false when token is not present", () => { | ||
(cookie.get as jest.Mock).mockReturnValue(null); | ||
const { result } = renderHook(() => useIsSignedIn(tokenKey)); | ||
|
||
expect(result.current).toBe(false); | ||
}); | ||
}); |
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,28 @@ | ||
import { renderHook } from "@testing-library/react-hooks"; | ||
import useOnClickOutside from "../useOnClickOutside"; | ||
|
||
describe("useOnClickOutside", () => { | ||
it("calls action on outside click", () => { | ||
const mockAction = jest.fn(); | ||
const excludingRef = { current: document.createElement("div") }; | ||
|
||
renderHook(() => useOnClickOutside(excludingRef, mockAction)); | ||
|
||
const event = new MouseEvent("mousedown", { bubbles: true }); | ||
document.dispatchEvent(event); | ||
|
||
expect(mockAction).toHaveBeenCalled(); | ||
}); | ||
|
||
it("does not call action on inside click", () => { | ||
const mockAction = jest.fn(); | ||
const excludingRef = { current: document.createElement("div") }; | ||
|
||
renderHook(() => useOnClickOutside(excludingRef, mockAction)); | ||
|
||
const event = new MouseEvent("mousedown", { bubbles: true }); | ||
excludingRef.current.dispatchEvent(event); | ||
|
||
expect(mockAction).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
Oops, something went wrong.