|
| 1 | +import { renderHook, act } from '@testing-library/react'; |
| 2 | +import { useTypingLifecycle } from '../useTypingLifecycle'; |
| 3 | + |
| 4 | +const makeChannel = (url = 'channel-a') => ({ |
| 5 | + url, |
| 6 | + startTyping: jest.fn(), |
| 7 | + endTyping: jest.fn(), |
| 8 | +}); |
| 9 | + |
| 10 | +describe('useTypingLifecycle', () => { |
| 11 | + it('startTyping calls channel.startTyping', () => { |
| 12 | + const channel = makeChannel(); |
| 13 | + const { result } = renderHook(() => useTypingLifecycle(channel)); |
| 14 | + |
| 15 | + act(() => result.current.startTyping()); |
| 16 | + |
| 17 | + expect(channel.startTyping).toHaveBeenCalledTimes(1); |
| 18 | + expect(channel.endTyping).not.toHaveBeenCalled(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('stopTyping calls channel.endTyping', () => { |
| 22 | + const channel = makeChannel(); |
| 23 | + const { result } = renderHook(() => useTypingLifecycle(channel)); |
| 24 | + |
| 25 | + act(() => result.current.startTyping()); |
| 26 | + act(() => result.current.stopTyping()); |
| 27 | + |
| 28 | + expect(channel.endTyping).toHaveBeenCalledTimes(1); |
| 29 | + }); |
| 30 | + |
| 31 | + it('calls endTyping on unmount when typing was active', () => { |
| 32 | + const channel = makeChannel(); |
| 33 | + const { result, unmount } = renderHook(() => useTypingLifecycle(channel)); |
| 34 | + |
| 35 | + act(() => result.current.startTyping()); |
| 36 | + unmount(); |
| 37 | + |
| 38 | + expect(channel.endTyping).toHaveBeenCalledTimes(1); |
| 39 | + }); |
| 40 | + |
| 41 | + it('does not call endTyping on unmount when typing was never active', () => { |
| 42 | + const channel = makeChannel(); |
| 43 | + const { unmount } = renderHook(() => useTypingLifecycle(channel)); |
| 44 | + |
| 45 | + unmount(); |
| 46 | + |
| 47 | + expect(channel.endTyping).not.toHaveBeenCalled(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('does not duplicate endTyping when stopTyping called before unmount', () => { |
| 51 | + const channel = makeChannel(); |
| 52 | + const { result, unmount } = renderHook(() => useTypingLifecycle(channel)); |
| 53 | + |
| 54 | + act(() => result.current.startTyping()); |
| 55 | + act(() => result.current.stopTyping()); |
| 56 | + unmount(); |
| 57 | + |
| 58 | + expect(channel.endTyping).toHaveBeenCalledTimes(1); |
| 59 | + }); |
| 60 | + |
| 61 | + it('calls endTyping on previous channel when channel changes', () => { |
| 62 | + const channelA = makeChannel('a'); |
| 63 | + const channelB = makeChannel('b'); |
| 64 | + |
| 65 | + const { result, rerender } = renderHook( |
| 66 | + ({ channel }) => useTypingLifecycle(channel), |
| 67 | + { initialProps: { channel: channelA } }, |
| 68 | + ); |
| 69 | + |
| 70 | + act(() => result.current.startTyping()); |
| 71 | + expect(channelA.startTyping).toHaveBeenCalledTimes(1); |
| 72 | + |
| 73 | + rerender({ channel: channelB }); |
| 74 | + |
| 75 | + expect(channelA.endTyping).toHaveBeenCalledTimes(1); |
| 76 | + expect(channelB.endTyping).not.toHaveBeenCalled(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('calls endTyping when active toggles to false', () => { |
| 80 | + const channel = makeChannel(); |
| 81 | + |
| 82 | + const { result, rerender } = renderHook( |
| 83 | + ({ active }) => useTypingLifecycle(channel, active), |
| 84 | + { initialProps: { active: true } }, |
| 85 | + ); |
| 86 | + |
| 87 | + act(() => result.current.startTyping()); |
| 88 | + rerender({ active: false }); |
| 89 | + |
| 90 | + expect(channel.endTyping).toHaveBeenCalledTimes(1); |
| 91 | + }); |
| 92 | + |
| 93 | + it('does not call endTyping when active toggles to false without typing', () => { |
| 94 | + const channel = makeChannel(); |
| 95 | + |
| 96 | + const { rerender } = renderHook( |
| 97 | + ({ active }) => useTypingLifecycle(channel, active), |
| 98 | + { initialProps: { active: true } }, |
| 99 | + ); |
| 100 | + |
| 101 | + rerender({ active: false }); |
| 102 | + |
| 103 | + expect(channel.endTyping).not.toHaveBeenCalled(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('handles null channel gracefully', () => { |
| 107 | + const { result, unmount } = renderHook(() => useTypingLifecycle(null)); |
| 108 | + |
| 109 | + expect(() => { |
| 110 | + act(() => result.current.startTyping()); |
| 111 | + act(() => result.current.stopTyping()); |
| 112 | + unmount(); |
| 113 | + }).not.toThrow(); |
| 114 | + }); |
| 115 | + |
| 116 | + describe('same-URL channel re-instantiation', () => { |
| 117 | + it('cleans up on the latest instance when typing starts after re-instantiation', () => { |
| 118 | + const original = makeChannel('a'); |
| 119 | + const refreshed = makeChannel('a'); |
| 120 | + |
| 121 | + const { result, rerender, unmount } = renderHook( |
| 122 | + ({ channel }) => useTypingLifecycle(channel), |
| 123 | + { initialProps: { channel: original } }, |
| 124 | + ); |
| 125 | + |
| 126 | + rerender({ channel: refreshed }); |
| 127 | + act(() => result.current.startTyping()); |
| 128 | + |
| 129 | + expect(refreshed.startTyping).toHaveBeenCalledTimes(1); |
| 130 | + expect(original.startTyping).not.toHaveBeenCalled(); |
| 131 | + |
| 132 | + unmount(); |
| 133 | + |
| 134 | + expect(refreshed.endTyping).toHaveBeenCalledTimes(1); |
| 135 | + expect(original.endTyping).not.toHaveBeenCalled(); |
| 136 | + }); |
| 137 | + |
| 138 | + it('cleans up on the original instance if typing started before re-instantiation', () => { |
| 139 | + const original = makeChannel('a'); |
| 140 | + const refreshed = makeChannel('a'); |
| 141 | + |
| 142 | + const { result, rerender, unmount } = renderHook( |
| 143 | + ({ channel }) => useTypingLifecycle(channel), |
| 144 | + { initialProps: { channel: original } }, |
| 145 | + ); |
| 146 | + |
| 147 | + act(() => result.current.startTyping()); |
| 148 | + rerender({ channel: refreshed }); |
| 149 | + unmount(); |
| 150 | + |
| 151 | + expect(original.endTyping).toHaveBeenCalledTimes(1); |
| 152 | + expect(refreshed.endTyping).not.toHaveBeenCalled(); |
| 153 | + }); |
| 154 | + |
| 155 | + it('uses the latest instance for endTyping when typing is restarted after re-instantiation', () => { |
| 156 | + const original = makeChannel('a'); |
| 157 | + const refreshed = makeChannel('a'); |
| 158 | + |
| 159 | + const { result, rerender, unmount } = renderHook( |
| 160 | + ({ channel }) => useTypingLifecycle(channel), |
| 161 | + { initialProps: { channel: original } }, |
| 162 | + ); |
| 163 | + |
| 164 | + act(() => result.current.startTyping()); |
| 165 | + rerender({ channel: refreshed }); |
| 166 | + act(() => result.current.startTyping()); |
| 167 | + unmount(); |
| 168 | + |
| 169 | + expect(original.startTyping).toHaveBeenCalledTimes(1); |
| 170 | + expect(refreshed.startTyping).toHaveBeenCalledTimes(1); |
| 171 | + // endTyping should hit only the refreshed instance (the most recent |
| 172 | + // startTyping target), not the original. |
| 173 | + expect(original.endTyping).not.toHaveBeenCalled(); |
| 174 | + expect(refreshed.endTyping).toHaveBeenCalledTimes(1); |
| 175 | + }); |
| 176 | + }); |
| 177 | +}); |
0 commit comments