-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #197 from graasp/187/chatbox
187/chatbox
- Loading branch information
Showing
17 changed files
with
542 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { DEFAULT_FOLDER_ITEM } from './items'; | ||
import { CURRENT_USER, MEMBERS } from './members'; | ||
|
||
export const ITEM_WITH_CHATBOX_MESSAGES = { | ||
...DEFAULT_FOLDER_ITEM, | ||
id: 'adf09f5a-5688-11eb-ae93-0242ac130004', | ||
path: 'adf09f5a_5688_11eb_ae93_0242ac130004', | ||
name: 'item with chatbox messages', | ||
chat: [ | ||
{ | ||
body: 'message1', | ||
chatId: 'adf09f5a-5688-11eb-ae93-0242ac130004', | ||
createdAt: '2021-08-11T12:56:36.834Z', | ||
creator: CURRENT_USER.id, | ||
}, | ||
{ | ||
body: 'message2', | ||
chatId: 'adf09f5a-5688-11eb-ae93-0242ac130004', | ||
createdAt: '2021-09-11T12:56:36.834Z', | ||
creator: MEMBERS.BOB.id, | ||
}, | ||
], | ||
}; | ||
|
||
export const ITEM_WITHOUT_CHATBOX_MESSAGES = { | ||
...DEFAULT_FOLDER_ITEM, | ||
id: 'bdf09f5a-5688-11eb-ae93-0242ac130001', | ||
path: 'bdf09f5a_5688_11eb_ae93_0242ac130001', | ||
name: 'item without chatbox messages', | ||
chat: [], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { WebSocket } from '@graasp/websockets/test/mock-client'; | ||
import { buildItemPath } from '../../../../src/config/paths'; | ||
import { | ||
CHATBOX_ID, | ||
CHATBOX_INPUT_BOX_ID, | ||
ITEM_CHATBOX_BUTTON_ID, | ||
} from '../../../../src/config/selectors'; | ||
import { | ||
ITEM_WITHOUT_CHATBOX_MESSAGES, | ||
ITEM_WITH_CHATBOX_MESSAGES, | ||
} from '../../../fixtures/chatbox'; | ||
import { CURRENT_USER, MEMBERS } from '../../../fixtures/members'; | ||
import { WEBSOCKETS_DELAY_TIME } from '../../../support/constants'; | ||
|
||
const openChatbox = () => { | ||
cy.get(`#${ITEM_CHATBOX_BUTTON_ID}`).click(); | ||
cy.wait('@getItemChat'); | ||
}; | ||
|
||
describe('Chatbox Scenarios', () => { | ||
let client; | ||
|
||
beforeEach(() => { | ||
client = new WebSocket(); | ||
}); | ||
|
||
it('Send messages in chatbox', () => { | ||
const item = ITEM_WITH_CHATBOX_MESSAGES; | ||
cy.visitAndMockWs(buildItemPath(item.id), { items: [item] }, client); | ||
|
||
// open chatbox | ||
openChatbox(); | ||
// check the chatbox displays the already saved messages | ||
for (const msg of item.chat) { | ||
cy.get(`#${CHATBOX_ID}`).should('contain', msg.body); | ||
} | ||
|
||
// send message | ||
const message = 'a new message'; | ||
cy.get(`#${CHATBOX_ID} #${CHATBOX_INPUT_BOX_ID} input`).type(message); | ||
cy.get(`#${CHATBOX_ID} #${CHATBOX_INPUT_BOX_ID} button`).click(); | ||
cy.wait('@postItemChatMessage').then(({ request: { body } }) => { | ||
expect(body.body).to.equal(message); | ||
|
||
// mock websocket response | ||
client.receive({ | ||
realm: 'notif', | ||
type: 'update', | ||
topic: 'chat/item', | ||
channel: item.id, | ||
body: { | ||
kind: 'item', | ||
op: 'publish', | ||
message: { | ||
creator: CURRENT_USER.id, | ||
chatId: item.id, | ||
body: message, | ||
}, | ||
}, | ||
}); | ||
cy.wait(WEBSOCKETS_DELAY_TIME); | ||
|
||
// check the new message is visible | ||
cy.get(`#${CHATBOX_ID}`).should('contain', message); | ||
}); | ||
}); | ||
|
||
it('Receive messages in chatbox from websockets', () => { | ||
const item = ITEM_WITHOUT_CHATBOX_MESSAGES; | ||
cy.visitAndMockWs(buildItemPath(item.id), { items: [item] }, client); | ||
|
||
openChatbox(); | ||
|
||
// check websocket: the chatbox displays someone else's message | ||
const bobMessage = 'a message from bob'; | ||
cy.get(`#${CHATBOX_ID}`).then(() => { | ||
client.receive({ | ||
realm: 'notif', | ||
type: 'update', | ||
topic: 'chat/item', | ||
channel: item.id, | ||
body: { | ||
kind: 'item', | ||
op: 'publish', | ||
message: { | ||
creator: MEMBERS.BOB.id, | ||
chatId: item.id, | ||
body: bobMessage, | ||
}, | ||
}, | ||
}); | ||
cy.wait(WEBSOCKETS_DELAY_TIME); | ||
|
||
// check the new message is visible | ||
cy.get(`#${CHATBOX_ID}`).should('contain', bobMessage); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Typography } from '@material-ui/core'; | ||
import { useTranslation } from 'react-i18next'; | ||
import GraaspChatbox from '@graasp/chatbox'; | ||
import { MUTATION_KEYS } from '@graasp/query-client'; | ||
import { Map } from 'immutable'; | ||
import { Loader } from '@graasp/ui'; | ||
import { hooks, useMutation } from '../../config/queryClient'; | ||
import { HEADER_HEIGHT } from '../../config/constants'; | ||
import { CHATBOX_INPUT_BOX_ID, CHATBOX_ID } from '../../config/selectors'; | ||
|
||
const { useItemChat, useCurrentMember } = hooks; | ||
|
||
const Chatbox = ({ item }) => { | ||
const { t } = useTranslation(); | ||
const { data: chat, isLoading: isChatLoading } = useItemChat(item.get('id')); | ||
const { data: currentMember, isLoadingCurrentMember } = useCurrentMember(); | ||
const { mutate: sendMessage } = useMutation( | ||
MUTATION_KEYS.POST_ITEM_CHAT_MESSAGE, | ||
); | ||
|
||
const renderChatbox = () => { | ||
if (isChatLoading || isLoadingCurrentMember) { | ||
return <Loader />; | ||
} | ||
|
||
return ( | ||
<GraaspChatbox | ||
id={CHATBOX_ID} | ||
sendMessageBoxId={CHATBOX_INPUT_BOX_ID} | ||
currentMember={currentMember} | ||
chatId={item.get('id')} | ||
messages={chat?.get('messages')} | ||
height={window.innerHeight - HEADER_HEIGHT * 2} | ||
sendMessageFunction={sendMessage} | ||
/> | ||
); | ||
}; | ||
|
||
return ( | ||
<> | ||
<Typography variant="h5">{t('Comments')}</Typography> | ||
{renderChatbox()} | ||
</> | ||
); | ||
}; | ||
|
||
Chatbox.propTypes = { | ||
item: PropTypes.instanceOf(Map).isRequired, | ||
}; | ||
|
||
export default Chatbox; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.