Skip to content

Commit

Permalink
chore: Added coverage for new API method
Browse files Browse the repository at this point in the history
  • Loading branch information
rijuma committed Nov 8, 2024
1 parent 5646e47 commit 14870d3
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/data/api.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* eslint-disable no-import-assign */
import * as auth from '@edx/frontend-platform/auth';

import { fetchLearningAssistantMessageHistory } from './api';

jest.mock('@edx/frontend-platform/auth');

const CHAT_RESPONSE_URL = 'https://some.url/endpoint';
jest.mock('@edx/frontend-platform', () => ({
getConfig: () => ({ CHAT_RESPONSE_URL }),
CHAT_RESPONSE_URL,
}));

describe('API', () => {
afterEach(() => {
jest.restoreAllMocks();
});

describe('fetchLearningAssistantMessageHistory()', () => {
const fakeCourseId = 'course-v1:edx+test+23';
const apiPayload = [
{
role: 'user',
content: 'Marco',
timestamp: '2024-11-04T19:05:07.403363Z',
},
{
role: 'assistant',
content: 'Polo',
timestamp: '2024-11-04T19:05:21.357636Z',
},
];

const fakeGet = jest.fn(async () => ({
data: apiPayload,
catch: () => {},
}));

beforeEach(() => {
auth.getAuthenticatedHttpClient = jest.fn(() => ({
get: fakeGet,
}));
});

it('should call the endpoint and process the results', async () => {
const response = await fetchLearningAssistantMessageHistory(fakeCourseId);

expect(response).toEqual(apiPayload);
expect(fakeGet).toHaveBeenCalledTimes(1);
expect(fakeGet).toHaveBeenCalledWith(`${CHAT_RESPONSE_URL}/${fakeCourseId}/history`);
});
});
});

0 comments on commit 14870d3

Please sign in to comment.