Can you write unit-test based on React Testing Library for this function (written by…
- User
Can you write unit-test based on React Testing Library for this function (written by Typescript): Напиши unit-тест следующей функции с использованием React Testing Library: import { useCallback } from "react";
type ScrollLockType = { lockScroll: () => void; unlockScroll: () => void; };
export const useScrollLock = (): ScrollLockType => { const lockScroll = useCallback((): void => { const scrollBarCompensation = window.innerWidth - document.body.offsetWidth; document.body.style.overflow = "hidden"; document.body.style.paddingRight =
${scrollBarCompensation}px; }, []);const unlockScroll = useCallback((): void => { document.body.style.overflow = ""; document.body.style.paddingRight = ""; }, []);
return { lockScroll, unlockScroll, }; };
- Assistant
Here's an example of how you can write unit tests for the
useScrollLockfunction using React Testing Library:import { renderHook } from "@testing-library/react-hooks"; import { act } from "react-dom/test-utils"; import { useScrollLock, ScrollLockType } from "./your-file-path"; describe("useScrollLock", () => { let scrollLock: ScrollLockType; beforeEach(() => { scrollLock = renderHook(() => useScrollLock()).result.current; }); it("should lock scroll", () => { act(() => { scrollLock.lockScroll(); }); expect(document.body.style.overflow).toBe("hidden"); }); it("should unlock scroll", () => { // Lock scroll act(() => { scrollLock.lockScroll(); }); // Unlock scroll act(() => { scrollLock.unlockScroll(); }); expect(document.body.style.overflow).toBe(""); }); });Make sure to replace
"./your-file-path"with the actual path to the file where theuseScrollLockfunction is defined. Also, make sure you have the necessary imports (renderHook,act,useScrollLock, andScrollLockType) for the test to work properly.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.