diff --git a/src/containers/chat/chat-item/ChatItem.tsx b/src/containers/chat/chat-item/ChatItem.tsx index 9770856f98..39883caec9 100644 --- a/src/containers/chat/chat-item/ChatItem.tsx +++ b/src/containers/chat/chat-item/ChatItem.tsx @@ -5,12 +5,12 @@ import Box from '@mui/material/Box' import Typography from '@mui/material/Typography' import Badge from '@mui/material/Badge' +import { useChatContext } from '~/context/chat-context' import { useAppSelector } from '~/hooks/use-redux' import { styles } from '~/containers/chat/chat-item/ChatItem.styles' import { ChatResponse, ComponentEnum, OverlapEnum, PositionEnum } from '~/types' import { getFormattedDate } from '~/utils/helper-functions' -import { useChatContext } from '~/context/chat-context' interface ItemOfChatProps { isActiveChat: boolean diff --git a/src/containers/offer-page/chat-dialog-window/ChatDialogWindow.tsx b/src/containers/offer-page/chat-dialog-window/ChatDialogWindow.tsx index af9bdad5da..f1380e55f9 100644 --- a/src/containers/offer-page/chat-dialog-window/ChatDialogWindow.tsx +++ b/src/containers/offer-page/chat-dialog-window/ChatDialogWindow.tsx @@ -17,12 +17,12 @@ import Typography from '@mui/material/Typography' import ChatDate from '~/containers/chat/chat-date/ChatDate' import ChatTextArea from '~/containers/chat/chat-text-area/ChatTextArea' -import Message from '~/components/message/Message' +import { useChatContext } from '~/context/chat-context' import useAxios from '~/hooks/use-axios' +import Message from '~/components/message/Message' import UserProfileInfo from '~/components/user-profile-info/UserProfileInfo' import Loader from '~/components/loader/Loader' import { messageService } from '~/services/message-service' -import { useChatContext } from '~/context/chat-context' import { getGroupedMessages } from '~/utils/helper-functions' import { ChatInfo, MessageInterface } from '~/types' diff --git a/src/pages/chat/Chat.tsx b/src/pages/chat/Chat.tsx index 0952cdae66..082385a2b2 100644 --- a/src/pages/chat/Chat.tsx +++ b/src/pages/chat/Chat.tsx @@ -97,7 +97,7 @@ const Chat = () => { useEffect(() => { if (currentChatId) { listOfChats.forEach((chat: ChatResponse) => { - if (chat._id === currentChatId) setSelectedChat(chat) + if (chat._id === currentChatId) return setSelectedChat(chat) }) } }, [currentChatId, listOfChats]) diff --git a/tests/unit/containers/chat/chat-item/ChatItem.spec.jsx b/tests/unit/containers/chat/chat-item/ChatItem.spec.jsx index a42b62257f..506e1f66cb 100644 --- a/tests/unit/containers/chat/chat-item/ChatItem.spec.jsx +++ b/tests/unit/containers/chat/chat-item/ChatItem.spec.jsx @@ -2,6 +2,14 @@ import { screen, fireEvent } from '@testing-library/react' import { renderWithProviders } from '~tests/test-utils' import ChatItem from '~/containers/chat/chat-item/ChatItem' +const mockChatContext = { + setCurrentChatId: vi.fn() +} + +vi.mock('~/context/chat-context', () => ({ + useChatContext: () => mockChatContext +})) + const user = { _id: '644e6b1668cc37f543f2f37c', firstName: 'Albus', @@ -55,5 +63,6 @@ describe('ChatItem', () => { fireEvent.click(message) expect(setSelectedChat).toHaveBeenCalledWith(chat) + expect(mockChatContext.setCurrentChatId).toHaveBeenCalledWith(null) }) })