2020-10-27 06:52:40 -04:00
|
|
|
import { expect } from 'chai'
|
2021-06-07 05:51:27 -04:00
|
|
|
import {
|
|
|
|
fireEvent,
|
|
|
|
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'
|
|
|
|
import {
|
2021-04-27 03:52:58 -04:00
|
|
|
cleanUpContext,
|
2024-06-05 04:33:11 -04:00
|
|
|
renderWithEditorContext,
|
2021-02-17 05:05:33 -05:00
|
|
|
} from '../../../helpers/render-with-context'
|
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('<ChatPane />', function () {
|
2022-03-31 07:22:36 -04:00
|
|
|
const user = {
|
|
|
|
id: 'fake_user',
|
|
|
|
first_name: 'fake_user_first_name',
|
|
|
|
email: 'fake@example.com',
|
|
|
|
}
|
|
|
|
|
2022-01-11 04:17:56 -05:00
|
|
|
beforeEach(function () {
|
2022-03-31 07:22:36 -04:00
|
|
|
window.metaAttributesCache.set('ol-user', user)
|
2024-10-09 05:17:12 -04:00
|
|
|
window.metaAttributesCache.set('ol-chatEnabled', true)
|
2022-01-11 04:17:56 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
fetchMock.reset()
|
|
|
|
})
|
|
|
|
|
2020-11-24 06:58:08 -05:00
|
|
|
const testMessages = [
|
|
|
|
{
|
2021-02-03 05:23:37 -05:00
|
|
|
id: 'msg_1',
|
2020-11-24 06:58:08 -05:00
|
|
|
content: 'a message',
|
2021-02-17 05:05:33 -05:00
|
|
|
user,
|
2021-04-27 03:52:58 -04:00
|
|
|
timestamp: new Date().getTime(),
|
2020-11-24 06:58:08 -05:00
|
|
|
},
|
|
|
|
{
|
2021-02-03 05:23:37 -05:00
|
|
|
id: 'msg_2',
|
2020-11-24 06:58:08 -05:00
|
|
|
content: 'another message',
|
2021-02-17 05:05:33 -05:00
|
|
|
user,
|
2021-04-27 03:52:58 -04:00
|
|
|
timestamp: new Date().getTime(),
|
|
|
|
},
|
2020-11-24 06:58:08 -05:00
|
|
|
]
|
2020-10-27 06:52:40 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(function () {
|
2021-02-17 05:05:33 -05:00
|
|
|
fetchMock.reset()
|
|
|
|
cleanUpContext()
|
|
|
|
|
2020-10-27 06:52:40 -04:00
|
|
|
stubMathJax()
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
afterEach(function () {
|
2020-10-27 06:52:40 -04:00
|
|
|
tearDownMathJaxStubs()
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('renders multiple messages', async function () {
|
2020-11-24 06:58:08 -05:00
|
|
|
fetchMock.get(/messages/, testMessages)
|
2021-02-17 05:05:33 -05:00
|
|
|
|
2024-06-05 04:33:11 -04:00
|
|
|
renderWithEditorContext(<ChatPane />, { user })
|
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-10-27 06:52:40 -04:00
|
|
|
})
|
|
|
|
|
2021-06-07 05:51:27 -04:00
|
|
|
it('provides error message with reload button on FetchError', async function () {
|
|
|
|
fetchMock.get(/messages/, 500)
|
|
|
|
|
2024-06-05 04:33:11 -04:00
|
|
|
renderWithEditorContext(<ChatPane />, { user })
|
2021-06-07 05:51:27 -04:00
|
|
|
|
|
|
|
// should have hit a FetchError and will prompt user to reconnect
|
|
|
|
await screen.findByText('Try again')
|
|
|
|
|
|
|
|
// bring chat back up
|
|
|
|
fetchMock.reset()
|
|
|
|
fetchMock.get(/messages/, [])
|
|
|
|
|
|
|
|
const reconnectButton = screen.getByRole('button', {
|
|
|
|
name: 'Try again',
|
|
|
|
})
|
|
|
|
expect(reconnectButton).to.exist
|
|
|
|
|
|
|
|
// should now reconnect with placeholder message
|
|
|
|
fireEvent.click(reconnectButton)
|
|
|
|
await screen.findByText('Send your first message to your collaborators')
|
|
|
|
})
|
|
|
|
|
2021-04-23 05:01:16 -04:00
|
|
|
it('a loading spinner is rendered while the messages are loading, then disappears', async function () {
|
2022-01-11 04:17:56 -05:00
|
|
|
fetchMock.get(/messages/, [], { delay: 1000 })
|
2021-02-17 05:05:33 -05:00
|
|
|
|
2024-06-05 04:33:11 -04:00
|
|
|
renderWithEditorContext(<ChatPane />, { user })
|
2021-02-17 05:05:33 -05:00
|
|
|
|
2023-09-26 05:09:31 -04:00
|
|
|
// not displayed initially
|
|
|
|
expect(screen.queryByText('Loading…')).to.not.exist
|
2022-01-11 04:17:56 -05:00
|
|
|
|
2023-09-26 05:09:31 -04:00
|
|
|
// eventually displayed
|
2022-01-11 04:17:56 -05:00
|
|
|
await screen.findByText('Loading…')
|
|
|
|
|
2023-09-26 05:09:31 -04:00
|
|
|
// eventually removed when the fetch call returns
|
2020-11-24 06:58:08 -05:00
|
|
|
await waitForElementToBeRemoved(() => screen.getByText('Loading…'))
|
2020-10-27 06:52:40 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('"send your first message" placeholder', function () {
|
|
|
|
it('is rendered when there are no messages ', async function () {
|
2020-11-24 06:58:08 -05:00
|
|
|
fetchMock.get(/messages/, [])
|
2021-02-17 05:05:33 -05:00
|
|
|
|
2024-06-05 04:33:11 -04:00
|
|
|
renderWithEditorContext(<ChatPane />, { user })
|
2021-02-17 05:05:33 -05:00
|
|
|
|
2020-11-24 06:58:08 -05:00
|
|
|
await screen.findByText('Send your first message to your collaborators')
|
2020-10-27 06:52:40 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('is not rendered when messages are displayed', function () {
|
2020-11-24 06:58:08 -05:00
|
|
|
fetchMock.get(/messages/, testMessages)
|
2021-02-17 05:05:33 -05:00
|
|
|
|
2024-06-05 04:33:11 -04:00
|
|
|
renderWithEditorContext(<ChatPane />, { user })
|
2021-02-17 05:05:33 -05:00
|
|
|
|
2020-11-24 06:58:08 -05:00
|
|
|
expect(
|
|
|
|
screen.queryByText('Send your first message to your collaborators')
|
|
|
|
).to.not.exist
|
2020-10-27 06:52:40 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|