2021-04-23 05:01:16 -04:00
|
|
|
// Disable prop type checks for test harnesses
|
|
|
|
/* eslint-disable react/prop-types */
|
|
|
|
|
|
|
|
import { renderHook, act } from '@testing-library/react-hooks/dom'
|
|
|
|
import { expect } from 'chai'
|
2022-10-31 11:04:49 -04:00
|
|
|
import sinon from 'sinon'
|
2021-04-23 05:01:16 -04:00
|
|
|
import fetchMock from 'fetch-mock'
|
|
|
|
import EventEmitter from 'events'
|
2022-10-31 11:04:49 -04:00
|
|
|
import uuid from 'uuid'
|
2021-04-23 05:01:16 -04:00
|
|
|
import { useChatContext } from '../../../../../frontend/js/features/chat/context/chat-context'
|
|
|
|
import {
|
|
|
|
ChatProviders,
|
2021-04-27 03:52:58 -04:00
|
|
|
cleanUpContext,
|
2021-04-23 05:01:16 -04:00
|
|
|
} from '../../../helpers/render-with-context'
|
|
|
|
import { stubMathJax, tearDownMathJaxStubs } from '../components/stubs'
|
|
|
|
|
|
|
|
describe('ChatContext', function () {
|
|
|
|
const user = {
|
|
|
|
id: 'fake_user',
|
|
|
|
first_name: 'fake_user_first_name',
|
2021-04-27 03:52:58 -04:00
|
|
|
email: 'fake@example.com',
|
2021-04-23 05:01:16 -04:00
|
|
|
}
|
2022-10-31 11:04:49 -04:00
|
|
|
const uuidValue = '00000000-0000-0000-0000-000000000000'
|
2021-04-23 05:01:16 -04:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
fetchMock.reset()
|
|
|
|
cleanUpContext()
|
|
|
|
|
|
|
|
stubMathJax()
|
2022-03-31 07:22:36 -04:00
|
|
|
|
|
|
|
window.metaAttributesCache = new Map()
|
|
|
|
window.metaAttributesCache.set('ol-user', user)
|
2022-10-31 11:04:49 -04:00
|
|
|
|
|
|
|
this.stub = sinon.stub(uuid, 'v4').returns(uuidValue)
|
2021-04-23 05:01:16 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
tearDownMathJaxStubs()
|
2022-03-31 07:22:36 -04:00
|
|
|
|
|
|
|
window.metaAttributesCache = new Map()
|
2022-10-31 11:04:49 -04:00
|
|
|
this.stub.restore()
|
2021-04-23 05:01:16 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('socket connection', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
// Mock GET messages to return no messages
|
|
|
|
fetchMock.get('express:/project/:projectId/messages', [])
|
|
|
|
|
|
|
|
// Mock POST new message to return 200
|
|
|
|
fetchMock.post('express:/project/:projectId/messages', 200)
|
|
|
|
})
|
|
|
|
|
2023-04-19 03:55:25 -04:00
|
|
|
afterEach(function () {
|
|
|
|
fetchMock.reset()
|
|
|
|
})
|
|
|
|
|
2021-04-23 05:01:16 -04:00
|
|
|
it('subscribes when mounted', function () {
|
|
|
|
const socket = new EventEmitter()
|
2022-03-31 07:22:36 -04:00
|
|
|
renderChatContextHook({ socket })
|
2021-04-23 05:01:16 -04:00
|
|
|
|
|
|
|
// Assert that there is 1 listener
|
|
|
|
expect(socket.rawListeners('new-chat-message').length).to.equal(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('unsubscribes when unmounted', function () {
|
|
|
|
const socket = new EventEmitter()
|
2022-03-31 07:22:36 -04:00
|
|
|
const { unmount } = renderChatContextHook({ socket })
|
2021-04-23 05:01:16 -04:00
|
|
|
|
|
|
|
unmount()
|
|
|
|
|
|
|
|
// Assert that there is 0 listeners
|
|
|
|
expect(socket.rawListeners('new-chat-message').length).to.equal(0)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('adds received messages to the list', async function () {
|
|
|
|
// Mock socket: we only need to emit events, not mock actual connections
|
|
|
|
const socket = new EventEmitter()
|
|
|
|
const { result, waitForNextUpdate } = renderChatContextHook({
|
2021-04-27 03:52:58 -04:00
|
|
|
socket,
|
2021-04-23 05:01:16 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
// Wait until initial messages have loaded
|
|
|
|
result.current.loadInitialMessages()
|
|
|
|
await waitForNextUpdate()
|
|
|
|
|
|
|
|
// No messages shown at first
|
|
|
|
expect(result.current.messages).to.deep.equal([])
|
|
|
|
|
|
|
|
// Mock message being received from another user
|
|
|
|
socket.emit('new-chat-message', {
|
|
|
|
id: 'msg_1',
|
|
|
|
content: 'new message',
|
|
|
|
timestamp: Date.now(),
|
|
|
|
user: {
|
|
|
|
id: 'another_fake_user',
|
|
|
|
first_name: 'another_fake_user_first_name',
|
2021-04-27 03:52:58 -04:00
|
|
|
email: 'another_fake@example.com',
|
|
|
|
},
|
2021-04-23 05:01:16 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
const message = result.current.messages[0]
|
|
|
|
expect(message.id).to.equal('msg_1')
|
|
|
|
expect(message.contents).to.deep.equal(['new message'])
|
|
|
|
})
|
|
|
|
|
2023-04-19 03:55:25 -04:00
|
|
|
it('deduplicate messages from preloading', async function () {
|
|
|
|
// Mock socket: we only need to emit events, not mock actual connections
|
|
|
|
const socket = new EventEmitter()
|
|
|
|
const { result, waitForNextUpdate } = renderChatContextHook({
|
|
|
|
socket,
|
|
|
|
})
|
|
|
|
|
|
|
|
fetchMock.get(
|
|
|
|
'express:/project/:projectId/messages',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
id: 'msg_1',
|
|
|
|
content: 'new message',
|
|
|
|
timestamp: Date.now(),
|
|
|
|
user: {
|
|
|
|
id: 'another_fake_user',
|
|
|
|
first_name: 'another_fake_user_first_name',
|
|
|
|
email: 'another_fake@example.com',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
{ overwriteRoutes: true }
|
|
|
|
)
|
|
|
|
|
|
|
|
// Mock message being received from another user
|
|
|
|
socket.emit('new-chat-message', {
|
|
|
|
id: 'msg_1',
|
|
|
|
content: 'new message',
|
|
|
|
timestamp: Date.now(),
|
|
|
|
user: {
|
|
|
|
id: 'another_fake_user',
|
|
|
|
first_name: 'another_fake_user_first_name',
|
|
|
|
email: 'another_fake@example.com',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
// Check if received the message ID
|
|
|
|
expect(result.current.messages).to.have.length(1)
|
|
|
|
|
|
|
|
// Wait until initial messages have loaded
|
|
|
|
result.current.loadInitialMessages()
|
|
|
|
await waitForNextUpdate()
|
|
|
|
|
|
|
|
// Check if there are no message duplication
|
|
|
|
expect(result.current.messages).to.have.length(1)
|
|
|
|
|
|
|
|
const message = result.current.messages[0]
|
|
|
|
expect(message.id).to.equal('msg_1')
|
|
|
|
expect(message.contents).to.deep.equal(['new message'])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('deduplicate messages from websocket', async function () {
|
|
|
|
// Mock socket: we only need to emit events, not mock actual connections
|
|
|
|
const socket = new EventEmitter()
|
|
|
|
const { result, waitForNextUpdate } = renderChatContextHook({
|
|
|
|
socket,
|
|
|
|
})
|
|
|
|
|
|
|
|
fetchMock.get(
|
|
|
|
'express:/project/:projectId/messages',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
id: 'msg_1',
|
|
|
|
content: 'new message',
|
|
|
|
timestamp: Date.now(),
|
|
|
|
user: {
|
|
|
|
id: 'another_fake_user',
|
|
|
|
first_name: 'another_fake_user_first_name',
|
|
|
|
email: 'another_fake@example.com',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
{ overwriteRoutes: true }
|
|
|
|
)
|
|
|
|
|
|
|
|
// Wait until initial messages have loaded
|
|
|
|
result.current.loadInitialMessages()
|
|
|
|
await waitForNextUpdate()
|
|
|
|
|
|
|
|
// Check if received the message ID
|
|
|
|
expect(result.current.messages).to.have.length(1)
|
|
|
|
|
|
|
|
// Mock message being received from another user
|
|
|
|
socket.emit('new-chat-message', {
|
|
|
|
id: 'msg_1',
|
|
|
|
content: 'new message',
|
|
|
|
timestamp: Date.now(),
|
|
|
|
user: {
|
|
|
|
id: 'another_fake_user',
|
|
|
|
first_name: 'another_fake_user_first_name',
|
|
|
|
email: 'another_fake@example.com',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
// Check if there are no message duplication
|
|
|
|
expect(result.current.messages).to.have.length(1)
|
|
|
|
|
|
|
|
const message = result.current.messages[0]
|
|
|
|
expect(message.id).to.equal('msg_1')
|
|
|
|
expect(message.contents).to.deep.equal(['new message'])
|
|
|
|
})
|
|
|
|
|
2021-04-23 05:01:16 -04:00
|
|
|
it("doesn't add received messages from the current user if a message was just sent", async function () {
|
|
|
|
const socket = new EventEmitter()
|
|
|
|
const { result, waitForNextUpdate } = renderChatContextHook({
|
2021-04-27 03:52:58 -04:00
|
|
|
socket,
|
2021-04-23 05:01:16 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
// Wait until initial messages have loaded
|
|
|
|
result.current.loadInitialMessages()
|
|
|
|
await waitForNextUpdate()
|
|
|
|
|
|
|
|
// Send a message from the current user
|
2022-10-31 11:04:49 -04:00
|
|
|
const sentMsg = 'sent message'
|
|
|
|
result.current.sendMessage(sentMsg)
|
2021-04-23 05:01:16 -04:00
|
|
|
|
2022-10-31 11:04:49 -04:00
|
|
|
act(() => {
|
|
|
|
// Receive a message from the current user
|
|
|
|
socket.emit('new-chat-message', {
|
|
|
|
id: 'msg_1',
|
|
|
|
content: 'received message',
|
|
|
|
timestamp: Date.now(),
|
|
|
|
user,
|
|
|
|
clientId: uuidValue,
|
|
|
|
})
|
2021-04-23 05:01:16 -04:00
|
|
|
})
|
|
|
|
|
2022-10-31 11:04:49 -04:00
|
|
|
expect(result.current.messages).to.have.length(1)
|
|
|
|
|
|
|
|
const [message] = result.current.messages
|
|
|
|
|
|
|
|
expect(message.contents).to.deep.equal([sentMsg])
|
2021-04-23 05:01:16 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('adds the new message from the current user if another message was received after sending', async function () {
|
|
|
|
const socket = new EventEmitter()
|
|
|
|
const { result, waitForNextUpdate } = renderChatContextHook({
|
2021-04-27 03:52:58 -04:00
|
|
|
socket,
|
2021-04-23 05:01:16 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
// Wait until initial messages have loaded
|
|
|
|
result.current.loadInitialMessages()
|
|
|
|
await waitForNextUpdate()
|
|
|
|
|
|
|
|
// Send a message from the current user
|
2022-10-31 11:04:49 -04:00
|
|
|
const sentMsg = 'sent message from current user'
|
|
|
|
result.current.sendMessage(sentMsg)
|
2021-04-23 05:01:16 -04:00
|
|
|
|
|
|
|
const [sentMessageFromCurrentUser] = result.current.messages
|
2022-10-31 11:04:49 -04:00
|
|
|
expect(sentMessageFromCurrentUser.contents).to.deep.equal([sentMsg])
|
|
|
|
|
|
|
|
const otherMsg = 'new message from other user'
|
2021-04-23 05:01:16 -04:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
// Receive a message from another user.
|
|
|
|
socket.emit('new-chat-message', {
|
|
|
|
id: 'msg_1',
|
2022-10-31 11:04:49 -04:00
|
|
|
content: otherMsg,
|
2021-04-23 05:01:16 -04:00
|
|
|
timestamp: Date.now(),
|
|
|
|
user: {
|
|
|
|
id: 'another_fake_user',
|
|
|
|
first_name: 'another_fake_user_first_name',
|
2021-04-27 03:52:58 -04:00
|
|
|
email: 'another_fake@example.com',
|
|
|
|
},
|
2022-10-31 11:04:49 -04:00
|
|
|
clientId: '123',
|
2021-04-23 05:01:16 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
const [, messageFromOtherUser] = result.current.messages
|
2022-10-31 11:04:49 -04:00
|
|
|
expect(messageFromOtherUser.contents).to.deep.equal([otherMsg])
|
2021-04-23 05:01:16 -04:00
|
|
|
|
2022-10-31 11:04:49 -04:00
|
|
|
act(() => {
|
|
|
|
// Receive a message from the current user
|
|
|
|
socket.emit('new-chat-message', {
|
|
|
|
id: 'msg_2',
|
|
|
|
content: 'received message from current user',
|
|
|
|
timestamp: Date.now(),
|
|
|
|
user,
|
|
|
|
clientId: uuidValue,
|
|
|
|
})
|
2021-04-23 05:01:16 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
// Since the current user didn't just send a message, it is now shown
|
2022-10-31 11:04:49 -04:00
|
|
|
expect(result.current.messages).to.deep.equal([
|
|
|
|
sentMessageFromCurrentUser,
|
|
|
|
messageFromOtherUser,
|
2021-04-23 05:01:16 -04:00
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('loadInitialMessages', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
fetchMock.get('express:/project/:projectId/messages', [
|
|
|
|
{
|
|
|
|
id: 'msg_1',
|
|
|
|
content: 'a message',
|
|
|
|
user,
|
2021-04-27 03:52:58 -04:00
|
|
|
timestamp: Date.now(),
|
|
|
|
},
|
2021-04-23 05:01:16 -04:00
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('adds messages to the list', async function () {
|
2022-03-31 07:22:36 -04:00
|
|
|
const { result, waitForNextUpdate } = renderChatContextHook({})
|
2021-04-23 05:01:16 -04:00
|
|
|
|
|
|
|
result.current.loadInitialMessages()
|
|
|
|
await waitForNextUpdate()
|
|
|
|
|
|
|
|
expect(result.current.messages[0].contents).to.deep.equal(['a message'])
|
|
|
|
})
|
|
|
|
|
|
|
|
it("won't load messages a second time", async function () {
|
2022-03-31 07:22:36 -04:00
|
|
|
const { result, waitForNextUpdate } = renderChatContextHook({})
|
2021-04-23 05:01:16 -04:00
|
|
|
|
|
|
|
result.current.loadInitialMessages()
|
|
|
|
await waitForNextUpdate()
|
|
|
|
|
|
|
|
expect(result.current.initialMessagesLoaded).to.equal(true)
|
|
|
|
|
|
|
|
// Calling a second time won't do anything
|
|
|
|
result.current.loadInitialMessages()
|
|
|
|
expect(fetchMock.calls()).to.have.lengthOf(1)
|
|
|
|
})
|
2021-06-07 05:51:27 -04:00
|
|
|
|
|
|
|
it('provides an error on failure', async function () {
|
|
|
|
fetchMock.reset()
|
|
|
|
fetchMock.get('express:/project/:projectId/messages', 500)
|
2022-03-31 07:22:36 -04:00
|
|
|
const { result, waitForNextUpdate } = renderChatContextHook({})
|
2021-06-07 05:51:27 -04:00
|
|
|
|
|
|
|
result.current.loadInitialMessages()
|
|
|
|
await waitForNextUpdate()
|
|
|
|
|
|
|
|
expect(result.current.error).to.exist
|
|
|
|
expect(result.current.status).to.equal('error')
|
|
|
|
})
|
2021-04-23 05:01:16 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('loadMoreMessages', function () {
|
|
|
|
it('adds messages to the list', async function () {
|
|
|
|
// Mock a GET request for an initial message
|
|
|
|
fetchMock.getOnce('express:/project/:projectId/messages', [
|
|
|
|
{
|
|
|
|
id: 'msg_1',
|
|
|
|
content: 'first message',
|
|
|
|
user,
|
2021-04-27 03:52:58 -04:00
|
|
|
timestamp: new Date('2021-03-04T10:00:00').getTime(),
|
|
|
|
},
|
2021-04-23 05:01:16 -04:00
|
|
|
])
|
|
|
|
|
2022-03-31 07:22:36 -04:00
|
|
|
const { result, waitForNextUpdate } = renderChatContextHook({})
|
2021-04-23 05:01:16 -04:00
|
|
|
|
|
|
|
result.current.loadMoreMessages()
|
|
|
|
await waitForNextUpdate()
|
|
|
|
|
|
|
|
expect(result.current.messages[0].contents).to.deep.equal([
|
2021-04-27 03:52:58 -04:00
|
|
|
'first message',
|
2021-04-23 05:01:16 -04:00
|
|
|
])
|
|
|
|
|
|
|
|
// The before query param is not set
|
|
|
|
expect(getLastFetchMockQueryParam('before')).to.be.null
|
|
|
|
})
|
|
|
|
|
|
|
|
it('adds more messages if called a second time', async function () {
|
|
|
|
// Mock 2 GET requests, with different content
|
|
|
|
fetchMock
|
|
|
|
.getOnce(
|
|
|
|
'express:/project/:projectId/messages',
|
|
|
|
// Resolve a full "page" of messages (50)
|
|
|
|
createMessages(50, user, new Date('2021-03-04T10:00:00').getTime())
|
|
|
|
)
|
|
|
|
.getOnce(
|
|
|
|
'express:/project/:projectId/messages',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
id: 'msg_51',
|
|
|
|
content: 'message from second page',
|
|
|
|
user,
|
2021-04-27 03:52:58 -04:00
|
|
|
timestamp: new Date('2021-03-04T11:00:00').getTime(),
|
|
|
|
},
|
2021-04-23 05:01:16 -04:00
|
|
|
],
|
|
|
|
{ overwriteRoutes: false }
|
|
|
|
)
|
|
|
|
|
2022-03-31 07:22:36 -04:00
|
|
|
const { result, waitForNextUpdate } = renderChatContextHook({})
|
2021-04-23 05:01:16 -04:00
|
|
|
|
|
|
|
result.current.loadMoreMessages()
|
|
|
|
await waitForNextUpdate()
|
|
|
|
|
|
|
|
// Call a second time
|
|
|
|
result.current.loadMoreMessages()
|
|
|
|
await waitForNextUpdate()
|
|
|
|
|
|
|
|
// The second request is added to the list
|
|
|
|
// Since both messages from the same user, they are collapsed into the
|
|
|
|
// same "message"
|
|
|
|
expect(result.current.messages[0].contents).to.include(
|
|
|
|
'message from second page'
|
|
|
|
)
|
|
|
|
|
|
|
|
// The before query param for the second request matches the timestamp
|
|
|
|
// of the first message
|
|
|
|
const beforeParam = parseInt(getLastFetchMockQueryParam('before'), 10)
|
|
|
|
expect(beforeParam).to.equal(new Date('2021-03-04T10:00:00').getTime())
|
|
|
|
})
|
|
|
|
|
|
|
|
it("won't load more messages if there are no more messages", async function () {
|
|
|
|
// Mock a GET request for 49 messages. This is less the the full page size
|
|
|
|
// (50 messages), meaning that there are no further messages to be loaded
|
|
|
|
fetchMock.getOnce(
|
|
|
|
'express:/project/:projectId/messages',
|
|
|
|
createMessages(49, user)
|
|
|
|
)
|
|
|
|
|
2022-03-31 07:22:36 -04:00
|
|
|
const { result, waitForNextUpdate } = renderChatContextHook({})
|
2021-04-23 05:01:16 -04:00
|
|
|
|
|
|
|
result.current.loadMoreMessages()
|
|
|
|
await waitForNextUpdate()
|
|
|
|
|
|
|
|
expect(result.current.messages[0].contents).to.have.length(49)
|
|
|
|
|
|
|
|
result.current.loadMoreMessages()
|
|
|
|
|
|
|
|
expect(result.current.atEnd).to.be.true
|
|
|
|
expect(fetchMock.calls()).to.have.lengthOf(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('handles socket messages while loading', async function () {
|
|
|
|
// Mock GET messages so that we can control when the promise is resolved
|
|
|
|
let resolveLoadingMessages
|
|
|
|
fetchMock.get(
|
|
|
|
'express:/project/:projectId/messages',
|
|
|
|
new Promise(resolve => {
|
|
|
|
resolveLoadingMessages = resolve
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
const socket = new EventEmitter()
|
|
|
|
const { result, waitForNextUpdate } = renderChatContextHook({
|
2021-04-27 03:52:58 -04:00
|
|
|
socket,
|
2021-04-23 05:01:16 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
// Start loading messages
|
|
|
|
result.current.loadMoreMessages()
|
|
|
|
|
|
|
|
// Mock message being received from the socket while the request is in
|
|
|
|
// flight
|
|
|
|
socket.emit('new-chat-message', {
|
|
|
|
id: 'socket_msg',
|
|
|
|
content: 'socket message',
|
|
|
|
timestamp: Date.now(),
|
|
|
|
user: {
|
|
|
|
id: 'another_fake_user',
|
|
|
|
first_name: 'another_fake_user_first_name',
|
2021-04-27 03:52:58 -04:00
|
|
|
email: 'another_fake@example.com',
|
|
|
|
},
|
2021-04-23 05:01:16 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
// Resolve messages being loaded
|
|
|
|
resolveLoadingMessages([
|
|
|
|
{
|
|
|
|
id: 'fetched_msg',
|
|
|
|
content: 'loaded message',
|
|
|
|
user,
|
2021-04-27 03:52:58 -04:00
|
|
|
timestamp: Date.now(),
|
|
|
|
},
|
2021-04-23 05:01:16 -04:00
|
|
|
])
|
|
|
|
await waitForNextUpdate()
|
|
|
|
|
|
|
|
// Although the loaded message was resolved last, it appears first (since
|
|
|
|
// requested messages must have come first)
|
|
|
|
const messageContents = result.current.messages.map(
|
|
|
|
({ contents }) => contents[0]
|
|
|
|
)
|
|
|
|
expect(messageContents).to.deep.equal([
|
|
|
|
'loaded message',
|
2021-04-27 03:52:58 -04:00
|
|
|
'socket message',
|
2021-04-23 05:01:16 -04:00
|
|
|
])
|
|
|
|
})
|
2021-06-07 05:51:27 -04:00
|
|
|
|
|
|
|
it('provides an error on failures', async function () {
|
|
|
|
fetchMock.reset()
|
|
|
|
fetchMock.get('express:/project/:projectId/messages', 500)
|
2022-03-31 07:22:36 -04:00
|
|
|
const { result, waitForNextUpdate } = renderChatContextHook({})
|
2021-06-07 05:51:27 -04:00
|
|
|
|
|
|
|
result.current.loadMoreMessages()
|
|
|
|
await waitForNextUpdate()
|
|
|
|
|
|
|
|
expect(result.current.error).to.exist
|
|
|
|
expect(result.current.status).to.equal('error')
|
|
|
|
})
|
2021-04-23 05:01:16 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('sendMessage', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
// Mock GET messages to return no messages and POST new message to be
|
|
|
|
// successful
|
|
|
|
fetchMock
|
|
|
|
.get('express:/project/:projectId/messages', [])
|
|
|
|
.postOnce('express:/project/:projectId/messages', 200)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('optimistically adds the message to the list', function () {
|
2022-03-31 07:22:36 -04:00
|
|
|
const { result } = renderChatContextHook({})
|
2021-04-23 05:01:16 -04:00
|
|
|
|
|
|
|
result.current.sendMessage('sent message')
|
|
|
|
|
|
|
|
expect(result.current.messages[0].contents).to.deep.equal([
|
2021-04-27 03:52:58 -04:00
|
|
|
'sent message',
|
2021-04-23 05:01:16 -04:00
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('POSTs the message to the backend', function () {
|
2022-03-31 07:22:36 -04:00
|
|
|
const { result } = renderChatContextHook({})
|
2021-04-23 05:01:16 -04:00
|
|
|
|
|
|
|
result.current.sendMessage('sent message')
|
|
|
|
|
|
|
|
const [, { body }] = fetchMock.lastCall(
|
|
|
|
'express:/project/:projectId/messages',
|
|
|
|
'POST'
|
|
|
|
)
|
2021-12-14 05:51:33 -05:00
|
|
|
expect(JSON.parse(body)).to.deep.include({ content: 'sent message' })
|
2021-04-23 05:01:16 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it("doesn't send if the content is empty", function () {
|
2022-03-31 07:22:36 -04:00
|
|
|
const { result } = renderChatContextHook({})
|
2021-04-23 05:01:16 -04:00
|
|
|
|
|
|
|
result.current.sendMessage('')
|
|
|
|
|
|
|
|
expect(result.current.messages).to.be.empty
|
|
|
|
expect(
|
|
|
|
fetchMock.called('express:/project/:projectId/messages', {
|
2021-04-27 03:52:58 -04:00
|
|
|
method: 'post',
|
2021-04-23 05:01:16 -04:00
|
|
|
})
|
|
|
|
).to.be.false
|
|
|
|
})
|
2021-06-07 05:51:27 -04:00
|
|
|
|
|
|
|
it('provides an error on failure', async function () {
|
|
|
|
fetchMock.reset()
|
|
|
|
fetchMock
|
|
|
|
.get('express:/project/:projectId/messages', [])
|
|
|
|
.postOnce('express:/project/:projectId/messages', 500)
|
2022-03-31 07:22:36 -04:00
|
|
|
const { result, waitForNextUpdate } = renderChatContextHook({})
|
2021-06-07 05:51:27 -04:00
|
|
|
|
|
|
|
result.current.sendMessage('sent message')
|
|
|
|
await waitForNextUpdate()
|
|
|
|
|
|
|
|
expect(result.current.error).to.exist
|
|
|
|
expect(result.current.status).to.equal('error')
|
|
|
|
})
|
2021-04-23 05:01:16 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('unread messages', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
// Mock GET messages to return no messages
|
|
|
|
fetchMock.get('express:/project/:projectId/messages', [])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('increments unreadMessageCount when a new message is received', function () {
|
|
|
|
const socket = new EventEmitter()
|
2022-03-31 07:22:36 -04:00
|
|
|
const { result } = renderChatContextHook({ socket })
|
2021-04-23 05:01:16 -04:00
|
|
|
|
|
|
|
// Receive a new message from the socket
|
|
|
|
socket.emit('new-chat-message', {
|
|
|
|
id: 'msg_1',
|
|
|
|
content: 'new message',
|
|
|
|
timestamp: Date.now(),
|
2021-04-27 03:52:58 -04:00
|
|
|
user,
|
2021-04-23 05:01:16 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
expect(result.current.unreadMessageCount).to.equal(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('resets unreadMessageCount when markMessagesAsRead is called', function () {
|
|
|
|
const socket = new EventEmitter()
|
2022-03-31 07:22:36 -04:00
|
|
|
const { result } = renderChatContextHook({ socket })
|
2021-04-23 05:01:16 -04:00
|
|
|
|
|
|
|
// Receive a new message from the socket, incrementing unreadMessageCount
|
|
|
|
// by 1
|
|
|
|
socket.emit('new-chat-message', {
|
|
|
|
id: 'msg_1',
|
|
|
|
content: 'new message',
|
|
|
|
timestamp: Date.now(),
|
2021-04-27 03:52:58 -04:00
|
|
|
user,
|
2021-04-23 05:01:16 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
result.current.markMessagesAsRead()
|
|
|
|
|
|
|
|
expect(result.current.unreadMessageCount).to.equal(0)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
function renderChatContextHook(props) {
|
|
|
|
return renderHook(() => useChatContext(), {
|
|
|
|
// Wrap with ChatContext.Provider (and the other editor context providers)
|
2021-05-13 06:21:35 -04:00
|
|
|
// eslint-disable-next-line react/display-name
|
2021-04-23 05:01:16 -04:00
|
|
|
wrapper: ({ children }) => (
|
|
|
|
<ChatProviders {...props}>{children}</ChatProviders>
|
2021-04-27 03:52:58 -04:00
|
|
|
),
|
2021-04-23 05:01:16 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function createMessages(number, user, timestamp = Date.now()) {
|
|
|
|
return Array.from({ length: number }, (_m, idx) => ({
|
|
|
|
id: `msg_${idx + 1}`,
|
|
|
|
content: `message ${idx + 1}`,
|
|
|
|
user,
|
2021-04-27 03:52:58 -04:00
|
|
|
timestamp,
|
2021-04-23 05:01:16 -04:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get query param by key from the last fetchMock response
|
|
|
|
*/
|
|
|
|
function getLastFetchMockQueryParam(key) {
|
2023-09-26 05:09:19 -04:00
|
|
|
const url = fetchMock.lastUrl()
|
2021-04-23 05:01:16 -04:00
|
|
|
const { searchParams } = new URL(url, 'https://www.overleaf.com')
|
|
|
|
return searchParams.get(key)
|
|
|
|
}
|