-
Notifications
You must be signed in to change notification settings - Fork 321
fix: useExamAccess hook does not make call to fetchExamAccessToken after isExam changes. #1644
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
Merged
michaelroytman
merged 1 commit into
master
from
michaelroytman/COSMO-683-staff-unable-to-view-exams
Mar 20, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import { useState } from 'react'; | ||
| import { logError } from '@edx/frontend-platform/logging'; | ||
| import { renderHook } from '@testing-library/react-hooks'; | ||
| import { act, renderHook } from '@testing-library/react-hooks'; | ||
| import { useExamAccessToken, useFetchExamAccessToken, useIsExam } from '@edx/frontend-lib-special-exams'; | ||
|
|
||
| import { initializeMockApp } from '../../../../../setupTest'; | ||
|
|
@@ -16,10 +17,27 @@ jest.mock('@edx/frontend-lib-special-exams', () => ({ | |
|
|
||
| const id = 'test-id'; | ||
|
|
||
| // This object allows us to manipulate the value of the accessToken. | ||
| const testAccessToken = { curr: '' }; | ||
|
|
||
| const mockFetchExamAccessToken = jest.fn().mockImplementation(() => Promise.resolve()); | ||
| useFetchExamAccessToken.mockReturnValue(mockFetchExamAccessToken); | ||
|
|
||
| const testAccessToken = 'test-access-token'; | ||
| const mockUseIsExam = (initialState = false) => { | ||
| const [isExam, setIsExam] = useState(initialState); | ||
|
|
||
| // This setTimeout block is intended to replicate the case where a unit is an exam, but | ||
| // the call to fetch exam metadata has not yet completed. That is the value of isExam starts | ||
| // as false and transitions to true once the call resolves. | ||
| if (!initialState) { | ||
| setTimeout( | ||
| () => setIsExam(true), | ||
| 500, | ||
| ); | ||
| } | ||
|
|
||
| return isExam; | ||
| }; | ||
|
|
||
| describe('useExamAccess hook', () => { | ||
| beforeAll(async () => { | ||
|
|
@@ -28,13 +46,13 @@ describe('useExamAccess hook', () => { | |
| }); | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
|
|
||
| // Mock implementations from previous test runs may not have been "consumed", so reset mock implementations. | ||
| jest.useFakeTimers(); | ||
| useExamAccessToken.mockReset(); | ||
| useExamAccessToken.mockReturnValueOnce(''); | ||
| useExamAccessToken.mockReturnValueOnce(testAccessToken); | ||
|
|
||
| useIsExam.mockImplementation(() => mockUseIsExam()); | ||
| useExamAccessToken.mockImplementation(() => testAccessToken.curr); | ||
| }); | ||
| describe.only('behavior', () => { | ||
| describe('behavior', () => { | ||
| it('returns accessToken and blockAccess and doesn\'t call token API if not an exam', () => { | ||
| const { result } = renderHook(() => useExamAccess({ id })); | ||
| const { accessToken, blockAccess } = result.current; | ||
|
|
@@ -43,31 +61,60 @@ describe('useExamAccess hook', () => { | |
| expect(blockAccess).toBe(false); | ||
| expect(mockFetchExamAccessToken).not.toHaveBeenCalled(); | ||
| }); | ||
| it('returns true for blockAccess if an exam but accessToken not yet fetched', () => { | ||
| useIsExam.mockImplementation(() => (true)); | ||
| it('returns true for blockAccess if an exam but accessToken not yet fetched', async () => { | ||
| useIsExam.mockImplementation(() => mockUseIsExam(true)); | ||
|
|
||
| const { result } = renderHook(() => useExamAccess({ id })); | ||
| const { result, waitForNextUpdate } = renderHook(() => useExamAccess({ id })); | ||
| const { accessToken, blockAccess } = result.current; | ||
|
|
||
| expect(accessToken).toEqual(''); | ||
| expect(blockAccess).toBe(true); | ||
| expect(mockFetchExamAccessToken).toHaveBeenCalled(); | ||
|
|
||
| // This is to get rid of the act(...) warning. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
| await act(async () => { | ||
| await waitForNextUpdate(); | ||
| }); | ||
| }); | ||
| it('returns false for blockAccess if an exam and accessToken fetch succeeds', async () => { | ||
| useIsExam.mockImplementation(() => (true)); | ||
| useIsExam.mockImplementation(() => mockUseIsExam(true)); | ||
| const { result, waitForNextUpdate } = renderHook(() => useExamAccess({ id })); | ||
|
|
||
| // We wait for the promise to resolve and for updates to state to complete so that blockAccess is updated. | ||
| await waitForNextUpdate(); | ||
|
|
||
| const { accessToken, blockAccess } = result.current; | ||
|
|
||
| expect(accessToken).toEqual(testAccessToken); | ||
| expect(accessToken).toEqual(testAccessToken.curr); | ||
| expect(blockAccess).toBe(false); | ||
| expect(mockFetchExamAccessToken).toHaveBeenCalled(); | ||
| }); | ||
| it('in progress', async () => { | ||
| const { result, waitForNextUpdate } = renderHook(() => useExamAccess({ id })); | ||
|
|
||
| let { accessToken, blockAccess } = result.current; | ||
|
|
||
| expect(accessToken).toEqual(''); | ||
| expect(blockAccess).toBe(false); | ||
| expect(mockFetchExamAccessToken).not.toHaveBeenCalled(); | ||
|
|
||
| testAccessToken.curr = 'test-access-token'; | ||
|
|
||
| // The runAllTimers will update the value of isExam, and the waitForNextUpdate will | ||
| // wait for call to setBlockAccess in the finally clause of useEffect hook. | ||
| await act(async () => { | ||
| jest.runAllTimers(); | ||
| await waitForNextUpdate(); | ||
| }); | ||
|
|
||
| ({ accessToken, blockAccess } = result.current); | ||
|
|
||
| expect(accessToken).toEqual('test-access-token'); | ||
| expect(blockAccess).toBe(false); | ||
| expect(mockFetchExamAccessToken).toHaveBeenCalled(); | ||
| }); | ||
| it('returns false for blockAccess if an exam and accessToken fetch fails', async () => { | ||
| useIsExam.mockImplementation(() => (true)); | ||
| useIsExam.mockImplementation(() => mockUseIsExam(true)); | ||
|
|
||
| const testError = 'test-error'; | ||
| mockFetchExamAccessToken.mockImplementationOnce(() => Promise.reject(testError)); | ||
|
|
@@ -79,7 +126,7 @@ describe('useExamAccess hook', () => { | |
|
|
||
| const { accessToken, blockAccess } = result.current; | ||
|
|
||
| expect(accessToken).toEqual(testAccessToken); | ||
| expect(accessToken).toEqual(testAccessToken.curr); | ||
| expect(blockAccess).toBe(false); | ||
| expect(mockFetchExamAccessToken).toHaveBeenCalled(); | ||
| expect(logError).toHaveBeenCalledWith(testError); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice find!