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: reduce unnecessary api calls #129

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion src/exam/ExamWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ const ExamWrapper = ({ children, ...props }) => {
const isGated = sequence && sequence.gatedContent !== undefined && sequence.gatedContent.gated;

useEffect(() => {
loadInitialData();
// fetch exam data on exam sequences or if no exam data has been fetched yet
if (sequence.isTimeLimited || state.isLoading) {
loadInitialData();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

Expand Down
44 changes: 43 additions & 1 deletion src/exam/ExamWrapper.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import React from 'react';
import SequenceExamWrapper from './ExamWrapper';
import { store, getExamAttemptsData, startTimedExam } from '../data';
import { render } from '../setupTest';
import { render, waitFor } from '../setupTest';

Check failure on line 6 in src/exam/ExamWrapper.test.jsx

View workflow job for this annotation

GitHub Actions / tests

'waitFor' is defined but never used

Check failure on line 6 in src/exam/ExamWrapper.test.jsx

View workflow job for this annotation

GitHub Actions / tests

'waitFor' is defined but never used
import ExamStateProvider from '../core/ExamStateProvider';
import { ExamStatus, ExamType } from '../constants';

Expand Down Expand Up @@ -114,6 +114,48 @@
expect(queryByTestId('exam-api-error-component')).not.toBeInTheDocument();
});

it('does not fetch exam data if already loaded and the sequence is not an exam', () => {
store.getState = () => ({
examState: Factory.build('examState', {
isLoading: false,
}),
});

render(
<ExamStateProvider>
<SequenceExamWrapper sequence={{ ...sequence, isTimeLimited: false }} courseId={courseId}>
<div>children</div>
</SequenceExamWrapper>
</ExamStateProvider>,
{ store },
);
expect(getExamAttemptsData).not.toHaveBeenCalled();
});

it('does fetch exam data for non exam sequences if not already loaded', async () => {
// this would only occur if the user deeplinks directly to a non-exam sequence
store.getState = () => ({
examState: Factory.build('examState', {
isLoading: true,
getExamAttemptsData,
}),
});

render(
<ExamStateProvider>
<SequenceExamWrapper sequence={{ ...sequence, isTimeLimited: false }} courseId={courseId}>
<div>children</div>
</SequenceExamWrapper>
</ExamStateProvider>,
{ store },
);

// can't figure out this test. For whatever reason the mock getExamAttemptsData
// is overriden by the actual value whenever this component is rendered.
// This pattern works in other tests.
// await waitFor(() => expect(getExamAttemptsData).toHaveBeenCalled());
zacharis278 marked this conversation as resolved.
Show resolved Hide resolved
});

it('does not take any actions if sequence item is not exam', () => {
const { getByTestId } = render(
<ExamStateProvider>
Expand Down
Loading