2022-08-18 22:14:56 -05:00
|
|
|
import { PropsWithChildren } from 'react'
|
2022-08-18 21:36:13 -05:00
|
|
|
import { render, screen } from '@testing-library/react'
|
|
|
|
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-18 22:14:56 -05:00
|
|
|
const mockSender = jest.fn()
|
|
|
|
|
|
|
|
jest.mock('trystero', () => ({
|
|
|
|
joinRoom: () => ({
|
|
|
|
makeAction: () => [mockSender, () => {}, () => {}],
|
|
|
|
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>
|
|
|
|
<Room />
|
|
|
|
</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>
|
|
|
|
<Room />
|
|
|
|
</RouteStub>
|
|
|
|
)
|
|
|
|
|
2022-08-18 21:36:13 -05:00
|
|
|
const sendButton = screen.getByText('Send')
|
|
|
|
expect(sendButton).toBeDisabled()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('inputting text enabled send button', () => {
|
2022-08-18 22:14:56 -05:00
|
|
|
render(
|
|
|
|
<RouteStub>
|
|
|
|
<Room />
|
|
|
|
</RouteStub>
|
|
|
|
)
|
|
|
|
|
2022-08-18 21:36:13 -05:00
|
|
|
const sendButton = screen.getByText('Send')
|
|
|
|
const textInput = screen.getByPlaceholderText('Your message')
|
|
|
|
userEvent.type(textInput, 'hello')
|
|
|
|
expect(sendButton).not.toBeDisabled()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('sending a message clears the text input', () => {
|
2022-08-18 22:14:56 -05:00
|
|
|
render(
|
|
|
|
<RouteStub>
|
|
|
|
<Room />
|
|
|
|
</RouteStub>
|
|
|
|
)
|
|
|
|
|
2022-08-18 21:36:13 -05:00
|
|
|
const sendButton = screen.getByText('Send')
|
|
|
|
const textInput = screen.getByPlaceholderText('Your message')
|
|
|
|
userEvent.type(textInput, 'hello')
|
|
|
|
userEvent.click(sendButton)
|
|
|
|
expect(textInput).toHaveValue('')
|
|
|
|
})
|
2022-08-18 22:14:56 -05:00
|
|
|
|
|
|
|
test('message is sent to peer', () => {
|
|
|
|
render(
|
|
|
|
<RouteStub>
|
|
|
|
<Room />
|
|
|
|
</RouteStub>
|
|
|
|
)
|
|
|
|
|
|
|
|
const sendButton = screen.getByText('Send')
|
|
|
|
const textInput = screen.getByPlaceholderText('Your message')
|
|
|
|
userEvent.type(textInput, 'hello')
|
|
|
|
userEvent.click(sendButton)
|
|
|
|
expect(mockSender).toHaveBeenCalledWith({ text: 'hello', timeSent: 100 })
|
|
|
|
})
|
2022-08-18 21:10:16 -05:00
|
|
|
})
|