Skip to content

Commit 0af5a6f

Browse files
authored
Added Chat Route in App.tsx to make Chat feature accessible (#3255)
* added chatroute * fixed race condition in chat.tsx * added await to ensure updated queryData * fixed errors in test
1 parent d3657e1 commit 0af5a6f

File tree

3 files changed

+80
-24
lines changed

3 files changed

+80
-24
lines changed

src/App.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import Posts from 'screens/UserPortal/Posts/Posts';
3838
import Organizations from 'screens/UserPortal/Organizations/Organizations';
3939
import People from 'screens/UserPortal/People/People';
4040
import Settings from 'screens/UserPortal/Settings/Settings';
41-
// import Chat from 'screens/UserPortal/Chat/Chat';
41+
import Chat from 'screens/UserPortal/Chat/Chat';
4242
import { useQuery } from '@apollo/client';
4343
import { CHECK_AUTH } from 'GraphQl/Queries/Queries';
4444
import Advertisements from 'components/Advertisements/Advertisements';
@@ -191,6 +191,7 @@ function app(): JSX.Element {
191191
<Route element={<SecuredRouteForUser />}>
192192
<Route path="/user/organizations" element={<Organizations />} />
193193
<Route path="/user/settings" element={<Settings />} />
194+
<Route path="/user/chat/:orgId" element={<Chat />} />
194195
{/* <Route path="/user/chat" element={<Chat />} /> */}
195196
<Route element={<UserScreen />}>
196197
<Route path="/user/organizations" element={<Organizations />} />

src/screens/UserPortal/Chat/Chat.spec.tsx

+56-4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
UNREAD_CHAT_LIST,
3030
} from 'GraphQl/Queries/PlugInQueries';
3131
import useLocalStorage from 'utils/useLocalstorage';
32+
import { StaticMockLink } from 'utils/StaticMockLink';
3233
// import userEvent from '@testing-library/user-event';
3334

3435
/**
@@ -4289,9 +4290,27 @@ describe('Testing Chat Screen [User Portal]', () => {
42894290
{
42904291
_id: '1',
42914292
isGroup: false,
4292-
users: [{ _id: '1', firstName: 'John', lastName: 'Doe' }],
4293+
users: [
4294+
{
4295+
_id: '1',
4296+
firstName: 'John',
4297+
lastName: 'Doe',
4298+
email: 'johndoe@example.com',
4299+
image: null,
4300+
},
4301+
],
42934302
messages: [],
4303+
name: null,
4304+
image: null,
42944305
unseenMessagesByUsers: '{}',
4306+
creator: {
4307+
_id: '1',
4308+
firstName: 'John',
4309+
lastName: 'Doe',
4310+
email: 'johndoe@example.com',
4311+
},
4312+
organization: null,
4313+
admins: [],
42954314
},
42964315
],
42974316
};
@@ -4319,13 +4338,44 @@ describe('Testing Chat Screen [User Portal]', () => {
43194338
},
43204339
},
43214340
},
4341+
{
4342+
request: {
4343+
query: UNREAD_CHAT_LIST,
4344+
},
4345+
result: {
4346+
data: {
4347+
getUnreadChatsByUserId: [],
4348+
},
4349+
},
4350+
},
4351+
{
4352+
request: {
4353+
query: GROUP_CHAT_LIST,
4354+
},
4355+
result: {
4356+
data: {
4357+
getGroupChatsByUserId: [],
4358+
},
4359+
},
4360+
},
4361+
{
4362+
request: {
4363+
query: CHATS_LIST,
4364+
variables: { id: '1' },
4365+
},
4366+
result: {
4367+
data: mockChatsData,
4368+
},
4369+
},
43224370
];
4323-
4371+
const link = new StaticMockLink(mocks, true);
43244372
render(
4325-
<MockedProvider mocks={mocks} addTypename={false}>
4373+
<MockedProvider link={link} addTypename={false}>
43264374
<BrowserRouter>
43274375
<Provider store={store}>
4328-
<Chat />
4376+
<I18nextProvider i18n={i18nForTest}>
4377+
<Chat />
4378+
</I18nextProvider>
43294379
</Provider>
43304380
</BrowserRouter>
43314381
</MockedProvider>,
@@ -4387,6 +4437,7 @@ describe('Testing Chat Screen [User Portal]', () => {
43874437
const contactCards = await screen.findAllByTestId('contactCardContainer');
43884438
expect(contactCards).toHaveLength(1);
43894439
});
4440+
43904441
test('Screen should be rendered properly', async () => {
43914442
render(
43924443
<MockedProvider addTypename={false} mocks={mock}>
@@ -4514,6 +4565,7 @@ describe('Testing Chat Screen [User Portal]', () => {
45144565
fireEvent.click(await screen.findByTestId('allChat'));
45154566
});
45164567
});
4568+
45174569
it('should fetch and set group chats when filterType is "group"', async () => {
45184570
setItem('userId', '1');
45194571

src/screens/UserPortal/Chat/Chat.tsx

+22-19
Original file line numberDiff line numberDiff line change
@@ -111,25 +111,6 @@ export default function chat(): JSX.Element {
111111
const { getItem } = useLocalStorage();
112112
const userId = getItem('userId');
113113

114-
React.useEffect(() => {
115-
if (filterType === 'all') {
116-
chatsListRefetch();
117-
if (chatsListData && chatsListData.chatsByUserId) {
118-
setChats(chatsListData.chatsByUserId);
119-
}
120-
} else if (filterType === 'unread') {
121-
unreadChatListRefetch();
122-
if (unreadChatListData && unreadChatListData.getUnreadChatsByUserId) {
123-
setChats(unreadChatListData.getUnreadChatsByUserId);
124-
}
125-
} else if (filterType === 'group') {
126-
groupChatListRefetch();
127-
if (groupChatListData && groupChatListData.getGroupChatsByUserId) {
128-
setChats(groupChatListData.getGroupChatsByUserId);
129-
}
130-
}
131-
}, [filterType]);
132-
133114
const [createDirectChatModalisOpen, setCreateDirectChatModalisOpen] =
134115
useState(false);
135116

@@ -180,6 +161,28 @@ export default function chat(): JSX.Element {
180161
});
181162
}, [selectedContact]);
182163

164+
React.useEffect(() => {
165+
async function getChats(): Promise<void> {
166+
if (filterType === 'all') {
167+
await chatsListRefetch();
168+
if (chatsListData && chatsListData.chatsByUserId) {
169+
setChats(chatsListData.chatsByUserId);
170+
}
171+
} else if (filterType === 'unread') {
172+
await unreadChatListRefetch();
173+
if (unreadChatListData && unreadChatListData.getUnreadChatsByUserId) {
174+
setChats(unreadChatListData.getUnreadChatsByUserId);
175+
}
176+
} else if (filterType === 'group') {
177+
await groupChatListRefetch();
178+
if (groupChatListData && groupChatListData.getGroupChatsByUserId) {
179+
setChats(groupChatListData.getGroupChatsByUserId);
180+
}
181+
}
182+
}
183+
getChats();
184+
}, [filterType]);
185+
183186
React.useEffect(() => {
184187
if (chatsListData && chatsListData?.chatsByUserId.length) {
185188
setChats(chatsListData.chatsByUserId);

0 commit comments

Comments
 (0)