diff --git a/src/hooks/message-history.test.js b/src/hooks/message-history.test.js new file mode 100644 index 00000000..a3fb7a50 --- /dev/null +++ b/src/hooks/message-history.test.js @@ -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(); + }); + }); +});