Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add course id to fetch attempts list #13

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/pages/ExamsPage/data/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export async function getCourseExams(courseId) {
return response.data;
}

export async function getExamAttempts(examId) {
const url = `${getExamsBaseUrl()}/api/v1/instructor_view/attempts?exam_id=${examId}&limit=1000000`;
export async function getExamAttempts(courseId, examId) {
const url = `${getExamsBaseUrl()}/api/v1/instructor_view/course_id/${courseId}/attempts?exam_id=${examId}&limit=1000000`;
const response = await getAuthenticatedHttpClient().get(url);
return response.data;
}
Expand Down
6 changes: 4 additions & 2 deletions src/pages/ExamsPage/data/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ describe('ExamsPage data api', () => {
describe('getExamAttempts', () => {
it('calls get on instructor view url with exam id', async () => {
axiosMock.onGet().reply(200, []);
const data = await api.getExamAttempts(examId);
expect(axiosMock.history.get[1].url).toBe('test-exams-url/api/v1/instructor_view/attempts?exam_id=0&limit=1000000');
const data = await api.getExamAttempts(courseId, examId);
expect(axiosMock.history.get[1].url).toBe(
`test-exams-url/api/v1/instructor_view/course_id/${courseId}/attempts?exam_id=0&limit=1000000`,
);
expect(data).toEqual([]);
});
});
Expand Down
6 changes: 6 additions & 0 deletions src/pages/ExamsPage/data/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createSlice } from '@reduxjs/toolkit';
import * as constants from 'data/constants';

export const initialState = {
courseId: null,
currentExamIndex: 0,
examsList: [],
attemptsList: [],
Expand All @@ -23,6 +24,10 @@ const slice = createSlice({
name: 'exams',
initialState,
reducers: {
setCourseId: (state, courseId) => ({
...state,
courseId: courseId.payload,
}),
loadExams: (state, { payload }) => ({
...state,
examsList: payload.map((exam) => ({
Expand Down Expand Up @@ -85,6 +90,7 @@ export const {
deleteExamAttempt,
modifyExamAttemptStatus,
setCurrentExam,
setCourseId,
} = slice.actions;

export const {
Expand Down
16 changes: 16 additions & 0 deletions src/pages/ExamsPage/data/reducer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('ExamsPage reducer', () => {
],
};
expect(reducer(initialState, action)).toEqual({
courseId: null,
currentExamIndex: 0,
examsList: [
{
Expand Down Expand Up @@ -72,6 +73,7 @@ describe('ExamsPage reducer', () => {
},
};
expect(reducer(initialState, action)).toEqual({
courseId: null,
currentExamIndex: 0,
examsList: [],
attemptsList: [
Expand Down Expand Up @@ -315,5 +317,19 @@ describe('ExamsPage reducer', () => {
});
});
});
describe('setCourseId', () => {
it('sets the courseId', () => {
const action = {
type: 'exams/setCourseId',
payload: 'course-v1:edX+Test+Test',
};
expect(reducer(initialState, action)).toEqual({
currentExamIndex: 0,
examsList: [],
attemptsList: [],
courseId: 'course-v1:edX+Test+Test',
});
});
});
});
});
2 changes: 2 additions & 0 deletions src/pages/ExamsPage/data/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export const courseExamsList = state => state.exams.examsList;
export const currentExam = state => state.exams.examsList[state.exams.currentExamIndex];

export const courseExamAttemptsList = state => state.exams.attemptsList;

export const courseId = state => state.exams.courseId;
6 changes: 6 additions & 0 deletions src/pages/ExamsPage/data/selectors.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const testAttempts = [{

const testState = {
exams: {
courseId: 'course-v1:edX+Test+Test',
examsList: testExams,
currentExamIndex: 1,
attemptsList: testAttempts,
Expand All @@ -51,4 +52,9 @@ describe('ExamsPage data selectors', () => {
expect(selectors.courseExamAttemptsList(testState)).toEqual(testAttempts);
});
});
describe('selectCourseId', () => {
it('should return courseId from store', () => {
expect(selectors.courseId(testState)).toEqual('course-v1:edX+Test+Test');
});
});
});
9 changes: 7 additions & 2 deletions src/pages/ExamsPage/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ export const useFetchCourseExams = () => {

export const useFetchExamAttempts = () => {
const makeNetworkRequest = reduxHooks.useMakeNetworkRequest();
const courseId = useSelector(selectors.courseId);
const dispatch = useDispatch();
return (examId) => (
makeNetworkRequest({
requestKey: RequestKeys.fetchExamAttempts,
promise: api.getExamAttempts(examId),
promise: api.getExamAttempts(courseId, examId),
onSuccess: (response) => dispatch(reducer.loadExamAttempts(response)),
})
);
Expand Down Expand Up @@ -66,7 +67,11 @@ export const useModifyExamAttempt = () => {

export const useInitializeExamsPage = (courseId) => {
const fetchCourseExams = module.useFetchCourseExams();
React.useEffect(() => { fetchCourseExams(courseId); }, []); // eslint-disable-line react-hooks/exhaustive-deps
const dispatch = useDispatch();
React.useEffect(() => {
fetchCourseExams(courseId);
dispatch(reducer.setCourseId(courseId));
}, []); // eslint-disable-line react-hooks/exhaustive-deps
};

export const useSetCurrentExam = () => {
Expand Down
8 changes: 7 additions & 1 deletion src/pages/ExamsPage/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import * as api from './data/api';
import * as hooks from './hooks';

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

jest.mock('data/redux/hooks', () => ({
Expand All @@ -23,7 +25,7 @@ describe('ExamsPage hooks', () => {
jest.restoreAllMocks();
});
describe('useInitializeExamsPage', () => {
it('calls useFetchCourseExams on component load', () => {
it('calls useFetchCourseExams and sets course id on component load', () => {
const mockFetchCourseExams = jest.fn();
jest.spyOn(hooks, 'useFetchCourseExams').mockImplementation(() => mockFetchCourseExams);
hooks.useInitializeExamsPage('course-1');
Expand All @@ -32,6 +34,10 @@ describe('ExamsPage hooks', () => {
expect(mockFetchCourseExams).not.toHaveBeenCalled();
cb();
expect(mockFetchCourseExams).toHaveBeenCalledWith('course-1');
expect(mockDispatch).toHaveBeenCalledWith({
payload: 'course-1',
type: 'exams/setCourseId',
});
});
});
describe('useFetchCourseExams', () => {
Expand Down