|
| 1 | +import EventEmitter from 'events'; |
| 2 | + |
| 3 | +import { act, renderHook, waitFor } from '@testing-library/react'; |
| 4 | +import { type ReactNode } from 'react'; |
| 5 | +import { MemoryRouter } from 'react-router-dom'; |
| 6 | + |
| 7 | +import { APP_EVENTS } from '@/application/constants'; |
| 8 | +import { AccessService, ViewService } from '@/application/services/domains'; |
| 9 | +import { Role, View, ViewLayout } from '@/application/types'; |
| 10 | +import { AuthInternalContext, AuthInternalContextType } from '@/components/app/contexts/AuthInternalContext'; |
| 11 | +import { SyncInternalContext, SyncInternalContextType } from '@/components/app/contexts/SyncInternalContext'; |
| 12 | + |
| 13 | +import { useWorkspaceData } from '../useWorkspaceData'; |
| 14 | + |
| 15 | +jest.mock('lodash-es', () => ({ |
| 16 | + sortBy: (items: Record<string, unknown>[], key: string) => |
| 17 | + [...items].sort((a, b) => String(a[key] ?? '').localeCompare(String(b[key] ?? ''))), |
| 18 | + uniqBy: (items: Record<string, unknown>[], key: string) => { |
| 19 | + const seen = new Set<unknown>(); |
| 20 | + |
| 21 | + return items.filter((item) => { |
| 22 | + const value = item[key]; |
| 23 | + |
| 24 | + if (seen.has(value)) return false; |
| 25 | + seen.add(value); |
| 26 | + return true; |
| 27 | + }); |
| 28 | + }, |
| 29 | +})); |
| 30 | + |
| 31 | +jest.mock('@/application/services/domains', () => ({ |
| 32 | + AccessService: { |
| 33 | + getShareWithMe: jest.fn(), |
| 34 | + }, |
| 35 | + ViewService: { |
| 36 | + get: jest.fn(), |
| 37 | + getDatabaseRelations: jest.fn(), |
| 38 | + getMultiple: jest.fn(), |
| 39 | + getOutline: jest.fn(), |
| 40 | + getTrash: jest.fn(), |
| 41 | + invalidateCache: jest.fn(), |
| 42 | + }, |
| 43 | + WorkspaceService: { |
| 44 | + getMentionableUsers: jest.fn(), |
| 45 | + }, |
| 46 | +})); |
| 47 | + |
| 48 | +const workspaceId = 'workspace-id'; |
| 49 | +const restoredViewId = 'restored-view-id'; |
| 50 | + |
| 51 | +function createDeferred<T>() { |
| 52 | + let resolve!: (value: T) => void; |
| 53 | + let reject!: (reason?: unknown) => void; |
| 54 | + const promise = new Promise<T>((res, rej) => { |
| 55 | + resolve = res; |
| 56 | + reject = rej; |
| 57 | + }); |
| 58 | + |
| 59 | + return { promise, resolve, reject }; |
| 60 | +} |
| 61 | + |
| 62 | +const createView = (viewId: string, overrides: Partial<View> = {}): View => ({ |
| 63 | + view_id: viewId, |
| 64 | + name: overrides.name ?? viewId, |
| 65 | + icon: overrides.icon ?? null, |
| 66 | + layout: overrides.layout ?? ViewLayout.Document, |
| 67 | + extra: overrides.extra ?? null, |
| 68 | + children: overrides.children ?? [], |
| 69 | + has_children: overrides.has_children, |
| 70 | + is_published: overrides.is_published ?? false, |
| 71 | + is_private: overrides.is_private ?? false, |
| 72 | + ...overrides, |
| 73 | +}); |
| 74 | + |
| 75 | +function createWrapper(eventEmitter: EventEmitter) { |
| 76 | + const authContext: AuthInternalContextType = { |
| 77 | + currentWorkspaceId: workspaceId, |
| 78 | + isAuthenticated: true, |
| 79 | + onChangeWorkspace: jest.fn(), |
| 80 | + userWorkspaceInfo: { |
| 81 | + userId: 'user-id', |
| 82 | + selectedWorkspace: { |
| 83 | + id: workspaceId, |
| 84 | + databaseStorageId: 'database-storage-id', |
| 85 | + role: Role.Owner, |
| 86 | + }, |
| 87 | + } as AuthInternalContextType['userWorkspaceInfo'], |
| 88 | + }; |
| 89 | + |
| 90 | + const syncContext = { |
| 91 | + eventEmitter, |
| 92 | + awarenessMap: {}, |
| 93 | + broadcastChannel: {}, |
| 94 | + flushAllSync: jest.fn(), |
| 95 | + registerSyncContext: jest.fn(), |
| 96 | + revertCollabVersion: jest.fn(), |
| 97 | + scheduleDeferredCleanup: jest.fn(), |
| 98 | + syncAllToServer: jest.fn(), |
| 99 | + webSocket: {}, |
| 100 | + } as unknown as SyncInternalContextType; |
| 101 | + |
| 102 | + return function Wrapper({ children }: { children: ReactNode }) { |
| 103 | + return ( |
| 104 | + <MemoryRouter future={{ v7_relativeSplatPath: true, v7_startTransition: true }}> |
| 105 | + <AuthInternalContext.Provider value={authContext}> |
| 106 | + <SyncInternalContext.Provider value={syncContext}> |
| 107 | + {children} |
| 108 | + </SyncInternalContext.Provider> |
| 109 | + </AuthInternalContext.Provider> |
| 110 | + </MemoryRouter> |
| 111 | + ); |
| 112 | + }; |
| 113 | +} |
| 114 | + |
| 115 | +describe('useWorkspaceData trash refresh', () => { |
| 116 | + beforeEach(() => { |
| 117 | + jest.clearAllMocks(); |
| 118 | + |
| 119 | + (AccessService.getShareWithMe as jest.Mock).mockResolvedValue(null); |
| 120 | + (ViewService.getDatabaseRelations as jest.Mock).mockResolvedValue({}); |
| 121 | + (ViewService.getMultiple as jest.Mock).mockResolvedValue([]); |
| 122 | + (ViewService.getOutline as jest.Mock).mockResolvedValue({ |
| 123 | + outline: [], |
| 124 | + folderRid: '1-1', |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + it('refreshes stale trash state when a remote restore adds the view back to the folder', async () => { |
| 129 | + const eventEmitter = new EventEmitter(); |
| 130 | + const restoredView = createView(restoredViewId); |
| 131 | + let trashResponse: View[] = [restoredView]; |
| 132 | + |
| 133 | + (ViewService.getTrash as jest.Mock).mockImplementation(async () => trashResponse); |
| 134 | + |
| 135 | + const { result } = renderHook(() => useWorkspaceData(), { |
| 136 | + wrapper: createWrapper(eventEmitter), |
| 137 | + }); |
| 138 | + |
| 139 | + await waitFor(() => { |
| 140 | + expect(result.current.trashList?.map((view) => view.view_id)).toEqual([restoredViewId]); |
| 141 | + }); |
| 142 | + |
| 143 | + const initialTrashRequestCount = (ViewService.getTrash as jest.Mock).mock.calls.length; |
| 144 | + |
| 145 | + trashResponse = []; |
| 146 | + |
| 147 | + await act(async () => { |
| 148 | + eventEmitter.emit(APP_EVENTS.FOLDER_VIEW_CHANGED, { |
| 149 | + changeType: 1, |
| 150 | + folderRid: '2-1', |
| 151 | + parentViewId: 'space-id', |
| 152 | + viewJson: JSON.stringify(restoredView), |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + await waitFor(() => { |
| 157 | + expect(ViewService.getTrash).toHaveBeenCalledTimes(initialTrashRequestCount + 1); |
| 158 | + expect(result.current.trashList).toEqual([]); |
| 159 | + }); |
| 160 | + }); |
| 161 | + |
| 162 | + it('ignores stale trash refresh responses from overlapping folder notifications', async () => { |
| 163 | + const eventEmitter = new EventEmitter(); |
| 164 | + const restoredView = createView(restoredViewId); |
| 165 | + const trashRequests: Array<ReturnType<typeof createDeferred<View[]>>> = []; |
| 166 | + |
| 167 | + (ViewService.getTrash as jest.Mock).mockImplementation(() => { |
| 168 | + const deferred = createDeferred<View[]>(); |
| 169 | + |
| 170 | + trashRequests.push(deferred); |
| 171 | + return deferred.promise; |
| 172 | + }); |
| 173 | + |
| 174 | + const { result } = renderHook(() => useWorkspaceData(), { |
| 175 | + wrapper: createWrapper(eventEmitter), |
| 176 | + }); |
| 177 | + |
| 178 | + await waitFor(() => { |
| 179 | + expect(trashRequests).toHaveLength(1); |
| 180 | + }); |
| 181 | + |
| 182 | + await act(async () => { |
| 183 | + trashRequests[0].resolve([restoredView]); |
| 184 | + }); |
| 185 | + |
| 186 | + await waitFor(() => { |
| 187 | + expect(result.current.trashList?.map((view) => view.view_id)).toEqual([restoredViewId]); |
| 188 | + }); |
| 189 | + |
| 190 | + await act(async () => { |
| 191 | + eventEmitter.emit(APP_EVENTS.FOLDER_VIEW_CHANGED, { |
| 192 | + changeType: 1, |
| 193 | + folderRid: '2-1', |
| 194 | + parentViewId: 'space-id', |
| 195 | + viewJson: JSON.stringify(restoredView), |
| 196 | + }); |
| 197 | + eventEmitter.emit(APP_EVENTS.FOLDER_OUTLINE_CHANGED, { |
| 198 | + folderRid: '3-1', |
| 199 | + outlineDiffJson: JSON.stringify([{ op: 'replace', path: '/outline', value: [] }]), |
| 200 | + }); |
| 201 | + }); |
| 202 | + |
| 203 | + await waitFor(() => { |
| 204 | + expect(trashRequests).toHaveLength(3); |
| 205 | + }); |
| 206 | + |
| 207 | + await act(async () => { |
| 208 | + trashRequests[2].resolve([]); |
| 209 | + }); |
| 210 | + |
| 211 | + await waitFor(() => { |
| 212 | + expect(result.current.trashList).toEqual([]); |
| 213 | + }); |
| 214 | + |
| 215 | + await act(async () => { |
| 216 | + trashRequests[1].resolve([restoredView]); |
| 217 | + }); |
| 218 | + |
| 219 | + await waitFor(() => { |
| 220 | + expect(result.current.trashList).toEqual([]); |
| 221 | + }); |
| 222 | + }); |
| 223 | +}); |
0 commit comments