2020-10-27 06:52:40 -04:00
|
|
|
import { expect } from 'chai'
|
|
|
|
import React from 'react'
|
2020-11-24 06:58:08 -05:00
|
|
|
import { render, screen } from '@testing-library/react'
|
2020-10-27 06:52:40 -04:00
|
|
|
|
|
|
|
import Message from '../../../../../frontend/js/features/chat/components/message'
|
2021-02-22 11:21:43 -05:00
|
|
|
import { stubMathJax, tearDownMathJaxStubs } from './stubs'
|
2020-10-27 06:52:40 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('<Message />', function () {
|
2020-10-27 06:52:40 -04:00
|
|
|
const currentUser = {
|
|
|
|
id: 'fake_user',
|
|
|
|
first_name: 'fake_user_first_name',
|
|
|
|
email: 'fake@example.com'
|
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(function () {
|
2020-11-24 06:58:08 -05:00
|
|
|
window.user = currentUser
|
2020-10-27 06:52:40 -04:00
|
|
|
stubMathJax()
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
afterEach(function () {
|
2020-11-24 06:58:08 -05:00
|
|
|
delete window.user
|
2020-10-27 06:52:40 -04:00
|
|
|
tearDownMathJaxStubs()
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('renders a basic message', function () {
|
2020-10-27 06:52:40 -04:00
|
|
|
const message = {
|
|
|
|
contents: ['a message'],
|
|
|
|
user: currentUser
|
|
|
|
}
|
|
|
|
|
|
|
|
render(<Message userId={currentUser.id} message={message} />)
|
|
|
|
|
|
|
|
screen.getByText('a message')
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('renders a message with multiple contents', function () {
|
2020-10-27 06:52:40 -04:00
|
|
|
const message = {
|
|
|
|
contents: ['a message', 'another message'],
|
|
|
|
user: currentUser
|
|
|
|
}
|
|
|
|
|
|
|
|
render(<Message userId={currentUser.id} message={message} />)
|
|
|
|
|
|
|
|
screen.getByText('a message')
|
|
|
|
screen.getByText('another message')
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('renders HTML links within messages', function () {
|
2020-10-27 06:52:40 -04:00
|
|
|
const message = {
|
|
|
|
contents: [
|
|
|
|
'a message with a <a href="https://overleaf.com">link to Overleaf</a>'
|
|
|
|
],
|
|
|
|
user: currentUser
|
|
|
|
}
|
|
|
|
|
|
|
|
render(<Message userId={currentUser.id} message={message} />)
|
|
|
|
|
|
|
|
screen.getByRole('link', { name: 'https://overleaf.com' })
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when the message is from the user themselves', function () {
|
2020-10-27 06:52:40 -04:00
|
|
|
const message = {
|
|
|
|
contents: ['a message'],
|
|
|
|
user: currentUser
|
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('does not render the user name nor the email', function () {
|
2020-10-27 06:52:40 -04:00
|
|
|
render(<Message userId={currentUser.id} message={message} />)
|
|
|
|
|
|
|
|
expect(screen.queryByText(currentUser.first_name)).to.not.exist
|
|
|
|
expect(screen.queryByText(currentUser.email)).to.not.exist
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when the message is from other user', function () {
|
2020-10-27 06:52:40 -04:00
|
|
|
const otherUser = {
|
|
|
|
id: 'other_user',
|
|
|
|
first_name: 'other_user_first_name'
|
|
|
|
}
|
|
|
|
|
|
|
|
const message = {
|
|
|
|
contents: ['a message'],
|
|
|
|
user: otherUser
|
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should render the other user name', function () {
|
2020-10-27 06:52:40 -04:00
|
|
|
render(<Message userId={currentUser.id} message={message} />)
|
|
|
|
|
|
|
|
screen.getByText(otherUser.first_name)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should render the other user email when their name is not available', function () {
|
2020-10-27 06:52:40 -04:00
|
|
|
const msg = {
|
|
|
|
contents: message.contents,
|
|
|
|
user: {
|
|
|
|
id: otherUser.id,
|
|
|
|
email: 'other@example.com'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render(<Message userId={currentUser.id} message={msg} />)
|
|
|
|
|
|
|
|
expect(screen.queryByText(otherUser.first_name)).to.not.exist
|
|
|
|
screen.getByText(msg.user.email)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|