diff --git a/src/__mocks__/trystero.ts b/src/__mocks__/trystero.ts index 8c7bd5e..1d6339c 100644 --- a/src/__mocks__/trystero.ts +++ b/src/__mocks__/trystero.ts @@ -5,7 +5,7 @@ export const joinRoom: typeof trysteroJoinRoom = ( _roomId: string ) => { const room: Room = { - makeAction: () => [() => {}, () => {}, () => {}], + makeAction: () => [() => Promise.resolve([]), () => {}, () => {}], ping: () => Promise.resolve(0), leave: () => {}, getPeers: () => [], diff --git a/src/components/ChatTranscript/ChatTranscript.tsx b/src/components/ChatTranscript/ChatTranscript.tsx index e326255..64dedd8 100644 --- a/src/components/ChatTranscript/ChatTranscript.tsx +++ b/src/components/ChatTranscript/ChatTranscript.tsx @@ -1,6 +1,6 @@ import Typography from '@mui/material/Typography' -import { UnsentMessage, ReceivedMessage } from 'models/chat' +import { isMessageReceived, UnsentMessage, ReceivedMessage } from 'models/chat' export interface ChatTranscriptProps { messageLog: Array @@ -10,24 +10,35 @@ export interface ChatTranscriptProps { export const ChatTranscript = ({ messageLog, userId }: ChatTranscriptProps) => { return (
- {messageLog.map((message, idx) => ( -
- - {message.text} - -
- ))} + {messageLog.map(message => { + let backgroundColor: string + + if (message.authorId === userId) { + backgroundColor = isMessageReceived(message) + ? 'primary.dark' + : 'primary.main' + } else { + backgroundColor = 'grey.700' + } + + return ( +
+ + {message.text} + +
+ ) + })}
) } diff --git a/src/components/Room/Room.test.tsx b/src/components/Room/Room.test.tsx index 482e179..5a0503c 100644 --- a/src/components/Room/Room.test.tsx +++ b/src/components/Room/Room.test.tsx @@ -1,5 +1,5 @@ import { PropsWithChildren } from 'react' -import { render, screen } from '@testing-library/react' +import { waitFor, render, screen } from '@testing-library/react' import userEvent from '@testing-library/user-event' import { MemoryRouter as Router, Route, Routes } from 'react-router-dom' @@ -8,7 +8,9 @@ import { Room } from './' const stubUserId = 'user-id' const mockGetUuid = jest.fn() -const mockMessagedSender = jest.fn() +const mockMessagedSender = jest + .fn() + .mockImplementation(() => Promise.resolve([])) jest.mock('trystero', () => ({ joinRoom: () => ({ @@ -73,7 +75,7 @@ describe('Room', () => { expect(sendButton).not.toBeDisabled() }) - test('sending a message clears the text input', () => { + test('sending a message clears the text input', async () => { render( @@ -83,11 +85,15 @@ describe('Room', () => { const sendButton = screen.getByText('Send') const textInput = screen.getByPlaceholderText('Your message') userEvent.type(textInput, 'hello') - userEvent.click(sendButton) + + await waitFor(() => { + userEvent.click(sendButton) + }) + expect(textInput).toHaveValue('') }) - test('message is sent to peer', () => { + test('message is sent to peer', async () => { render( { const sendButton = screen.getByText('Send') const textInput = screen.getByPlaceholderText('Your message') userEvent.type(textInput, 'hello') - userEvent.click(sendButton) + + await waitFor(() => { + userEvent.click(sendButton) + }) + expect(mockMessagedSender).toHaveBeenCalledWith({ authorId: stubUserId, text: 'hello', diff --git a/src/components/Room/Room.tsx b/src/components/Room/Room.tsx index 25303d7..0baf3ed 100644 --- a/src/components/Room/Room.tsx +++ b/src/components/Room/Room.tsx @@ -24,6 +24,7 @@ export function Room({ }: RoomProps) { const { roomId = '' } = useParams() + const [isMessageSending, setIsMessageSending] = useState(false) const [textMessage, setTextMessage] = useState('') const [messageLog, setMessageLog] = useState< Array @@ -46,7 +47,7 @@ export function Room({ setTextMessage(value) } - const handleMessageSubmit = ( + const handleMessageSubmit = async ( event: React.SyntheticEvent ) => { event.preventDefault() @@ -58,10 +59,16 @@ export function Room({ id: getUuid(), } - sendMessage(unsentMessage) - setTextMessage('') + setIsMessageSending(true) setMessageLog([...messageLog, unsentMessage]) + await sendMessage(unsentMessage) + + setMessageLog([ + ...messageLog, + { ...unsentMessage, timeReceived: Date.now() }, + ]) + setIsMessageSending(false) } receiveMessage(message => { @@ -85,7 +92,7 @@ export function Room({