diff --git a/app/views/RoomsListView/hooks/useSubscriptions.test.tsx b/app/views/RoomsListView/hooks/useSubscriptions.test.tsx new file mode 100644 index 00000000000..d28c15cdc77 --- /dev/null +++ b/app/views/RoomsListView/hooks/useSubscriptions.test.tsx @@ -0,0 +1,79 @@ +import React from 'react'; +import { render, act, waitFor } from '@testing-library/react-native'; +import { Provider } from 'react-redux'; +import { createStore } from 'redux'; + +import { useSubscriptions } from './useSubscriptions'; + +let queryCallCount = 0; + +jest.mock('../../../lib/database', () => ({ + __esModule: true, + default: { + active: { + get: () => ({ + query: () => ({ + observeWithColumns: () => { + queryCallCount += 1; + return { + subscribe: (cb: (data: any[]) => void) => { + cb([]); + return { unsubscribe: jest.fn() }; + } + }; + } + }) + }) + } + } +})); + +const baseState = { + server: { server: 'https://open.rocket.chat', connecting: false, loading: false, connected: true }, + settings: { UI_Use_Real_Name: false }, + sortPreferences: { sortBy: 'activity', showUnread: false, showFavorites: false, groupByType: false }, + login: { user: { roles: [] } } +}; + +const rootReducer = (state: any = baseState, action: any) => + action.type === 'UPDATE_SERVER' ? { ...state, server: action.server } : state; + +const Harness = () => { + useSubscriptions(); + return null; +}; + +describe('useSubscriptions', () => { + it('does not re-query on connection state changes', async () => { + const store = createStore(rootReducer, baseState); + + render( + + + + ); + + await waitFor(() => { + expect(queryCallCount).toBeGreaterThanOrEqual(1); + }); + const initialCalls = queryCallCount; + + await act(async () => { + store.dispatch({ + type: 'UPDATE_SERVER', + server: { server: 'https://open.rocket.chat', connecting: true, loading: false, connected: false } + }); + }); + + await act(async () => { + store.dispatch({ + type: 'UPDATE_SERVER', + server: { server: 'https://open.rocket.chat', connecting: false, loading: true, connected: false } + }); + }); + + await waitFor(() => { + expect(queryCallCount).toBe(initialCalls); + }); + }); +}); diff --git a/app/views/RoomsListView/hooks/useSubscriptions.ts b/app/views/RoomsListView/hooks/useSubscriptions.ts index 485649f35c4..db6f6b00d26 100644 --- a/app/views/RoomsListView/hooks/useSubscriptions.ts +++ b/app/views/RoomsListView/hooks/useSubscriptions.ts @@ -39,7 +39,7 @@ export const useSubscriptions = () => { 'use memo'; const useRealName = useAppSelector(state => state.settings.UI_Use_Real_Name); - const server = useAppSelector(state => state.server); + const server = useAppSelector(state => state.server.server); const subscriptionRef = useRef(null); const [subscriptions, setSubscriptions] = useState([]); const [loading, setLoading] = useState(true);