From 3625ed2e1caf023b56c3195c03af74dbaa346abf Mon Sep 17 00:00:00 2001 From: ilee2u Date: Wed, 4 Dec 2024 14:17:51 -0500 Subject: [PATCH] fix: convert stored datestring back to date + nits --- src/data/api.js | 4 ++-- src/data/api.test.js | 6 +++--- src/data/slice.js | 5 +---- src/data/thunks.js | 10 ++++------ src/data/thunks.test.js | 30 +++++++++++++++--------------- src/widgets/Xpert.jsx | 11 +++++------ src/widgets/Xpert.test.jsx | 6 +++--- 7 files changed, 33 insertions(+), 39 deletions(-) diff --git a/src/data/api.js b/src/data/api.js index 09f04b9b..0f90283f 100644 --- a/src/data/api.js +++ b/src/data/api.js @@ -23,7 +23,7 @@ async function fetchChatResponse(courseId, messageList, unitId, customQueryParam return data; } -async function fetchLearningAssistantSummary(courseId) { +async function fetchLearningAssistantChatSummary(courseId) { const url = new URL(`${getConfig().CHAT_RESPONSE_URL}/${courseId}/chat-summary`); const { data } = await getAuthenticatedHttpClient().get(url.href); @@ -32,5 +32,5 @@ async function fetchLearningAssistantSummary(courseId) { export { fetchChatResponse, - fetchLearningAssistantSummary, + fetchLearningAssistantChatSummary, }; diff --git a/src/data/api.test.js b/src/data/api.test.js index da61e169..d685150d 100644 --- a/src/data/api.test.js +++ b/src/data/api.test.js @@ -1,7 +1,7 @@ /* eslint-disable no-import-assign */ import * as auth from '@edx/frontend-platform/auth'; -import { fetchLearningAssistantSummary } from './api'; +import { fetchLearningAssistantChatSummary } from './api'; jest.mock('@edx/frontend-platform/auth'); @@ -16,7 +16,7 @@ describe('API', () => { jest.restoreAllMocks(); }); - describe('fetchLearningAssistantSummary()', () => { + describe('fetchLearningAssistantChatSummary()', () => { const courseId = 'course-v1:edx+test+23'; const apiPayload = { enabled: true, @@ -50,7 +50,7 @@ describe('API', () => { }); it('should call the endpoint and process the results', async () => { - const response = await fetchLearningAssistantSummary(courseId); + const response = await fetchLearningAssistantChatSummary(courseId); expect(response).toEqual(apiPayload); expect(mockGet).toHaveBeenCalledTimes(1); diff --git a/src/data/slice.js b/src/data/slice.js index 39e06ec7..91982422 100644 --- a/src/data/slice.js +++ b/src/data/slice.js @@ -11,10 +11,7 @@ export const initialState = { disclosureAcknowledged: false, sidebarIsOpen: false, isEnabled: false, - auditTrial: { - startDate: null, - expirationDate: null, - }, + auditTrial: {}, }; export const learningAssistantSlice = createSlice({ diff --git a/src/data/thunks.js b/src/data/thunks.js index 037be51e..fb84955c 100644 --- a/src/data/thunks.js +++ b/src/data/thunks.js @@ -4,7 +4,7 @@ import { getAuthenticatedUser } from '@edx/frontend-platform/auth'; import trackChatBotMessageOptimizely from '../utils/optimizelyExperiment'; import { fetchChatResponse, - fetchLearningAssistantSummary, + fetchLearningAssistantChatSummary, } from './api'; import { setCurrentMessage, @@ -101,12 +101,12 @@ export function updateSidebarIsOpen(isOpen) { }; } -export function getLearningAssistantSummary(courseId) { +export function getLearningAssistantChatSummary(courseId) { return async (dispatch) => { dispatch(setApiIsLoading(true)); try { - const data = await fetchLearningAssistantSummary(courseId); + const data = await fetchLearningAssistantChatSummary(courseId); // Enabled dispatch(setIsEnabled(data.enabled)); @@ -132,12 +132,10 @@ export function getLearningAssistantSummary(courseId) { const auditTrial = data.audit_trial; // If returned audit trial data is not empty - if (Object.keys(auditTrial).length !== 0) { // eslint-disable-line no-undef + if (Object.keys(auditTrial).length !== 0) { dispatch(setAuditTrial(auditTrial)); } } catch (error) { - // NOTE: When used to not show if fetching the messages failed - // But we do know since this endpoint is combined. dispatch(setApiError()); } dispatch(setApiIsLoading(false)); diff --git a/src/data/thunks.test.js b/src/data/thunks.test.js index 75d1d576..439b9e49 100644 --- a/src/data/thunks.test.js +++ b/src/data/thunks.test.js @@ -1,6 +1,6 @@ -import { fetchLearningAssistantSummary } from './api'; +import { fetchLearningAssistantChatSummary } from './api'; -import { getLearningAssistantSummary } from './thunks'; +import { getLearningAssistantChatSummary } from './thunks'; jest.mock('./api'); @@ -9,7 +9,7 @@ describe('Thunks unit tests', () => { afterEach(() => jest.resetAllMocks()); - describe('getLearningAssistantSummary()', () => { + describe('getLearningAssistantChatSummary()', () => { const courseId = 'course-v1:edx+test+23'; it('with message_history and audit_trial data returned, call all expected dispatches', async () => { @@ -33,16 +33,16 @@ describe('Thunks unit tests', () => { }, }; - fetchLearningAssistantSummary.mockResolvedValue(apiResponse); + fetchLearningAssistantChatSummary.mockResolvedValue(apiResponse); - await getLearningAssistantSummary(courseId)(dispatch); + await getLearningAssistantChatSummary(courseId)(dispatch); expect(dispatch).toHaveBeenNthCalledWith(1, { type: 'learning-assistant/setApiIsLoading', payload: true, }); - expect(fetchLearningAssistantSummary).toHaveBeenCalledWith(courseId); + expect(fetchLearningAssistantChatSummary).toHaveBeenCalledWith(courseId); expect(dispatch).toHaveBeenNthCalledWith(2, { type: 'learning-assistant/setIsEnabled', @@ -88,16 +88,16 @@ describe('Thunks unit tests', () => { }, }; - fetchLearningAssistantSummary.mockResolvedValue(apiResponse); + fetchLearningAssistantChatSummary.mockResolvedValue(apiResponse); - await getLearningAssistantSummary(courseId)(dispatch); + await getLearningAssistantChatSummary(courseId)(dispatch); expect(dispatch).toHaveBeenNthCalledWith(1, { type: 'learning-assistant/setApiIsLoading', payload: true, }); - expect(fetchLearningAssistantSummary).toHaveBeenCalledWith(courseId); + expect(fetchLearningAssistantChatSummary).toHaveBeenCalledWith(courseId); expect(dispatch).toHaveBeenNthCalledWith(2, { type: 'learning-assistant/setIsEnabled', @@ -144,16 +144,16 @@ describe('Thunks unit tests', () => { audit_trial: {}, }; - fetchLearningAssistantSummary.mockResolvedValue(apiResponse); + fetchLearningAssistantChatSummary.mockResolvedValue(apiResponse); - await getLearningAssistantSummary(courseId)(dispatch); + await getLearningAssistantChatSummary(courseId)(dispatch); expect(dispatch).toHaveBeenNthCalledWith(1, { type: 'learning-assistant/setApiIsLoading', payload: true, }); - expect(fetchLearningAssistantSummary).toHaveBeenCalledWith(courseId); + expect(fetchLearningAssistantChatSummary).toHaveBeenCalledWith(courseId); expect(dispatch).toHaveBeenNthCalledWith(2, { type: 'learning-assistant/setIsEnabled', @@ -182,16 +182,16 @@ describe('Thunks unit tests', () => { }); it('when throwing on fetching, should set the loading state and throw error', async () => { - fetchLearningAssistantSummary.mockRejectedValue('Whoopsie!'); + fetchLearningAssistantChatSummary.mockRejectedValue('Whoopsie!'); - await getLearningAssistantSummary(courseId)(dispatch); + await getLearningAssistantChatSummary(courseId)(dispatch); expect(dispatch).toHaveBeenNthCalledWith(1, { type: 'learning-assistant/setApiIsLoading', payload: true, }); - expect(fetchLearningAssistantSummary).toHaveBeenCalledWith(courseId); + expect(fetchLearningAssistantChatSummary).toHaveBeenCalledWith(courseId); expect(dispatch).not.toHaveBeenCalledWith( expect.objectContaining({ type: 'learning-assistant/setMessageList' }), diff --git a/src/widgets/Xpert.jsx b/src/widgets/Xpert.jsx index b15853b6..19f41c5e 100644 --- a/src/widgets/Xpert.jsx +++ b/src/widgets/Xpert.jsx @@ -2,15 +2,13 @@ import PropTypes from 'prop-types'; import { useEffect } from 'react'; import { useDispatch, useSelector } from 'react-redux'; -import { updateSidebarIsOpen, getLearningAssistantSummary } from '../data/thunks'; +import { updateSidebarIsOpen, getLearningAssistantChatSummary } from '../data/thunks'; import ToggleXpert from '../components/ToggleXpertButton'; import Sidebar from '../components/Sidebar'; import { ExperimentsProvider } from '../experiments'; -// import { getLearningAssistantData } from '../hooks'; const Xpert = ({ courseId, contentToolsEnabled, unitId }) => { const dispatch = useDispatch(); - // getLearningAssistantData(courseId); const { isEnabled, @@ -23,13 +21,14 @@ const Xpert = ({ courseId, contentToolsEnabled, unitId }) => { }; useEffect(() => { - dispatch(getLearningAssistantSummary(courseId)); + dispatch(getLearningAssistantChatSummary(courseId)); }, [dispatch, courseId]); // NOTE: This value can be used later on if/when we pass the enrollment mode to this component const isAuditTrialNotExpired = () => { // eslint-disable-line no-unused-vars - const auditTrialExpired = (Date.now() - auditTrial.expirationDate) > 0; - if (auditTrialExpired) { + const auditTrialExpirationDate = new Date(auditTrial.expirationDate); + + if ((Date.now() - auditTrialExpirationDate) >= 0) { return true; } return false; diff --git a/src/widgets/Xpert.test.jsx b/src/widgets/Xpert.test.jsx index cb3a99af..24819f66 100644 --- a/src/widgets/Xpert.test.jsx +++ b/src/widgets/Xpert.test.jsx @@ -45,7 +45,7 @@ const assertSidebarElementsNotInDOM = () => { beforeEach(() => { const responseMessage = createRandomResponseForTesting(); - jest.spyOn(api, 'fetchLearningAssistantSummary').mockResolvedValue({ + jest.spyOn(api, 'fetchLearningAssistantChatSummary').mockResolvedValue({ enabled: true, message_history: responseMessage, audit_trial: { @@ -63,7 +63,7 @@ beforeEach(() => { }); test('doesn\'t load if not enabled', async () => { - jest.spyOn(api, 'fetchLearningAssistantSummary').mockResolvedValue({ enabled: false }); + jest.spyOn(api, 'fetchLearningAssistantChatSummary').mockResolvedValue({ enabled: false }); render(, { preloadedState: initialState }); @@ -316,7 +316,7 @@ test('popup modal should close and display CTA', async () => { expect(screen.queryByTestId('action-message')).toBeVisible(); }); test('survey monkey survey should appear after closing sidebar', async () => { - jest.spyOn(api, 'fetchLearningAssistantSummary').mockResolvedValue({ + jest.spyOn(api, 'fetchLearningAssistantChatSummary').mockResolvedValue({ enabled: true, message_history: [ // A length >= 2 is required to load the survey