2022-08-18 22:14:56 -05:00
|
|
|
import { PropsWithChildren } from 'react'
|
2022-08-22 21:57:45 -05:00
|
|
|
import { waitFor, render, screen } from '@testing-library/react'
|
2022-08-18 21:36:13 -05:00
|
|
|
import userEvent from '@testing-library/user-event'
|
2022-08-18 21:10:16 -05:00
|
|
|
import { MemoryRouter as Router, Route, Routes } from 'react-router-dom'
|
|
|
|
|
|
|
|
import { Room } from './'
|
|
|
|
|
2022-08-23 21:15:58 -05:00
|
|
|
const mockUserId = 'user-id'
|
|
|
|
const mockRoomId = 'room-123'
|
2022-08-20 14:20:51 -05:00
|
|
|
|
2022-08-19 09:42:14 -05:00
|
|
|
const mockGetUuid = jest.fn()
|
2022-08-22 21:57:45 -05:00
|
|
|
const mockMessagedSender = jest
|
|
|
|
.fn()
|
|
|
|
.mockImplementation(() => Promise.resolve([]))
|
2022-08-18 22:14:56 -05:00
|
|
|
|
|
|
|
jest.mock('trystero', () => ({
|
|
|
|
joinRoom: () => ({
|
2022-08-19 09:42:14 -05:00
|
|
|
makeAction: () => [mockMessagedSender, () => {}, () => {}],
|
2022-08-18 22:14:56 -05:00
|
|
|
ping: () => Promise.resolve(0),
|
|
|
|
leave: () => {},
|
|
|
|
getPeers: () => [],
|
|
|
|
addStream: () => [Promise.resolve()],
|
|
|
|
removeStream: () => {},
|
|
|
|
addTrack: () => [Promise.resolve()],
|
|
|
|
removeTrack: () => {},
|
|
|
|
replaceTrack: () => [Promise.resolve()],
|
|
|
|
onPeerJoin: () => {},
|
|
|
|
onPeerLeave: () => {},
|
|
|
|
onPeerStream: () => {},
|
|
|
|
onPeerTrack: () => {},
|
|
|
|
}),
|
|
|
|
}))
|
|
|
|
|
|
|
|
const RouteStub = ({ children }: PropsWithChildren) => {
|
2022-08-18 21:10:16 -05:00
|
|
|
return (
|
|
|
|
<Router initialEntries={['/public/abc123']}>
|
|
|
|
<Routes>
|
2022-08-18 22:14:56 -05:00
|
|
|
<Route path="/public/:roomId" element={children}></Route>
|
2022-08-18 21:10:16 -05:00
|
|
|
</Routes>
|
|
|
|
</Router>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-18 22:14:56 -05:00
|
|
|
jest.useFakeTimers().setSystemTime(100)
|
|
|
|
|
2022-08-18 21:10:16 -05:00
|
|
|
describe('Room', () => {
|
|
|
|
test('is available', () => {
|
2022-08-18 22:14:56 -05:00
|
|
|
render(
|
|
|
|
<RouteStub>
|
2022-08-23 21:15:58 -05:00
|
|
|
<Room userId={mockUserId} roomId={mockRoomId} />
|
2022-08-18 22:14:56 -05:00
|
|
|
</RouteStub>
|
|
|
|
)
|
2022-08-18 21:10:16 -05:00
|
|
|
})
|
2022-08-18 21:36:13 -05:00
|
|
|
|
|
|
|
test('send button is disabled', () => {
|
2022-08-18 22:14:56 -05:00
|
|
|
render(
|
|
|
|
<RouteStub>
|
2022-08-23 21:15:58 -05:00
|
|
|
<Room userId={mockUserId} roomId={mockRoomId} />
|
2022-08-18 22:14:56 -05:00
|
|
|
</RouteStub>
|
|
|
|
)
|
|
|
|
|
2022-08-27 19:06:54 -05:00
|
|
|
const sendButton = screen.getByLabelText('Send')
|
2022-08-18 21:36:13 -05:00
|
|
|
expect(sendButton).toBeDisabled()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('inputting text enabled send button', () => {
|
2022-08-18 22:14:56 -05:00
|
|
|
render(
|
|
|
|
<RouteStub>
|
2022-08-23 21:15:58 -05:00
|
|
|
<Room userId={mockUserId} roomId={mockRoomId} />
|
2022-08-18 22:14:56 -05:00
|
|
|
</RouteStub>
|
|
|
|
)
|
|
|
|
|
2022-08-27 19:06:54 -05:00
|
|
|
const sendButton = screen.getByLabelText('Send')
|
2022-08-18 21:36:13 -05:00
|
|
|
const textInput = screen.getByPlaceholderText('Your message')
|
|
|
|
userEvent.type(textInput, 'hello')
|
|
|
|
expect(sendButton).not.toBeDisabled()
|
|
|
|
})
|
|
|
|
|
2022-08-22 21:57:45 -05:00
|
|
|
test('sending a message clears the text input', async () => {
|
2022-08-18 22:14:56 -05:00
|
|
|
render(
|
|
|
|
<RouteStub>
|
2022-08-23 21:15:58 -05:00
|
|
|
<Room userId={mockUserId} roomId={mockRoomId} />
|
2022-08-18 22:14:56 -05:00
|
|
|
</RouteStub>
|
|
|
|
)
|
|
|
|
|
2022-08-27 19:06:54 -05:00
|
|
|
const sendButton = screen.getByLabelText('Send')
|
2022-08-18 21:36:13 -05:00
|
|
|
const textInput = screen.getByPlaceholderText('Your message')
|
|
|
|
userEvent.type(textInput, 'hello')
|
2022-08-22 21:57:45 -05:00
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
userEvent.click(sendButton)
|
|
|
|
})
|
|
|
|
|
2022-08-18 21:36:13 -05:00
|
|
|
expect(textInput).toHaveValue('')
|
|
|
|
})
|
2022-08-18 22:14:56 -05:00
|
|
|
|
2022-08-22 21:57:45 -05:00
|
|
|
test('message is sent to peer', async () => {
|
2022-08-18 22:14:56 -05:00
|
|
|
render(
|
|
|
|
<RouteStub>
|
2022-08-20 14:20:51 -05:00
|
|
|
<Room
|
|
|
|
getUuid={mockGetUuid.mockImplementation(() => 'abc123')}
|
2022-08-23 21:15:58 -05:00
|
|
|
userId={mockUserId}
|
|
|
|
roomId={mockRoomId}
|
2022-08-20 14:20:51 -05:00
|
|
|
/>
|
2022-08-18 22:14:56 -05:00
|
|
|
</RouteStub>
|
|
|
|
)
|
|
|
|
|
2022-08-27 19:06:54 -05:00
|
|
|
const sendButton = screen.getByLabelText('Send')
|
2022-08-18 22:14:56 -05:00
|
|
|
const textInput = screen.getByPlaceholderText('Your message')
|
|
|
|
userEvent.type(textInput, 'hello')
|
2022-08-22 21:57:45 -05:00
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
userEvent.click(sendButton)
|
|
|
|
})
|
|
|
|
|
2022-08-19 09:42:14 -05:00
|
|
|
expect(mockMessagedSender).toHaveBeenCalledWith({
|
2022-08-23 21:15:58 -05:00
|
|
|
authorId: mockUserId,
|
2022-08-19 09:42:14 -05:00
|
|
|
text: 'hello',
|
|
|
|
timeSent: 100,
|
|
|
|
id: 'abc123',
|
|
|
|
})
|
2022-08-18 22:14:56 -05:00
|
|
|
})
|
2022-08-18 21:10:16 -05:00
|
|
|
})
|