Skip to content

Commit

Permalink
Bugfix: fixed duplication of chats (#3360)
Browse files Browse the repository at this point in the history
* fixed duplication of chats

* removed istanbul ignore text

* added error handling

* added tests for createdirectchat.tsx
  • Loading branch information
Aad1tya27 authored Jan 21, 2025
1 parent a32533b commit 5434bfc
Show file tree
Hide file tree
Showing 6 changed files with 342 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

> **default**(`__namedParameters`): `JSX.Element`
Defined in: [src/components/UserPortal/CreateDirectChat/CreateDirectChat.tsx:60](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/components/UserPortal/CreateDirectChat/CreateDirectChat.tsx#L60)
Defined in: [src/components/UserPortal/CreateDirectChat/CreateDirectChat.tsx:136](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/components/UserPortal/CreateDirectChat/CreateDirectChat.tsx#L136)

## Parameters

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[Admin Docs](/)

***

# Function: handleCreateDirectChat()

> **handleCreateDirectChat**(`id`, `chats`, `t`, `createChat`, `organizationId`, `userId`, `chatsListRefetch`, `toggleCreateDirectChatModal`): `Promise`\<`void`\>
Defined in: [src/components/UserPortal/CreateDirectChat/CreateDirectChat.tsx:70](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/components/UserPortal/CreateDirectChat/CreateDirectChat.tsx#L70)

## Parameters

### id

`string`

### chats

[`Chat`](../../../../../screens/UserPortal/Chat/Chat/type-aliases/Chat.md)[]

### t

`TFunction`\<`"userChat"`\>

### createChat

(`options`?) => `Promise`\<`FetchResult`\<`unknown`\>\>(`arg0`) => `unknown`

### organizationId

`string`

### userId

`string`

### chatsListRefetch

(`variables`?) => `Promise`\<`ApolloQueryResult`\<`unknown`\>\>() => `Promise`\<`ApolloQueryResult`\<`unknown`\>\>

### toggleCreateDirectChatModal

() => `void`() => `void`

## Returns

`Promise`\<`void`\>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[Admin Docs](/)

***

# Type Alias: Chat

> **Chat**: `object`
Defined in: [src/screens/UserPortal/Chat/Chat.tsx:87](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/screens/UserPortal/Chat/Chat.tsx#L87)

## Type declaration

### \_id

> **\_id**: `string`
### image

> **image**: `string`
### isGroup

> **isGroup**: `boolean`
### messages

> **messages**: `DirectMessage`[]
### name

> **name**: `string`
### unseenMessagesByUsers

> **unseenMessagesByUsers**: `string`
### users

> **users**: `object`[]
167 changes: 164 additions & 3 deletions src/components/UserPortal/CreateDirectChat/CreateDirectChat.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { BrowserRouter } from 'react-router-dom';
import { Provider } from 'react-redux';
import { store } from 'state/store';
import i18nForTest from 'utils/i18nForTest';
import Chat from '../../../screens/UserPortal/Chat/Chat';
import type { Chat } from '../../../screens/UserPortal/Chat/Chat';
import ChatComp from '../../../screens/UserPortal/Chat/Chat';
import {
CREATE_CHAT,
MARK_CHAT_MESSAGES_AS_READ,
Expand All @@ -27,6 +28,10 @@ import {
} from 'GraphQl/Queries/PlugInQueries';
import useLocalStorage from 'utils/useLocalstorage';
import { vi } from 'vitest';
import { handleCreateDirectChat } from './CreateDirectChat';
import type { TFunction } from 'i18next';
import type { ApolloQueryResult, FetchResult } from '@apollo/client';

const { setItem } = useLocalStorage();

/**
Expand Down Expand Up @@ -3947,7 +3952,7 @@ describe('Testing Create Direct Chat Modal [User Portal]', () => {
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<Chat />
<ChatComp />
</I18nextProvider>
</Provider>
</BrowserRouter>
Expand Down Expand Up @@ -4001,7 +4006,7 @@ describe('Testing Create Direct Chat Modal [User Portal]', () => {
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<Chat />
<ChatComp />
</I18nextProvider>
</Provider>
</BrowserRouter>
Expand Down Expand Up @@ -4033,4 +4038,160 @@ describe('Testing Create Direct Chat Modal [User Portal]', () => {

await wait();
});
it('should create chat if a conversation does not with the selected user', async () => {
const t = ((key: string) => {
return key;
}) as TFunction;
const createChat = vi.fn(async (): Promise<FetchResult<unknown>> => {
return { data: {} } as FetchResult<unknown>;
});
const chatsListRefetch = vi.fn(
async (): Promise<ApolloQueryResult<unknown>> => {
return { data: {} } as ApolloQueryResult<unknown>;
},
);
const toggleCreateDirectChatModal = vi.fn();
const chats: Chat[] = [
{
_id: '1',
isGroup: false,
name: '',
image: '',
messages: [],
unseenMessagesByUsers: '{}',
users: [
{
_id: '1',
firstName: 'Aaditya',
lastName: 'Agarwal',
email: '',
image: '',
},
{
_id: '3',
firstName: 'Test',
lastName: 'User',
email: '',
image: '',
},
],
},
];
await handleCreateDirectChat(
'2',
chats,
t,
createChat,
'1',
'1',
chatsListRefetch,
toggleCreateDirectChatModal,
);
expect(createChat).toHaveBeenCalled();
expect(chatsListRefetch).toHaveBeenCalled();
expect(toggleCreateDirectChatModal).toHaveBeenCalled();
});
it('should not create chat if a conversation already exists with the selected user', async () => {
const t = ((key: string) => {
return key;
}) as TFunction;
const createChat = vi.fn(async (): Promise<FetchResult<unknown>> => {
return { data: {} } as FetchResult<unknown>;
});
const chatsListRefetch = vi.fn(
async (): Promise<ApolloQueryResult<unknown>> => {
return { data: {} } as ApolloQueryResult<unknown>;
},
);
const toggleCreateDirectChatModal = vi.fn();
const chats: Chat[] = [
{
_id: '1',
isGroup: false,
name: '',
image: '',
messages: [],
unseenMessagesByUsers: '{}',
users: [
{
_id: '1',
firstName: 'Aaditya',
lastName: 'Agarwal',
email: '',
image: '',
},
{
_id: '2',
firstName: 'Test',
lastName: 'User',
email: '',
image: '',
},
],
},
];
await handleCreateDirectChat(
'2',
chats,
t,
createChat,
'1',
'1',
chatsListRefetch,
toggleCreateDirectChatModal,
);
expect(createChat).not.toHaveBeenCalled();
expect(chatsListRefetch).not.toHaveBeenCalled();
expect(toggleCreateDirectChatModal).not.toHaveBeenCalled();
});

it('should handle error if create chat fails', async () => {
const t = ((key: string) => {
return key;
}) as TFunction;
const createChat = vi.fn(async (): Promise<FetchResult<unknown>> => {
throw new Error('Error');
});
const chatsListRefetch = vi.fn();
const toggleCreateDirectChatModal = vi.fn();
const chats: Chat[] = [
{
_id: '1',
isGroup: false,
name: '',
image: '',
messages: [],
unseenMessagesByUsers: '{}',
users: [
{
_id: '1',
firstName: 'Aaditya',
lastName: 'Agarwal',
email: '',
image: '',
},
{
_id: '3',
firstName: 'Test',
lastName: 'User',
email: '',
image: '',
},
],
},
];
await handleCreateDirectChat(
'2',
chats,
t,
createChat,
'1',
'1',
chatsListRefetch,
toggleCreateDirectChatModal,
);
expect(createChat).toHaveBeenCalled();
expect(chatsListRefetch).not.toHaveBeenCalled();
expect(toggleCreateDirectChatModal).not.toHaveBeenCalled();
});
});
Loading

0 comments on commit 5434bfc

Please sign in to comment.