Skip to content

Commit

Permalink
fix: convert stored datestring back to date + nits
Browse files Browse the repository at this point in the history
  • Loading branch information
ilee2u committed Dec 4, 2024
1 parent 6a4a2aa commit 3625ed2
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/data/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -32,5 +32,5 @@ async function fetchLearningAssistantSummary(courseId) {

export {
fetchChatResponse,
fetchLearningAssistantSummary,
fetchLearningAssistantChatSummary,
};
6 changes: 3 additions & 3 deletions src/data/api.test.js
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -16,7 +16,7 @@ describe('API', () => {
jest.restoreAllMocks();
});

describe('fetchLearningAssistantSummary()', () => {
describe('fetchLearningAssistantChatSummary()', () => {
const courseId = 'course-v1:edx+test+23';
const apiPayload = {
enabled: true,
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 1 addition & 4 deletions src/data/slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ export const initialState = {
disclosureAcknowledged: false,
sidebarIsOpen: false,
isEnabled: false,
auditTrial: {
startDate: null,
expirationDate: null,
},
auditTrial: {},
};

export const learningAssistantSlice = createSlice({
Expand Down
10 changes: 4 additions & 6 deletions src/data/thunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getAuthenticatedUser } from '@edx/frontend-platform/auth';
import trackChatBotMessageOptimizely from '../utils/optimizelyExperiment';
import {
fetchChatResponse,
fetchLearningAssistantSummary,
fetchLearningAssistantChatSummary,
} from './api';
import {
setCurrentMessage,
Expand Down Expand Up @@ -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));
Expand All @@ -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));
Expand Down
30 changes: 15 additions & 15 deletions src/data/thunks.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fetchLearningAssistantSummary } from './api';
import { fetchLearningAssistantChatSummary } from './api';

import { getLearningAssistantSummary } from './thunks';
import { getLearningAssistantChatSummary } from './thunks';

jest.mock('./api');

Expand All @@ -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 () => {
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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' }),
Expand Down
11 changes: 5 additions & 6 deletions src/widgets/Xpert.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/widgets/Xpert.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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(<Xpert courseId={courseId} contentToolsEnabled={false} unitId={unitId} />, { preloadedState: initialState });

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3625ed2

Please sign in to comment.