Skip to content

Commit

Permalink
fix: error when navigating exam units
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharis278 committed Aug 8, 2023
1 parent dcd6847 commit e6beffe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/courseware/course/sequence/Unit/hooks/useExamAccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const useExamAccess = ({
const [blockAccess, setBlockAccess] = useKeyedState(stateKeys.blockAccess, isExam());
React.useEffect(() => {
if (isExam()) {
return fetchExamAccess()
fetchExamAccess()
.finally(() => {
const examAccess = getExamAccess();
setAccessToken(examAccess);
Expand All @@ -26,7 +26,6 @@ const useExamAccess = ({
logError(error);
});
}
return undefined;
}, [id]);

return {
Expand Down
15 changes: 9 additions & 6 deletions src/courseware/course/sequence/Unit/hooks/useExamAccess.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { mockUseKeyedState } from '@edx/react-unit-test-utils';
import { getExamAccess, fetchExamAccess, isExam } from '@edx/frontend-lib-special-exams';
import { isEqual } from 'lodash';

import { waitFor } from '../../../../../setupTest';
import useExamAccess, { stateKeys } from './useExamAccess';

const getEffect = (prereqs) => {
Expand Down Expand Up @@ -54,27 +55,29 @@ describe('useExamAccess hook', () => {
useExamAccess({ id });
state.expectInitializedWith(stateKeys.blockAccess, true);
});
describe('effects - on id change', () => {
let cb;
describe.only('effects - on id change', () => {
let useEffectCb;
beforeEach(() => {
useExamAccess({ id });
cb = getEffect([id], React);
useEffectCb = getEffect([id], React);
});
it('does not call fetchExamAccess if not an exam', () => {
cb();
useEffectCb();
expect(fetchExamAccess).not.toHaveBeenCalled();
});
it('fetches and sets exam access if isExam', async () => {
isExam.mockReturnValueOnce(true);
await cb();
useEffectCb();
await waitFor(() => expect(fetchExamAccess).toHaveBeenCalled());
state.expectSetStateCalledWith(stateKeys.accessToken, testAccessToken);
state.expectSetStateCalledWith(stateKeys.blockAccess, false);
});
const testError = 'test-error';
it('logs error if fetchExamAccess fails', async () => {
isExam.mockReturnValueOnce(true);
fetchExamAccess.mockReturnValueOnce(Promise.reject(testError));
await cb();
useEffectCb();
await waitFor(() => expect(fetchExamAccess).toHaveBeenCalled());
expect(logError).toHaveBeenCalledWith(testError);
});
});
Expand Down

0 comments on commit e6beffe

Please sign in to comment.