Skip to content

Commit

Permalink
test: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
varshamenon4 committed Nov 21, 2024
1 parent cc1e941 commit 3478480
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/components/ChatBox/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ import MessageDivider from '../MessageDivider';
const ChatBox = ({ chatboxContainerRef }) => {
const { messageList, apiIsLoading } = useSelector(state => state.learningAssistant);
const today = new Date();
const messagesBeforeToday = messageList.filter((m) => (m.timestamp.date < today.date));
const messagesToday = messageList.filter((m) => (m.timestamp.date === today.date));

return (
<div ref={chatboxContainerRef} className="flex-grow-1 scroller d-flex flex-column pb-4">
{messageList.filter((m) => (m.timestamp.date < today.date)).map(({ role, content, timestamp }) => (
{messagesBeforeToday.map(({ role, content, timestamp }) => (
<Message key={timestamp.toString()} variant={role} message={content} />
))}
<MessageDivider text="Today" />
{messageList.filter((m) => (m.timestamp.date === today.date)).map(({ role, content, timestamp }) => (
{(messageList.length !== 0 && messagesBeforeToday.length !== 0) && (
<MessageDivider text="Today" data-testid="today-divider" />
)}
{messagesToday.map(({ role, content, timestamp }) => (
<Message key={timestamp.toString()} variant={role} message={content} />
))}
{apiIsLoading && (
Expand Down
100 changes: 100 additions & 0 deletions src/components/ChatBox/index.test.jsx
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

View workflow job for this annotation

GitHub Actions / test

A space is required after '{'

Check failure on line 2 in src/components/ChatBox/index.test.jsx

View workflow job for this annotation

GitHub Actions / test

'waitFor' is defined but never used

Check failure on line 2 in src/components/ChatBox/index.test.jsx

View workflow job for this annotation

GitHub Actions / test

A space is required before '}'

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';

Check failure on line 24 in src/components/ChatBox/index.test.jsx

View workflow job for this annotation

GitHub Actions / test

'courseId' is assigned a value but never used
const unitId = 'block-v1:edX+DemoX+Demo_Course+type@unit+block@unit1';

Check failure on line 25 in src/components/ChatBox/index.test.jsx

View workflow job for this annotation

GitHub Actions / test

'unitId' is assigned a value but never used

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();
});
});

0 comments on commit 3478480

Please sign in to comment.