|
1 | 1 | import { useEffect } from 'react' |
2 | | -import { renderHook, cleanup } from 'src' |
| 2 | +import { renderHook, cleanup, addCleanup, removeCleanup } from 'src/pure' |
3 | 3 |
|
4 | 4 | describe('cleanup tests', () => { |
5 | 5 | test('should flush effects on cleanup', async () => { |
@@ -38,4 +38,98 @@ describe('cleanup tests', () => { |
38 | 38 | expect(cleanupCalled[1]).toBe(true) |
39 | 39 | expect(cleanupCalled[2]).toBe(true) |
40 | 40 | }) |
| 41 | + |
| 42 | + test('should call cleanups in reverse order', async () => { |
| 43 | + let callSequence = [] |
| 44 | + addCleanup(() => { |
| 45 | + callSequence.push('cleanup') |
| 46 | + }) |
| 47 | + addCleanup(() => { |
| 48 | + callSequence.push('another cleanup') |
| 49 | + }) |
| 50 | + const hookWithCleanup = () => { |
| 51 | + useEffect(() => { |
| 52 | + return () => { |
| 53 | + callSequence.push('unmount') |
| 54 | + } |
| 55 | + }) |
| 56 | + } |
| 57 | + renderHook(() => hookWithCleanup()) |
| 58 | + |
| 59 | + await cleanup() |
| 60 | + |
| 61 | + expect(callSequence).toEqual(['unmount', 'another cleanup', 'cleanup']) |
| 62 | + }) |
| 63 | + |
| 64 | + test('should wait for async cleanup', async () => { |
| 65 | + let callSequence = [] |
| 66 | + addCleanup(() => { |
| 67 | + callSequence.push('cleanup') |
| 68 | + }) |
| 69 | + addCleanup(async () => { |
| 70 | + await new Promise((resolve) => setTimeout(resolve, 10)) |
| 71 | + callSequence.push('another cleanup') |
| 72 | + }) |
| 73 | + const hookWithCleanup = () => { |
| 74 | + useEffect(() => { |
| 75 | + return () => { |
| 76 | + callSequence.push('unmount') |
| 77 | + } |
| 78 | + }) |
| 79 | + } |
| 80 | + renderHook(() => hookWithCleanup()) |
| 81 | + |
| 82 | + await cleanup() |
| 83 | + |
| 84 | + expect(callSequence).toEqual(['unmount', 'another cleanup', 'cleanup']) |
| 85 | + }) |
| 86 | + |
| 87 | + test('should remove cleanup using removeCleanup', async () => { |
| 88 | + let callSequence = [] |
| 89 | + addCleanup(() => { |
| 90 | + callSequence.push('cleanup') |
| 91 | + }) |
| 92 | + const anotherCleanup = () => { |
| 93 | + callSequence.push('another cleanup') |
| 94 | + } |
| 95 | + addCleanup(anotherCleanup) |
| 96 | + const hookWithCleanup = () => { |
| 97 | + useEffect(() => { |
| 98 | + return () => { |
| 99 | + callSequence.push('unmount') |
| 100 | + } |
| 101 | + }) |
| 102 | + } |
| 103 | + renderHook(() => hookWithCleanup()) |
| 104 | + |
| 105 | + removeCleanup(anotherCleanup) |
| 106 | + |
| 107 | + await cleanup() |
| 108 | + |
| 109 | + expect(callSequence).toEqual(['unmount', 'cleanup']) |
| 110 | + }) |
| 111 | + |
| 112 | + test('should remove cleanup using returned handler', async () => { |
| 113 | + let callSequence = [] |
| 114 | + addCleanup(() => { |
| 115 | + callSequence.push('cleanup') |
| 116 | + }) |
| 117 | + const remove = addCleanup(() => { |
| 118 | + callSequence.push('another cleanup') |
| 119 | + }) |
| 120 | + const hookWithCleanup = () => { |
| 121 | + useEffect(() => { |
| 122 | + return () => { |
| 123 | + callSequence.push('unmount') |
| 124 | + } |
| 125 | + }) |
| 126 | + } |
| 127 | + renderHook(() => hookWithCleanup()) |
| 128 | + |
| 129 | + remove() |
| 130 | + |
| 131 | + await cleanup() |
| 132 | + |
| 133 | + expect(callSequence).toEqual(['unmount', 'cleanup']) |
| 134 | + }) |
41 | 135 | }) |
0 commit comments