Skip to content

Commit

Permalink
chore: Added coverage to useMessageHistory() hook
Browse files Browse the repository at this point in the history
  • Loading branch information
rijuma committed Nov 12, 2024
1 parent 586b83a commit 6498378
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/hooks/message-history.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { renderHook } from '@testing-library/react-hooks';

import { useSelector } from 'react-redux';
import { useMessageHistory } from './message-history';
import { getLearningAssistantMessageHistory } from '../data/thunks';

const mockDispatch = jest.fn();
jest.mock('react-redux', () => ({
...jest.requireActual('react-redux'),
useSelector: jest.fn(),
useDispatch: () => mockDispatch,
}));

jest.mock('../data/thunks');

describe('Learning Assistant Message History Hooks', () => {
afterEach(() => {
jest.resetAllMocks();
});

describe('useMessageHistory()', () => {
let hook;
const fakeCourseId = 'course-v1:edx+test+23';

const renderTestHook = (courseId, isEnabled) => {
const mockedStoreState = { learningAssistant: { isEnabled } };
useSelector.mockImplementation(selector => selector(mockedStoreState));
hook = renderHook(() => useMessageHistory(courseId));
return hook;
};

it('should dispatch getLearningAssistantMessageHistory() with the chat history', () => {
renderTestHook(fakeCourseId, true);

expect(mockDispatch).toHaveBeenCalledTimes(1);
expect(getLearningAssistantMessageHistory).toHaveBeenCalledWith(fakeCourseId);
});

it('should NOT dispatch getLearningAssistantMessageHistory() when disabled', () => {
renderTestHook(fakeCourseId, false);

expect(mockDispatch).not.toHaveBeenCalled();
expect(getLearningAssistantMessageHistory).not.toHaveBeenCalled();
});

it('should NOT dispatch getLearningAssistantMessageHistory() with no courseId', () => {
renderTestHook(null, true);

expect(mockDispatch).not.toHaveBeenCalled();
expect(getLearningAssistantMessageHistory).not.toHaveBeenCalled();
});
});
});

0 comments on commit 6498378

Please sign in to comment.