|
| 1 | +import { act } from '@testing-library/react'; |
| 2 | +import { describe, expect, it, vi } from 'vitest'; |
| 3 | + |
| 4 | +import { renderHookSSR } from '../../_internal/test-utils/renderHookSSR.tsx'; |
| 5 | + |
| 6 | +import { useConditionalEffect } from './useConditionalEffect.ts'; |
| 7 | + |
| 8 | +describe('useConditionalEffect', () => { |
| 9 | + it('is safe on server side rendering', () => { |
| 10 | + const effect = vi.fn(); |
| 11 | + const condition = vi.fn(() => false); |
| 12 | + |
| 13 | + renderHookSSR.serverOnly(() => useConditionalEffect(effect, [1], condition)); |
| 14 | + |
| 15 | + expect(condition).toHaveBeenCalled(); |
| 16 | + expect(effect).not.toHaveBeenCalled(); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should not run effect when condition returns false', async () => { |
| 20 | + const effect = vi.fn(); |
| 21 | + const condition = vi.fn(() => false); |
| 22 | + |
| 23 | + await renderHookSSR(() => useConditionalEffect(effect, [1], condition)); |
| 24 | + |
| 25 | + expect(condition).toHaveBeenCalledWith(undefined, [1]); |
| 26 | + expect(effect).not.toHaveBeenCalled(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should run effect when condition returns true', async () => { |
| 30 | + const effect = vi.fn(); |
| 31 | + const condition = vi.fn(() => true); |
| 32 | + |
| 33 | + await renderHookSSR(() => useConditionalEffect(effect, [1], condition)); |
| 34 | + |
| 35 | + expect(condition).toHaveBeenCalledWith(undefined, [1]); |
| 36 | + expect(effect).toHaveBeenCalled(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should warn when an empty dependency array is provided', async () => { |
| 40 | + const originalWarn = console.warn; |
| 41 | + console.warn = vi.fn(); |
| 42 | + |
| 43 | + const effect = vi.fn(); |
| 44 | + const condition = vi.fn(() => true); |
| 45 | + |
| 46 | + await renderHookSSR(() => useConditionalEffect(effect, [], condition)); |
| 47 | + |
| 48 | + expect(console.warn).toHaveBeenCalledWith( |
| 49 | + 'useConditionalEffect received an empty dependency array. ' + |
| 50 | + 'This may indicate missing dependencies and could lead to unexpected behavior.' |
| 51 | + ); |
| 52 | + |
| 53 | + console.warn = originalWarn; |
| 54 | + }); |
| 55 | + |
| 56 | + it('should run effect multiple times when condition is repeatedly true', async () => { |
| 57 | + const effect = vi.fn(); |
| 58 | + const condition = vi.fn(() => true); |
| 59 | + |
| 60 | + const { rerender } = await renderHookSSR(({ deps }) => useConditionalEffect(effect, deps, condition), { |
| 61 | + initialProps: { deps: [1] }, |
| 62 | + }); |
| 63 | + |
| 64 | + expect(effect).toHaveBeenCalledTimes(1); |
| 65 | + |
| 66 | + effect.mockClear(); |
| 67 | + await act(async () => { |
| 68 | + rerender({ deps: [2] }); |
| 69 | + }); |
| 70 | + |
| 71 | + expect(condition).toHaveBeenCalledWith([1], [2]); |
| 72 | + expect(effect).toHaveBeenCalledTimes(1); |
| 73 | + |
| 74 | + effect.mockClear(); |
| 75 | + await act(async () => { |
| 76 | + rerender({ deps: [3] }); |
| 77 | + }); |
| 78 | + |
| 79 | + expect(condition).toHaveBeenCalledWith([2], [3]); |
| 80 | + expect(effect).toHaveBeenCalledTimes(1); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should run cleanup function when effect returns one', async () => { |
| 84 | + const cleanup = vi.fn(); |
| 85 | + const effect = vi.fn(() => cleanup); |
| 86 | + const condition = vi.fn(() => true); |
| 87 | + |
| 88 | + const { unmount } = await renderHookSSR(() => useConditionalEffect(effect, [1], condition)); |
| 89 | + |
| 90 | + unmount(); |
| 91 | + |
| 92 | + expect(cleanup).toHaveBeenCalled(); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should store deps for next comparison', async () => { |
| 96 | + const effect = vi.fn(); |
| 97 | + const condition = vi.fn(() => false); |
| 98 | + |
| 99 | + const { rerender } = await renderHookSSR(({ deps }) => useConditionalEffect(effect, deps, condition), { |
| 100 | + initialProps: { deps: [1] }, |
| 101 | + }); |
| 102 | + |
| 103 | + expect(condition).toHaveBeenCalledWith(undefined, [1]); |
| 104 | + |
| 105 | + condition.mockClear(); |
| 106 | + |
| 107 | + await act(async () => { |
| 108 | + rerender({ deps: [2] }); |
| 109 | + }); |
| 110 | + |
| 111 | + expect(condition).toHaveBeenCalledWith([1], [2]); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should run effect based on conditional logic', async () => { |
| 115 | + const effect = vi.fn(); |
| 116 | + |
| 117 | + const condition = vi.fn((prev: readonly number[] | undefined, current: readonly number[]) => { |
| 118 | + if (prev === undefined) return false; |
| 119 | + return current[0] > prev[0]; |
| 120 | + }); |
| 121 | + |
| 122 | + const { rerender } = await renderHookSSR(({ count }) => useConditionalEffect(effect, [count], condition), { |
| 123 | + initialProps: { count: 0 }, |
| 124 | + }); |
| 125 | + |
| 126 | + expect(effect).not.toHaveBeenCalled(); |
| 127 | + |
| 128 | + effect.mockClear(); |
| 129 | + condition.mockClear(); |
| 130 | + |
| 131 | + await act(async () => { |
| 132 | + rerender({ count: 1 }); |
| 133 | + }); |
| 134 | + |
| 135 | + expect(condition).toHaveBeenCalledWith([0], [1]); |
| 136 | + expect(effect).toHaveBeenCalled(); |
| 137 | + |
| 138 | + effect.mockClear(); |
| 139 | + condition.mockClear(); |
| 140 | + |
| 141 | + await act(async () => { |
| 142 | + rerender({ count: 0 }); |
| 143 | + }); |
| 144 | + |
| 145 | + expect(condition).toHaveBeenCalledWith([1], [0]); |
| 146 | + expect(effect).not.toHaveBeenCalled(); |
| 147 | + }); |
| 148 | +}); |
0 commit comments