-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: added test coverage and some nits
- Loading branch information
Maham Akif
authored and
Maham Akif
committed
Jul 30, 2024
1 parent
c1af030
commit c490c0b
Showing
4 changed files
with
143 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { logError } from '@edx/frontend-platform/logging'; | ||
import { useTranscripts } from '../hooks'; | ||
import { fetchAndAddTranscripts } from '../service'; | ||
|
||
// Mocking dependencies | ||
jest.mock('../service', () => ({ | ||
fetchAndAddTranscripts: jest.fn(), | ||
})); | ||
|
||
jest.mock('@edx/frontend-platform/logging', () => ({ | ||
logError: jest.fn(), | ||
})); | ||
|
||
describe('useTranscripts', () => { | ||
const customOptions = { | ||
showTranscripts: true, | ||
transcriptUrls: { | ||
en: 'https://example.com/transcript-en.txt', | ||
}, | ||
}; | ||
const mockPlayer = {}; | ||
|
||
it('should set isLoading to true initially if showTranscripts is true', () => { | ||
const { result } = renderHook(() => useTranscripts({ player: mockPlayer, customOptions })); | ||
expect(result.current.isLoading).toBe(true); | ||
}); | ||
|
||
it('should fetch and set textTracks and transcriptUrl correctly', async () => { | ||
const textTracks = { en: 'https://example.com/transcript-en.txt' }; | ||
fetchAndAddTranscripts.mockResolvedValueOnce(textTracks); | ||
|
||
const { result, waitForNextUpdate } = renderHook(() => useTranscripts({ player: mockPlayer, customOptions })); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(result.current.isLoading).toBe(false); | ||
expect(result.current.textTracks).toEqual(textTracks); | ||
expect(result.current.transcriptUrl).toBe(textTracks.en); | ||
}); | ||
|
||
it('should log error and set isLoading to false if fetching transcripts fails', async () => { | ||
const errorMessage = 'Error fetching transcripts'; | ||
fetchAndAddTranscripts.mockRejectedValueOnce(new Error(errorMessage)); | ||
|
||
const { result, waitForNextUpdate } = renderHook(() => useTranscripts({ player: mockPlayer, customOptions })); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(logError).toHaveBeenCalledWith(`Error fetching transcripts for player: Error: ${errorMessage}`); | ||
expect(result.current.isLoading).toBe(false); | ||
expect(result.current.textTracks).toEqual([]); | ||
expect(result.current.transcriptUrl).toBeNull(); | ||
}); | ||
|
||
it('should not fetch transcripts if showTranscripts is false', async () => { | ||
const customOptionsWithoutTranscripts = { | ||
showTranscripts: false, | ||
transcriptUrls: undefined, | ||
}; | ||
|
||
const { result } = renderHook(() => useTranscripts({ | ||
player: mockPlayer, | ||
customOptions: customOptionsWithoutTranscripts, | ||
})); | ||
|
||
expect(result.current.textTracks).toEqual([]); | ||
expect(result.current.transcriptUrl).toBeNull(); | ||
}); | ||
}); |
This file contains 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 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