Skip to content

Commit 021f797

Browse files
committed
test: add a set to test useCourseUnit hook with resetXBlockPublishState functionality
1 parent e9f9d44 commit 021f797

1 file changed

Lines changed: 74 additions & 1 deletion

File tree

src/course-unit/hooks.test.jsx

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,41 @@
11
import React from 'react';
22
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';
48
import { iframeMessageTypes } from '../constants';
59

10+
import { setXBlockPublishState } from './data/slice';
11+
12+
const queryClient = new QueryClient();
613
jest.useFakeTimers();
714

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+
839
describe('useLayoutGrid', () => {
940
it('returns fullWidth layout when isUnitLibraryType is true', () => {
1041
const { result } = renderHook(() => useLayoutGrid('someCategory', true));
@@ -179,3 +210,45 @@ describe('useScrollToLastPosition', () => {
179210
expect(setStateSpy).not.toHaveBeenCalledWith(false);
180211
});
181212
});
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

Comments
 (0)