@crossed/primitive
Version:
A universal & performant styling library for React Native, Next.js & React
67 lines (66 loc) • 2.23 kB
JavaScript
import { useEvent, useGet } from "../useEvent";
import { useCallback, useEffect, useLayoutEffect, useRef } from "react";
jest.mock("react");
const useCallbackMocked = useCallback;
const useEffectMocked = useEffect;
const useLayoutEffectMocked = useLayoutEffect;
const useRefMocked = useRef;
describe("useEvent", () => {
beforeEach(() => {
useCallbackMocked.mockImplementation((e) => e);
useEffectMocked.mockImplementation((e) => e());
useLayoutEffectMocked.mockImplementation((e) => e());
useRefMocked.mockImplementation(() => ({ current: void 0 }));
});
afterEach(() => {
useCallbackMocked.mockReset();
useEffectMocked.mockReset();
useLayoutEffectMocked.mockReset();
useRefMocked.mockReset();
});
test("with callback", () => {
const callback = jest.fn();
const event = useEvent(callback);
expect(useRefMocked).toBeCalled();
expect(useLayoutEffectMocked).toBeCalled();
expect(useCallbackMocked).toBeCalled();
expect(typeof event).toBe("function");
event();
expect(callback).toBeCalled();
});
test("without callback", () => {
const event = useEvent();
expect(useRefMocked).toBeCalled();
expect(useLayoutEffectMocked).toBeCalled();
expect(useCallbackMocked).toBeCalled();
expect(typeof event).toBe("function");
event();
});
});
describe("useGet", () => {
beforeEach(() => {
useCallbackMocked.mockImplementation((e) => e);
useEffectMocked.mockImplementation((e) => e());
useLayoutEffectMocked.mockImplementation((e) => e());
useRefMocked.mockImplementation(() => ({ current: void 0 }));
});
afterEach(() => {
useCallbackMocked.mockReset();
useEffectMocked.mockReset();
useLayoutEffectMocked.mockReset();
useRefMocked.mockReset();
});
test("simple", () => {
const callback = jest.fn();
const event = useGet(callback);
expect(useRefMocked).toBeCalled();
expect(useLayoutEffectMocked).toBeCalled();
expect(useCallbackMocked).toBeCalled();
expect(typeof event).toBe("function");
const eventReturn = event();
expect(typeof eventReturn).toBe("function");
eventReturn();
expect(callback).toBeCalled();
});
});
//# sourceMappingURL=useEvent.spec.js.map