|
1 | 1 | import React from 'react'; |
2 | 2 | import { act, renderHook } from '@testing-library/react'; |
3 | | -import { useScrollToLastPosition, useLayoutGrid } from './hooks'; |
| 3 | +import { useDispatch, useSelector } from 'react-redux'; |
| 4 | +import { useNavigate, useSearchParams } from 'react-router-dom'; |
| 5 | +import { IntlProvider } from '@edx/frontend-platform/i18n'; |
| 6 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; |
| 7 | +import { useScrollToLastPosition, useLayoutGrid, useCourseUnit } from './hooks'; |
4 | 8 | import { iframeMessageTypes } from '../constants'; |
5 | 9 |
|
| 10 | +import { setXBlockPublishState } from './data/slice'; |
| 11 | + |
| 12 | +const queryClient = new QueryClient(); |
6 | 13 | jest.useFakeTimers(); |
7 | 14 |
|
| 15 | +jest.mock('react-redux', () => ({ |
| 16 | + useDispatch: jest.fn(), |
| 17 | + useSelector: jest.fn(), |
| 18 | +})); |
| 19 | + |
| 20 | +jest.mock('react-router-dom', () => ({ |
| 21 | + useNavigate: jest.fn(), |
| 22 | + useSearchParams: jest.fn(), |
| 23 | +})); |
| 24 | + |
| 25 | +jest.mock('../generic/hooks/context/hooks', () => ({ |
| 26 | + useIframe: jest.fn().mockReturnValue({ |
| 27 | + sendMessageToIframe: jest.fn(), |
| 28 | + }), |
| 29 | +})); |
| 30 | + |
| 31 | +const wrapper = ({ children }) => ( |
| 32 | + <QueryClientProvider client={queryClient}> |
| 33 | + <IntlProvider locale="en" messages={{}}> |
| 34 | + {children} |
| 35 | + </IntlProvider> |
| 36 | + </QueryClientProvider> |
| 37 | +); |
| 38 | + |
8 | 39 | describe('useLayoutGrid', () => { |
9 | 40 | it('returns fullWidth layout when isUnitLibraryType is true', () => { |
10 | 41 | const { result } = renderHook(() => useLayoutGrid('someCategory', true)); |
@@ -179,3 +210,45 @@ describe('useScrollToLastPosition', () => { |
179 | 210 | expect(setStateSpy).not.toHaveBeenCalledWith(false); |
180 | 211 | }); |
181 | 212 | }); |
| 213 | + |
| 214 | +describe('useCourseUnit', () => { |
| 215 | + const mockDispatch = jest.fn(); |
| 216 | + |
| 217 | + beforeEach(() => { |
| 218 | + useDispatch.mockReturnValue(mockDispatch); |
| 219 | + useNavigate.mockReturnValue(jest.fn()); |
| 220 | + useSearchParams.mockReturnValue([new URLSearchParams(), jest.fn()]); |
| 221 | + |
| 222 | + useSelector.mockImplementation(() => ({})); |
| 223 | + }); |
| 224 | + |
| 225 | + afterEach(() => jest.clearAllMocks()); |
| 226 | + |
| 227 | + describe('resetXBlockPublishState', () => { |
| 228 | + it('dispatches setXBlockPublishState action with false', () => { |
| 229 | + const { result } = renderHook( |
| 230 | + () => useCourseUnit({ courseId: 'test-course', blockId: 'test-block' }), |
| 231 | + { wrapper }, |
| 232 | + ); |
| 233 | + |
| 234 | + act(() => { |
| 235 | + result.current.resetXBlockPublishState(); |
| 236 | + }); |
| 237 | + |
| 238 | + const filteredCalls = mockDispatch.mock.calls.filter( |
| 239 | + ([action]) => action.type === setXBlockPublishState.type, |
| 240 | + ); |
| 241 | + expect(filteredCalls).toHaveLength(1); |
| 242 | + expect(filteredCalls[0][0]).toEqual(setXBlockPublishState(false)); |
| 243 | + }); |
| 244 | + |
| 245 | + it('is included in the hook return value', () => { |
| 246 | + const { result } = renderHook( |
| 247 | + () => useCourseUnit({ courseId: 'test-course', blockId: 'test-block' }), |
| 248 | + { wrapper }, |
| 249 | + ); |
| 250 | + |
| 251 | + expect(typeof result.current.resetXBlockPublishState).toBe('function'); |
| 252 | + }); |
| 253 | + }); |
| 254 | +}); |
0 commit comments