Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions app/views/RoomsListView/hooks/useSubscriptions.test.tsx
Original file line number Diff line number Diff line change
@@ -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(
<Provider store={store}>
<Harness />
</Provider>
);

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 }
});
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.

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);
});
});
});
2 changes: 1 addition & 1 deletion app/views/RoomsListView/hooks/useSubscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Subscription>(null);
const [subscriptions, setSubscriptions] = useState<TSubscriptionModel[]>([]);
const [loading, setLoading] = useState(true);
Expand Down