|
| 1 | +import * as hooks from './hooks'; |
| 2 | +import * as appHooks from '../../hooks'; |
| 3 | +import { thunkActions } from '../../data/redux'; |
| 4 | + |
| 5 | +jest.mock('../../data/store', () => ({ |
| 6 | + __esModule: true, |
| 7 | + default: {}, |
| 8 | +})); |
| 9 | + |
| 10 | +const mockState = { |
| 11 | + app: { |
| 12 | + learningContextId: 'course-v1:id', |
| 13 | + blockId: 'block-v1:id', |
| 14 | + }, |
| 15 | +}; |
| 16 | + |
| 17 | +jest.mock('react-redux', () => ({ |
| 18 | + ...jest.requireActual('react-redux'), |
| 19 | + useSelector: jest.fn(selector => selector(mockState)), |
| 20 | +})); |
| 21 | + |
| 22 | +jest.mock('../../hooks', () => ({ |
| 23 | + navigateTo: jest.fn(), |
| 24 | +})); |
| 25 | + |
| 26 | +jest.mock('../../data/redux', () => ({ |
| 27 | + thunkActions: { |
| 28 | + video: { |
| 29 | + uploadVideo: jest.fn(), |
| 30 | + }, |
| 31 | + }, |
| 32 | + selectors: { |
| 33 | + app: { |
| 34 | + learningContextId: jest.fn(state => state.app.learningContextId), |
| 35 | + blockId: jest.fn(state => state.app.blockId), |
| 36 | + }, |
| 37 | + }, |
| 38 | +})); |
| 39 | + |
| 40 | +describe('hooks module', () => { |
| 41 | + beforeEach(() => { |
| 42 | + jest.clearAllMocks(); |
| 43 | + }); |
| 44 | + |
| 45 | + describe('postUploadRedirect', () => { |
| 46 | + it('returns a function that navigates to the correct URL', () => { |
| 47 | + const storeState = { |
| 48 | + app: { |
| 49 | + learningContextId: 'course-v1:test', |
| 50 | + blockId: 'block-123', |
| 51 | + }, |
| 52 | + }; |
| 53 | + const redirectFn = hooks.postUploadRedirect(storeState); |
| 54 | + redirectFn('test-video-url'); |
| 55 | + |
| 56 | + expect(appHooks.navigateTo).toHaveBeenCalledWith( |
| 57 | + '/authoring/course/course-v1:test/editor/video/block-123?selectedVideoUrl=test-video-url', |
| 58 | + ); |
| 59 | + }); |
| 60 | + |
| 61 | + it('uses custom uploadType in URL if provided', () => { |
| 62 | + const storeState = { |
| 63 | + app: { |
| 64 | + learningContextId: 'course-v1:test', |
| 65 | + blockId: 'block-123', |
| 66 | + }, |
| 67 | + }; |
| 68 | + const redirectFn = hooks.postUploadRedirect(storeState, 'customType'); |
| 69 | + redirectFn('test-video-url'); |
| 70 | + |
| 71 | + expect(appHooks.navigateTo).toHaveBeenCalledWith( |
| 72 | + '/authoring/course/course-v1:test/editor/video/block-123?customType=test-video-url', |
| 73 | + ); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('useUploadVideo', () => { |
| 78 | + it('dispatches uploadVideo thunk with correct parameters', async () => { |
| 79 | + const dispatch = jest.fn(); |
| 80 | + const supportedFiles = ['file1.mp4']; |
| 81 | + const setLoadSpinner = jest.fn(); |
| 82 | + const postUploadRedirectFunction = jest.fn(); |
| 83 | + |
| 84 | + await hooks.useUploadVideo({ |
| 85 | + dispatch, |
| 86 | + supportedFiles, |
| 87 | + setLoadSpinner, |
| 88 | + postUploadRedirectFunction, |
| 89 | + }); |
| 90 | + |
| 91 | + expect(thunkActions.video.uploadVideo).toHaveBeenCalledWith({ |
| 92 | + supportedFiles, |
| 93 | + setLoadSpinner, |
| 94 | + postUploadRedirectFunction, |
| 95 | + }); |
| 96 | + expect(dispatch).toHaveBeenCalled(); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('useHistoryGoBack', () => { |
| 101 | + it('returns a function that calls window.history.back', () => { |
| 102 | + window.history.back = jest.fn(); |
| 103 | + const goBack = hooks.useHistoryGoBack(); |
| 104 | + goBack(); |
| 105 | + expect(window.history.back).toHaveBeenCalled(); |
| 106 | + }); |
| 107 | + }); |
| 108 | +}); |
0 commit comments