From 56b39357c5a7bf07e2756e64ffe4bde08ad0b74f Mon Sep 17 00:00:00 2001 From: Zacharis278 Date: Fri, 5 Jan 2024 12:14:10 -0500 Subject: [PATCH] feat: reduce unnecessary api calls --- src/exam/ExamWrapper.jsx | 5 +++- src/exam/ExamWrapper.test.jsx | 44 ++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/exam/ExamWrapper.jsx b/src/exam/ExamWrapper.jsx index 05bde2f4..68b7eee8 100644 --- a/src/exam/ExamWrapper.jsx +++ b/src/exam/ExamWrapper.jsx @@ -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 }, []); diff --git a/src/exam/ExamWrapper.test.jsx b/src/exam/ExamWrapper.test.jsx index 33e30f09..47397fef 100644 --- a/src/exam/ExamWrapper.test.jsx +++ b/src/exam/ExamWrapper.test.jsx @@ -3,7 +3,7 @@ import { Factory } from 'rosie'; import React from 'react'; import SequenceExamWrapper from './ExamWrapper'; import { store, getExamAttemptsData, startTimedExam } from '../data'; -import { render } from '../setupTest'; +import { render, waitFor } from '../setupTest'; import ExamStateProvider from '../core/ExamStateProvider'; import { ExamStatus, ExamType } from '../constants'; @@ -114,6 +114,48 @@ describe('SequenceExamWrapper', () => { 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( + + +
children
+
+
, + { 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( + + +
children
+
+
, + { 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()); + }); + it('does not take any actions if sequence item is not exam', () => { const { getByTestId } = render(