2020-10-27 06:52:40 -04:00
|
|
|
import { expect } from 'chai'
|
|
|
|
import sinon from 'sinon'
|
|
|
|
import { screen, render, fireEvent } from '@testing-library/react'
|
|
|
|
|
|
|
|
import MessageInput from '../../../../../frontend/js/features/chat/components/message-input'
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('<MessageInput />', function () {
|
2020-10-27 06:52:40 -04:00
|
|
|
let resetUnreadMessages, sendMessage
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(function () {
|
2020-10-27 06:52:40 -04:00
|
|
|
resetUnreadMessages = sinon.stub()
|
|
|
|
sendMessage = sinon.stub()
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('renders successfully', function () {
|
2020-10-27 06:52:40 -04:00
|
|
|
render(
|
|
|
|
<MessageInput
|
|
|
|
sendMessage={sendMessage}
|
|
|
|
resetUnreadMessages={resetUnreadMessages}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
2020-11-09 09:52:34 -05:00
|
|
|
screen.getByLabelText('Your Message')
|
2020-10-27 06:52:40 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('sends a message after typing and hitting enter', function () {
|
2020-10-27 06:52:40 -04:00
|
|
|
render(
|
|
|
|
<MessageInput
|
|
|
|
sendMessage={sendMessage}
|
|
|
|
resetUnreadMessages={resetUnreadMessages}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
2020-11-09 09:52:34 -05:00
|
|
|
const input = screen.getByRole('textbox')
|
2020-10-27 06:52:40 -04:00
|
|
|
|
|
|
|
fireEvent.change(input, { target: { value: 'hello world' } })
|
|
|
|
fireEvent.keyDown(input, { key: 'Enter' })
|
|
|
|
expect(sendMessage).to.be.calledOnce
|
|
|
|
expect(sendMessage).to.be.calledWith('hello world')
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('resets the number of unread messages after clicking on the input', function () {
|
2020-10-27 06:52:40 -04:00
|
|
|
render(
|
|
|
|
<MessageInput
|
|
|
|
sendMessage={sendMessage}
|
|
|
|
resetUnreadMessages={resetUnreadMessages}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
|
|
|
const input = screen.getByPlaceholderText('Your Message…')
|
|
|
|
|
|
|
|
fireEvent.click(input)
|
|
|
|
expect(resetUnreadMessages).to.be.calledOnce
|
|
|
|
})
|
|
|
|
})
|