2020-10-27 06:52:40 -04:00
|
|
|
import React from 'react'
|
|
|
|
import { expect } from 'chai'
|
2020-12-14 06:44:10 -05:00
|
|
|
import { screen, waitForElementToBeRemoved } from '@testing-library/react'
|
2020-11-24 06:58:08 -05:00
|
|
|
import fetchMock from 'fetch-mock'
|
2020-10-27 06:52:40 -04:00
|
|
|
|
|
|
|
import ChatPane from '../../../../../frontend/js/features/chat/components/chat-pane'
|
2020-12-14 06:44:10 -05:00
|
|
|
import { renderWithEditorContext } from '../../../helpers/render-with-context'
|
2020-10-27 06:52:40 -04:00
|
|
|
import {
|
2020-11-24 06:58:08 -05:00
|
|
|
stubChatStore,
|
2020-10-27 06:52:40 -04:00
|
|
|
stubMathJax,
|
|
|
|
stubUIConfig,
|
2020-11-24 06:58:08 -05:00
|
|
|
tearDownChatStore,
|
2020-10-27 06:52:40 -04:00
|
|
|
tearDownMathJaxStubs,
|
|
|
|
tearDownUIConfigStubs
|
|
|
|
} from './stubs'
|
|
|
|
|
|
|
|
describe('<ChatPane />', function() {
|
|
|
|
const currentUser = {
|
|
|
|
id: 'fake_user',
|
|
|
|
first_name: 'fake_user_first_name',
|
|
|
|
email: 'fake@example.com'
|
|
|
|
}
|
|
|
|
|
2020-11-24 06:58:08 -05:00
|
|
|
const testMessages = [
|
|
|
|
{
|
|
|
|
content: 'a message',
|
|
|
|
user: currentUser,
|
|
|
|
timestamp: new Date().getTime()
|
|
|
|
},
|
|
|
|
{
|
|
|
|
content: 'another message',
|
|
|
|
user: currentUser,
|
|
|
|
timestamp: new Date().getTime()
|
|
|
|
}
|
|
|
|
]
|
2020-10-27 06:52:40 -04:00
|
|
|
|
2020-11-24 06:58:08 -05:00
|
|
|
beforeEach(function() {
|
|
|
|
stubChatStore({ user: currentUser })
|
2020-10-27 06:52:40 -04:00
|
|
|
stubUIConfig()
|
|
|
|
stubMathJax()
|
2020-11-24 06:58:08 -05:00
|
|
|
fetchMock.reset()
|
2020-10-27 06:52:40 -04:00
|
|
|
})
|
|
|
|
|
2020-11-24 06:58:08 -05:00
|
|
|
afterEach(function() {
|
|
|
|
tearDownChatStore()
|
2020-10-27 06:52:40 -04:00
|
|
|
tearDownUIConfigStubs()
|
|
|
|
tearDownMathJaxStubs()
|
2020-11-24 06:58:08 -05:00
|
|
|
fetchMock.reset()
|
2020-10-27 06:52:40 -04:00
|
|
|
})
|
|
|
|
|
2020-11-24 06:58:08 -05:00
|
|
|
it('renders multiple messages', async function() {
|
|
|
|
fetchMock.get(/messages/, testMessages)
|
2020-12-14 06:44:10 -05:00
|
|
|
// unmounting before `beforeEach` block is executed is required to prevent cleanup errors
|
|
|
|
const { unmount } = renderWithEditorContext(
|
2021-01-11 06:57:38 -05:00
|
|
|
<ChatPane resetUnreadMessages={() => {}} chatIsOpen />
|
2020-12-14 06:44:10 -05:00
|
|
|
)
|
2020-10-27 06:52:40 -04:00
|
|
|
|
2020-11-24 06:58:08 -05:00
|
|
|
await screen.findByText('a message')
|
|
|
|
await screen.findByText('another message')
|
2020-12-14 06:44:10 -05:00
|
|
|
unmount()
|
2020-10-27 06:52:40 -04:00
|
|
|
})
|
|
|
|
|
2020-11-24 06:58:08 -05:00
|
|
|
it('A loading spinner is rendered while the messages are loading, then disappears', async function() {
|
|
|
|
fetchMock.get(/messages/, [])
|
2020-12-14 06:44:10 -05:00
|
|
|
const { unmount } = renderWithEditorContext(
|
2021-01-11 06:57:38 -05:00
|
|
|
<ChatPane resetUnreadMessages={() => {}} chatIsOpen />
|
2020-12-14 06:44:10 -05:00
|
|
|
)
|
2020-11-24 06:58:08 -05:00
|
|
|
await waitForElementToBeRemoved(() => screen.getByText('Loading…'))
|
2020-12-14 06:44:10 -05:00
|
|
|
unmount()
|
2020-10-27 06:52:40 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('"send your first message" placeholder', function() {
|
2020-11-24 06:58:08 -05:00
|
|
|
it('is rendered when there are no messages ', async function() {
|
|
|
|
fetchMock.get(/messages/, [])
|
2020-12-14 06:44:10 -05:00
|
|
|
const { unmount } = renderWithEditorContext(
|
2021-01-11 06:57:38 -05:00
|
|
|
<ChatPane resetUnreadMessages={() => {}} chatIsOpen />
|
2020-12-14 06:44:10 -05:00
|
|
|
)
|
2020-11-24 06:58:08 -05:00
|
|
|
await screen.findByText('Send your first message to your collaborators')
|
2020-12-14 06:44:10 -05:00
|
|
|
unmount()
|
2020-10-27 06:52:40 -04:00
|
|
|
})
|
|
|
|
|
2020-11-24 06:58:08 -05:00
|
|
|
it('is not rendered when messages are displayed', function() {
|
|
|
|
fetchMock.get(/messages/, testMessages)
|
2020-12-14 06:44:10 -05:00
|
|
|
const { unmount } = renderWithEditorContext(
|
2021-01-11 06:57:38 -05:00
|
|
|
<ChatPane resetUnreadMessages={() => {}} chatIsOpen />
|
2020-12-14 06:44:10 -05:00
|
|
|
)
|
2020-11-24 06:58:08 -05:00
|
|
|
expect(
|
|
|
|
screen.queryByText('Send your first message to your collaborators')
|
|
|
|
).to.not.exist
|
2020-12-14 06:44:10 -05:00
|
|
|
unmount()
|
2020-10-27 06:52:40 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|