|
| 1 | +import { renderHook } from "@testing-library/react"; |
| 2 | +import { useEffect } from "react"; |
| 3 | + |
| 4 | +import { useBetterEffect } from "./use-better-effect"; |
| 5 | + |
| 6 | +describe("useBetterEffect", () => { |
| 7 | + describe('runCondition: "ON_MOUNT"', () => { |
| 8 | + it("should run callbackFn on mount", () => { |
| 9 | + const callbackFn = jest.fn(); |
| 10 | + renderHook(() => |
| 11 | + useBetterEffect({ callbackFn, runCondition: "ON_MOUNT" }) |
| 12 | + ); |
| 13 | + expect(callbackFn).toHaveBeenCalledTimes(1); |
| 14 | + }); |
| 15 | + |
| 16 | + it("should not run callbackFn on rerenders", () => { |
| 17 | + const callbackFn = jest.fn(); |
| 18 | + const { rerender } = renderHook(() => { |
| 19 | + useBetterEffect({ |
| 20 | + callbackFn, |
| 21 | + runCondition: "ON_MOUNT", |
| 22 | + }); |
| 23 | + }); |
| 24 | + expect(callbackFn).toHaveBeenCalledTimes(1); |
| 25 | + rerender(); |
| 26 | + expect(callbackFn).toHaveBeenCalledTimes(1); |
| 27 | + }); |
| 28 | + |
| 29 | + it("should run cleanupFn on unmount", () => { |
| 30 | + const cleanupFn = jest.fn(); |
| 31 | + const { unmount } = renderHook(() => |
| 32 | + useBetterEffect({ |
| 33 | + callbackFn: () => {}, |
| 34 | + cleanupFn, |
| 35 | + runCondition: "ON_MOUNT", |
| 36 | + }) |
| 37 | + ); |
| 38 | + expect(cleanupFn).toHaveBeenCalledTimes(0); |
| 39 | + unmount(); |
| 40 | + expect(cleanupFn).toHaveBeenCalledTimes(1); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('runCondition: "EVERY_RENDER"', () => { |
| 45 | + it("should run callbackFn on mount", () => { |
| 46 | + const callbackFn = jest.fn(); |
| 47 | + renderHook(() => |
| 48 | + useBetterEffect({ callbackFn, runCondition: "EVERY_RENDER" }) |
| 49 | + ); |
| 50 | + expect(callbackFn).toHaveBeenCalledTimes(1); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should run callbackFn on every render", () => { |
| 54 | + const callbackFn = jest.fn(); |
| 55 | + const { rerender } = renderHook(() => |
| 56 | + useBetterEffect({ callbackFn, runCondition: "EVERY_RENDER" }) |
| 57 | + ); |
| 58 | + expect(callbackFn).toHaveBeenCalledTimes(1); |
| 59 | + rerender(); |
| 60 | + expect(callbackFn).toHaveBeenCalledTimes(2); |
| 61 | + rerender(); |
| 62 | + expect(callbackFn).toHaveBeenCalledTimes(3); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should run cleanupFn on unmount", () => { |
| 66 | + const cleanupFn = jest.fn(); |
| 67 | + const { unmount } = renderHook(() => |
| 68 | + useBetterEffect({ |
| 69 | + callbackFn: () => {}, |
| 70 | + cleanupFn, |
| 71 | + runCondition: "EVERY_RENDER", |
| 72 | + }) |
| 73 | + ); |
| 74 | + expect(cleanupFn).toHaveBeenCalledTimes(0); |
| 75 | + unmount(); |
| 76 | + expect(cleanupFn).toHaveBeenCalledTimes(1); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe('runCondition: "DEPENDENCIES_CHANGED"', () => { |
| 81 | + it("should run callbackFn on mount", () => { |
| 82 | + const callbackFn = jest.fn(); |
| 83 | + renderHook(() => |
| 84 | + useBetterEffect({ |
| 85 | + callbackFn, |
| 86 | + runCondition: "DEPENDENCIES_CHANGED", |
| 87 | + dependencies: [1, 2, 3], |
| 88 | + }) |
| 89 | + ); |
| 90 | + expect(callbackFn).toHaveBeenCalledTimes(1); |
| 91 | + }); |
| 92 | + |
| 93 | + it.only("should run callbackFn on dependencies changed", () => { |
| 94 | + const callbackFn = jest.fn(); |
| 95 | + const { rerender } = renderHook( |
| 96 | + ({ dependencies }) => useEffect(callbackFn, dependencies), |
| 97 | + { initialProps: { dependencies: [1, 2, 3] } } |
| 98 | + ); |
| 99 | + expect(callbackFn).toHaveBeenCalledTimes(1); |
| 100 | + rerender({ dependencies: [1, 2, 3, 4] }); |
| 101 | + expect(callbackFn).toHaveBeenCalledTimes(2); |
| 102 | + }); |
| 103 | + |
| 104 | + it("should not run cleanupFn on dependencies changed", () => { |
| 105 | + const cleanupFn = jest.fn(); |
| 106 | + const { rerender } = renderHook( |
| 107 | + ({ dependencies }) => |
| 108 | + useBetterEffect({ |
| 109 | + callbackFn: () => {}, |
| 110 | + cleanupFn, |
| 111 | + runCondition: "DEPENDENCIES_CHANGED", |
| 112 | + dependencies, |
| 113 | + }), |
| 114 | + { initialProps: { dependencies: [1, 2, 3] } } |
| 115 | + ); |
| 116 | + expect(cleanupFn).toHaveBeenCalledTimes(0); |
| 117 | + rerender({ dependencies: [1, 2, 3, 4] }); |
| 118 | + expect(cleanupFn).toHaveBeenCalledTimes(0); |
| 119 | + }); |
| 120 | + |
| 121 | + it("should run cleanupFn on unmount", () => { |
| 122 | + const cleanupFn = jest.fn(); |
| 123 | + const { unmount } = renderHook(() => |
| 124 | + useBetterEffect({ |
| 125 | + callbackFn: () => {}, |
| 126 | + cleanupFn, |
| 127 | + runCondition: "DEPENDENCIES_CHANGED", |
| 128 | + dependencies: [1, 2, 3], |
| 129 | + }) |
| 130 | + ); |
| 131 | + expect(cleanupFn).toHaveBeenCalledTimes(0); |
| 132 | + unmount(); |
| 133 | + expect(cleanupFn).toHaveBeenCalledTimes(1); |
| 134 | + }); |
| 135 | + }); |
| 136 | +}); |
0 commit comments