generated from openedx/frontend-template-application
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add message divider between old and new messages (#67)
- Loading branch information
1 parent
55e4b80
commit 455345e
Showing
4 changed files
with
156 additions
and
2 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
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,102 @@ | ||
import React from 'react'; | ||
import { screen, act } from '@testing-library/react'; | ||
|
||
import { render as renderComponent } from '../../utils/utils.test'; | ||
import { initialState } from '../../data/slice'; | ||
|
||
import ChatBox from '.'; | ||
|
||
const mockDispatch = jest.fn(); | ||
jest.mock('react-redux', () => ({ | ||
...jest.requireActual('react-redux'), | ||
useDispatch: () => mockDispatch, | ||
})); | ||
|
||
const defaultProps = { | ||
chatboxContainerRef: jest.fn(), | ||
}; | ||
|
||
const render = async (props = {}, sliceState = {}) => { | ||
const componentProps = { | ||
...defaultProps, | ||
...props, | ||
}; | ||
|
||
const initState = { | ||
preloadedState: { | ||
learningAssistant: { | ||
...initialState, | ||
...sliceState, | ||
}, | ||
}, | ||
}; | ||
return act(async () => renderComponent( | ||
<ChatBox {...componentProps} />, | ||
initState, | ||
)); | ||
}; | ||
|
||
describe('<ChatBox />', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
it('message divider does not appear when no messages', () => { | ||
const messageList = []; | ||
const sliceState = { | ||
messageList, | ||
}; | ||
render(undefined, sliceState); | ||
|
||
expect(screen.queryByText('Today')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('message divider does not appear when all messages from today', () => { | ||
const date = new Date(); | ||
const messageList = [ | ||
{ role: 'user', content: 'hi', timestamp: date - 60 }, | ||
{ role: 'user', content: 'hello', timestamp: date }, | ||
]; | ||
const sliceState = { | ||
messageList, | ||
}; | ||
render(undefined, sliceState); | ||
|
||
expect(screen.queryByText('hi')).toBeInTheDocument(); | ||
expect(screen.queryByText('hello')).toBeInTheDocument(); | ||
expect(screen.queryByText('Today')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('message divider shows when all messages from before today', () => { | ||
const date = new Date(); | ||
const messageList = [ | ||
{ role: 'user', content: 'hi', timestamp: date.setDate(date.getDate() - 1) }, | ||
{ role: 'user', content: 'hello', timestamp: date + 1 }, | ||
]; | ||
const sliceState = { | ||
messageList, | ||
}; | ||
render(undefined, sliceState); | ||
|
||
expect(screen.queryByText('hi')).toBeInTheDocument(); | ||
expect(screen.queryByText('hello')).toBeInTheDocument(); | ||
expect(screen.queryByText('Today')).toBeInTheDocument(); | ||
}); | ||
|
||
it('correctly divides old and new messages', () => { | ||
const today = new Date(); | ||
const messageList = [ | ||
{ role: 'user', content: 'Today yesterday', timestamp: today.setDate(today.getDate() - 1) }, | ||
{ role: 'user', content: 'Today today', timestamp: +Date.now() }, | ||
]; | ||
const sliceState = { | ||
messageList, | ||
}; | ||
render(undefined, sliceState); | ||
|
||
const results = screen.getAllByText('Today', { exact: false }); | ||
expect(results.length).toBe(3); | ||
expect(results[0]).toHaveTextContent('Today yesterday'); | ||
expect(results[1]).toHaveTextContent('Today'); | ||
expect(results[2]).toHaveTextContent('Today today'); | ||
}); | ||
}); |
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,20 @@ | ||
@use '../../utils/variables'; | ||
|
||
.message-divider { | ||
display: flex; | ||
font-size: 15px; | ||
} | ||
.message-divider:before, .message-divider:after{ | ||
content: ""; | ||
flex: 1 1; | ||
border-bottom: 1px solid; | ||
margin: auto; | ||
} | ||
.message-divider:before { | ||
margin-right: 10px; | ||
margin-left: 10px; | ||
} | ||
.message-divider:after { | ||
margin-right: 10px; | ||
margin-left: 10px; | ||
} |
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,15 @@ | ||
import React from 'react'; | ||
import './MessageDivider.scss'; | ||
import PropTypes from 'prop-types'; | ||
|
||
const MessageDivider = ({ text }) => ( | ||
<div className="message-divider"> | ||
{text} | ||
</div> | ||
); | ||
|
||
MessageDivider.propTypes = { | ||
text: PropTypes.string.isRequired, | ||
}; | ||
|
||
export default MessageDivider; |