feat: assign ids to messages

This commit is contained in:
Jeremy Kahn 2022-08-19 09:42:14 -05:00
parent 5aa46ebb41
commit 09d039d806
3 changed files with 14 additions and 5 deletions

View File

@ -5,11 +5,12 @@ import { MemoryRouter as Router, Route, Routes } from 'react-router-dom'
import { Room } from './' import { Room } from './'
const mockSender = jest.fn() const mockGetUuid = jest.fn()
const mockMessagedSender = jest.fn()
jest.mock('trystero', () => ({ jest.mock('trystero', () => ({
joinRoom: () => ({ joinRoom: () => ({
makeAction: () => [mockSender, () => {}, () => {}], makeAction: () => [mockMessagedSender, () => {}, () => {}],
ping: () => Promise.resolve(0), ping: () => Promise.resolve(0),
leave: () => {}, leave: () => {},
getPeers: () => [], getPeers: () => [],
@ -87,7 +88,7 @@ describe('Room', () => {
test('message is sent to peer', () => { test('message is sent to peer', () => {
render( render(
<RouteStub> <RouteStub>
<Room /> <Room getUuid={mockGetUuid.mockImplementation(() => 'abc123')} />
</RouteStub> </RouteStub>
) )
@ -95,6 +96,10 @@ describe('Room', () => {
const textInput = screen.getByPlaceholderText('Your message') const textInput = screen.getByPlaceholderText('Your message')
userEvent.type(textInput, 'hello') userEvent.type(textInput, 'hello')
userEvent.click(sendButton) userEvent.click(sendButton)
expect(mockSender).toHaveBeenCalledWith({ text: 'hello', timeSent: 100 }) expect(mockMessagedSender).toHaveBeenCalledWith({
text: 'hello',
timeSent: 100,
id: 'abc123',
})
}) })
}) })

View File

@ -1,5 +1,6 @@
import { useState } from 'react' import { useState } from 'react'
import { useParams } from 'react-router-dom' import { useParams } from 'react-router-dom'
import { v4 as uuid } from 'uuid'
import Button from '@mui/material/Button' import Button from '@mui/material/Button'
import FormControl from '@mui/material/FormControl' import FormControl from '@mui/material/FormControl'
import Typography from '@mui/material/Typography' import Typography from '@mui/material/Typography'
@ -11,10 +12,12 @@ import { UnsentMessage, ReceivedMessage } from 'models/chat'
export interface RoomProps { export interface RoomProps {
appId?: string appId?: string
getUuid?: typeof uuid
} }
export function Room({ export function Room({
appId = `${encodeURI(window.location.origin)}_${process.env.REACT_APP_NAME}`, appId = `${encodeURI(window.location.origin)}_${process.env.REACT_APP_NAME}`,
getUuid = uuid,
}: RoomProps) { }: RoomProps) {
const { roomId = '' } = useParams() const { roomId = '' } = useParams()
@ -42,7 +45,7 @@ export function Room({
event: React.SyntheticEvent<HTMLFormElement> event: React.SyntheticEvent<HTMLFormElement>
) => { ) => {
event.preventDefault() event.preventDefault()
sendMessage({ text: textMessage, timeSent: Date.now() }) sendMessage({ text: textMessage, timeSent: Date.now(), id: getUuid() })
setTextMessage('') setTextMessage('')
} }

View File

@ -1,4 +1,5 @@
export interface UnsentMessage { export interface UnsentMessage {
id: string
text: string text: string
timeSent: number timeSent: number
} }