|
| 1 | +import { act, renderHook, waitFor } from '@testing-library/react'; |
| 2 | +import fetchMock from 'fetch-mock'; |
| 3 | +import { useRouter } from 'next/router'; |
| 4 | +import * as Y from 'yjs'; |
| 5 | + |
| 6 | +import { AppWrapper } from '@/tests/utils'; |
| 7 | + |
| 8 | +import useSaveDoc from '../useSaveDoc'; |
| 9 | + |
| 10 | +jest.mock('next/router', () => ({ |
| 11 | + useRouter: jest.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +jest.mock('@/docs/doc-versioning', () => ({ |
| 15 | + KEY_LIST_DOC_VERSIONS: 'test-key-list-doc-versions', |
| 16 | +})); |
| 17 | + |
| 18 | +jest.mock('@/docs/doc-management', () => ({ |
| 19 | + useUpdateDoc: jest.requireActual('@/docs/doc-management/api/useUpdateDoc') |
| 20 | + .useUpdateDoc, |
| 21 | +})); |
| 22 | + |
| 23 | +describe('useSaveDoc', () => { |
| 24 | + const mockRouterEvents = { |
| 25 | + on: jest.fn(), |
| 26 | + off: jest.fn(), |
| 27 | + }; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + jest.clearAllMocks(); |
| 31 | + fetchMock.restore(); |
| 32 | + |
| 33 | + (useRouter as jest.Mock).mockReturnValue({ |
| 34 | + events: mockRouterEvents, |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should setup event listeners on mount', () => { |
| 39 | + const yDoc = new Y.Doc(); |
| 40 | + const docId = 'test-doc-id'; |
| 41 | + |
| 42 | + const addEventListenerSpy = jest.spyOn(window, 'addEventListener'); |
| 43 | + |
| 44 | + renderHook(() => useSaveDoc(docId, yDoc, true), { |
| 45 | + wrapper: AppWrapper, |
| 46 | + }); |
| 47 | + |
| 48 | + // Verify router event listeners are set up |
| 49 | + expect(mockRouterEvents.on).toHaveBeenCalledWith( |
| 50 | + 'routeChangeStart', |
| 51 | + expect.any(Function), |
| 52 | + ); |
| 53 | + |
| 54 | + // Verify window event listener is set up |
| 55 | + expect(addEventListenerSpy).toHaveBeenCalledWith( |
| 56 | + 'beforeunload', |
| 57 | + expect.any(Function), |
| 58 | + ); |
| 59 | + |
| 60 | + addEventListenerSpy.mockRestore(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should not save when canSave is false', async () => { |
| 64 | + jest.useFakeTimers(); |
| 65 | + const yDoc = new Y.Doc(); |
| 66 | + const docId = 'test-doc-id'; |
| 67 | + |
| 68 | + fetchMock.patch('http://test.jest/api/v1.0/documents/test-doc-id/', { |
| 69 | + body: JSON.stringify({ |
| 70 | + id: 'test-doc-id', |
| 71 | + content: 'test-content', |
| 72 | + title: 'test-title', |
| 73 | + }), |
| 74 | + }); |
| 75 | + |
| 76 | + renderHook(() => useSaveDoc(docId, yDoc, false), { |
| 77 | + wrapper: AppWrapper, |
| 78 | + }); |
| 79 | + |
| 80 | + act(() => { |
| 81 | + // Trigger a local update |
| 82 | + yDoc.getMap('test').set('key', 'value'); |
| 83 | + }); |
| 84 | + |
| 85 | + act(() => { |
| 86 | + // Now advance timers after state has updated |
| 87 | + jest.advanceTimersByTime(61000); |
| 88 | + }); |
| 89 | + |
| 90 | + await waitFor(() => { |
| 91 | + expect(fetchMock.calls().length).toBe(0); |
| 92 | + }); |
| 93 | + |
| 94 | + jest.useRealTimers(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should save when there are local changes', async () => { |
| 98 | + jest.useFakeTimers(); |
| 99 | + const yDoc = new Y.Doc(); |
| 100 | + const docId = 'test-doc-id'; |
| 101 | + |
| 102 | + fetchMock.patch('http://test.jest/api/v1.0/documents/test-doc-id/', { |
| 103 | + body: JSON.stringify({ |
| 104 | + id: 'test-doc-id', |
| 105 | + content: 'test-content', |
| 106 | + title: 'test-title', |
| 107 | + }), |
| 108 | + }); |
| 109 | + |
| 110 | + renderHook(() => useSaveDoc(docId, yDoc, true), { |
| 111 | + wrapper: AppWrapper, |
| 112 | + }); |
| 113 | + |
| 114 | + act(() => { |
| 115 | + // Trigger a local update |
| 116 | + yDoc.getMap('test').set('key', 'value'); |
| 117 | + }); |
| 118 | + |
| 119 | + act(() => { |
| 120 | + // Now advance timers after state has updated |
| 121 | + jest.advanceTimersByTime(61000); |
| 122 | + }); |
| 123 | + |
| 124 | + await waitFor(() => { |
| 125 | + expect(fetchMock.lastCall()?.[0]).toBe( |
| 126 | + 'http://test.jest/api/v1.0/documents/test-doc-id/', |
| 127 | + ); |
| 128 | + }); |
| 129 | + |
| 130 | + jest.useRealTimers(); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should not save when there are no local changes', async () => { |
| 134 | + jest.useFakeTimers(); |
| 135 | + const yDoc = new Y.Doc(); |
| 136 | + const docId = 'test-doc-id'; |
| 137 | + |
| 138 | + fetchMock.patch('http://test.jest/api/v1.0/documents/test-doc-id/', { |
| 139 | + body: JSON.stringify({ |
| 140 | + id: 'test-doc-id', |
| 141 | + content: 'test-content', |
| 142 | + title: 'test-title', |
| 143 | + }), |
| 144 | + }); |
| 145 | + |
| 146 | + renderHook(() => useSaveDoc(docId, yDoc, true), { |
| 147 | + wrapper: AppWrapper, |
| 148 | + }); |
| 149 | + |
| 150 | + act(() => { |
| 151 | + // Now advance timers after state has updated |
| 152 | + jest.advanceTimersByTime(61000); |
| 153 | + }); |
| 154 | + |
| 155 | + await waitFor(() => { |
| 156 | + expect(fetchMock.calls().length).toBe(0); |
| 157 | + }); |
| 158 | + |
| 159 | + jest.useRealTimers(); |
| 160 | + }); |
| 161 | + |
| 162 | + it('should cleanup event listeners on unmount', () => { |
| 163 | + const yDoc = new Y.Doc(); |
| 164 | + const docId = 'test-doc-id'; |
| 165 | + const removeEventListenerSpy = jest.spyOn(window, 'removeEventListener'); |
| 166 | + |
| 167 | + const { unmount } = renderHook(() => useSaveDoc(docId, yDoc, true), { |
| 168 | + wrapper: AppWrapper, |
| 169 | + }); |
| 170 | + |
| 171 | + unmount(); |
| 172 | + |
| 173 | + // Verify router event listeners are cleaned up |
| 174 | + expect(mockRouterEvents.off).toHaveBeenCalledWith( |
| 175 | + 'routeChangeStart', |
| 176 | + expect.any(Function), |
| 177 | + ); |
| 178 | + |
| 179 | + // Verify window event listener is cleaned up |
| 180 | + expect(removeEventListenerSpy).toHaveBeenCalledWith( |
| 181 | + 'beforeunload', |
| 182 | + expect.any(Function), |
| 183 | + ); |
| 184 | + }); |
| 185 | +}); |
0 commit comments