generated from openedx/frontend-template-application
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc1e941
commit 3478480
Showing
2 changed files
with
107 additions
and
3 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,100 @@ | ||
import React from 'react'; | ||
import {screen, act, waitFor} from '@testing-library/react'; | ||
Check failure on line 2 in src/components/ChatBox/index.test.jsx GitHub Actions / test
Check failure on line 2 in src/components/ChatBox/index.test.jsx GitHub Actions / test
|
||
|
||
import { render as renderComponent } from '../../utils/utils.test'; | ||
import { initialState } from '../../data/slice'; | ||
|
||
import ChatBox from '.'; | ||
|
||
const mockedAuthenticatedUser = { userId: 123 }; | ||
jest.mock('@edx/frontend-platform/auth', () => ({ | ||
getAuthenticatedUser: () => mockedAuthenticatedUser, | ||
})); | ||
|
||
const mockDispatch = jest.fn(); | ||
jest.mock('react-redux', () => ({ | ||
...jest.requireActual('react-redux'), | ||
useDispatch: () => mockDispatch, | ||
})); | ||
|
||
const defaultProps = { | ||
chatboxContainerRef: jest.fn(), | ||
}; | ||
|
||
const courseId = 'course-v1:edX+DemoX+Demo_Course'; | ||
const unitId = 'block-v1:edX+DemoX+Demo_Course+type@unit+block@unit1'; | ||
|
||
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.getByText('Today')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('message divider does not appear when all messages from today', () => { | ||
const messageList = [ | ||
{ role: 'user', content: 'hi', timestamp: +Date.now() }, | ||
{ role: 'user', content: 'hi', timestamp: +Date.now() }, | ||
]; | ||
const sliceState = { | ||
messageList, | ||
}; | ||
render(undefined, sliceState); | ||
|
||
expect(screen.getByText('Today')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('message divider shows when all messages from before today', () => { | ||
const messageList = [ | ||
{ role: 'user', content: 'hi', timestamp: +Date.now() - 2 }, | ||
{ role: 'user', content: 'hi', timestamp: +Date.now() - 2 }, | ||
]; | ||
const sliceState = { | ||
messageList, | ||
}; | ||
render(undefined, sliceState); | ||
|
||
expect(screen.getByText('Today')).toBeInTheDocument(); | ||
}); | ||
|
||
// todo: correctly divides old and new messages, check order | ||
it('message divider shows when all messages from before today', () => { | ||
const messageList = [ | ||
{ role: 'user', content: 'hi yesterday', timestamp: +Date.now() - 1 }, | ||
{ role: 'user', content: 'hi today', timestamp: +Date.now() }, | ||
]; | ||
const sliceState = { | ||
messageList, | ||
}; | ||
render(undefined, sliceState); | ||
|
||
expect(screen.getByText('Today')).toBeInTheDocument(); | ||
}); | ||
}); |