-
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.
- Loading branch information
Maham Akif
authored and
Maham Akif
committed
Jul 19, 2024
1 parent
516a21b
commit c8621ea
Showing
30 changed files
with
1,248 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,12 @@ | ||
import { useParams } from 'react-router-dom'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { queryVideoDetail } from '../queries'; | ||
import useEnterpriseCustomer from './useEnterpriseCustomer'; | ||
|
||
export default function useVideoDetails() { | ||
const { videoUUID } = useParams(); | ||
const { data: enterpriseCustomer } = useEnterpriseCustomer(); | ||
return useQuery({ | ||
...queryVideoDetail(videoUUID, enterpriseCustomer.uuid), | ||
}); | ||
} |
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,90 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { QueryClientProvider } from '@tanstack/react-query'; | ||
import { AppContext } from '@edx/frontend-platform/react'; | ||
import { useParams } from 'react-router-dom'; | ||
import { queryClient } from '../../../../utils/tests'; | ||
import { queryVideoDetail } from '../queries'; | ||
import useVideoDetails from './useVideoDetails'; | ||
import useEnterpriseCustomer from './useEnterpriseCustomer'; | ||
import { authenticatedUserFactory, enterpriseCustomerFactory } from '../services/data/__factories__'; | ||
|
||
jest.mock('../queries', () => ({ | ||
...jest.requireActual('../queries'), | ||
queryVideoDetail: jest.fn(), | ||
})); | ||
jest.mock('react-router-dom', () => ({ | ||
...jest.requireActual('react-router-dom'), | ||
useParams: jest.fn(), | ||
})); | ||
jest.mock('./useEnterpriseCustomer'); | ||
|
||
const mockEnterpriseCustomer = enterpriseCustomerFactory(); | ||
const mockAuthenticatedUser = authenticatedUserFactory(); | ||
|
||
const mockVideoDetailsData = { | ||
uuid: 'video-uuid', | ||
title: 'My Awesome Video', | ||
description: 'This is a great video.', | ||
duration: 120, | ||
thumbnail: 'example.com/videos/images/awesome-video.png', | ||
}; | ||
|
||
describe('useVideoDetails', () => { | ||
const Wrapper = ({ children }) => ( | ||
<QueryClientProvider client={queryClient()}> | ||
<AppContext.Provider value={{ authenticatedUser: mockAuthenticatedUser }}> | ||
{children} | ||
</AppContext.Provider> | ||
</QueryClientProvider> | ||
); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
useEnterpriseCustomer.mockReturnValue({ data: mockEnterpriseCustomer }); | ||
useParams.mockReturnValue({ videoUUID: 'video-uuid' }); | ||
queryVideoDetail.mockImplementation((videoUUID, enterpriseUUID) => ({ | ||
queryKey: ['videoDetail', videoUUID, enterpriseUUID, mockVideoDetailsData], | ||
queryFn: () => Promise.resolve(mockVideoDetailsData), | ||
})); | ||
}); | ||
|
||
it('should handle resolved value correctly', async () => { | ||
const { result, waitForNextUpdate } = renderHook(() => useVideoDetails(), { wrapper: Wrapper }); | ||
await waitForNextUpdate(); | ||
|
||
expect(result.current).toEqual( | ||
expect.objectContaining({ | ||
data: mockVideoDetailsData, | ||
isLoading: false, | ||
isFetching: false, | ||
}), | ||
); | ||
}); | ||
|
||
it('should handle loading state correctly', () => { | ||
queryVideoDetail.mockImplementation(() => ({ | ||
queryKey: ['videoDetail'], | ||
queryFn: () => new Promise(() => {}), // Simulate loading | ||
})); | ||
|
||
const { result } = renderHook(() => useVideoDetails(), { wrapper: Wrapper }); | ||
|
||
expect(result.current.isLoading).toBe(true); | ||
expect(result.current.isFetching).toBe(true); | ||
}); | ||
|
||
it('should handle error state correctly', async () => { | ||
const mockError = new Error('Failed to fetch video details'); | ||
queryVideoDetail.mockImplementation(() => ({ | ||
queryKey: ['videoDetail', mockError], | ||
queryFn: () => Promise.reject(mockError), | ||
})); | ||
|
||
const { result, waitForNextUpdate } = renderHook(() => useVideoDetails(), { wrapper: Wrapper }); | ||
await waitForNextUpdate(); | ||
|
||
expect(result.current.error).toEqual(mockError); | ||
expect(result.current.isLoading).toBe(false); | ||
expect(result.current.isFetching).toBe(false); | ||
}); | ||
}); |
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
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,18 @@ | ||
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth'; | ||
import { camelCaseObject } from '@edx/frontend-platform/utils'; | ||
import { getConfig } from '@edx/frontend-platform/config'; | ||
import { logError } from '@edx/frontend-platform/logging'; | ||
import { transformVideoData } from '../../../microlearning/data/utils'; | ||
|
||
export const fetchVideoDetail = async (edxVideoID) => { | ||
const { ENTERPRISE_CATALOG_API_BASE_URL } = getConfig(); | ||
const url = `${ENTERPRISE_CATALOG_API_BASE_URL}/api/v1/videos/${edxVideoID}`; | ||
|
||
try { | ||
const result = await getAuthenticatedHttpClient().get(url); | ||
return camelCaseObject(transformVideoData(result?.data || {})); | ||
} catch (error) { | ||
logError(error); | ||
return null; | ||
} | ||
}; |
Oops, something went wrong.