From 6c8fca811331f24d2d54c44b9d6958433d3ed760 Mon Sep 17 00:00:00 2001 From: Maxim Beder Date: Mon, 20 Mar 2023 15:26:45 +0100 Subject: [PATCH] test: add tests for VideoSettingsModal --- .../VideoSourceWidget/hooks.test.jsx | 42 +++++++++++++++++++ .../VideoSourceWidget/index.test.jsx | 10 ++--- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/editors/containers/VideoEditor/components/VideoSettingsModal/components/VideoSourceWidget/hooks.test.jsx b/src/editors/containers/VideoEditor/components/VideoSettingsModal/components/VideoSourceWidget/hooks.test.jsx index 34181e523..9315a1caa 100644 --- a/src/editors/containers/VideoEditor/components/VideoSettingsModal/components/VideoSourceWidget/hooks.test.jsx +++ b/src/editors/containers/VideoEditor/components/VideoSettingsModal/components/VideoSourceWidget/hooks.test.jsx @@ -1,5 +1,6 @@ import { dispatch } from 'react-redux'; import { actions } from '../../../../../../data/redux'; +import * as requests from '../../../../../../data/redux/thunkActions/requests'; import * as hooks from './hooks'; jest.mock('react-redux', () => { @@ -20,6 +21,13 @@ jest.mock('../../../../../../data/redux', () => ({ }, })); +jest.mock('../../../../../../data/redux/thunkActions/requests', () => ({ + checkTranscriptsForImport: jest.fn(), +})); + +const youtubeId = 'yOuTuBEiD'; +const youtubeUrl = `https://youtu.be/${youtubeId}`; + describe('VideoEditorHandout hooks', () => { let hook; @@ -28,6 +36,9 @@ describe('VideoEditorHandout hooks', () => { beforeEach(() => { hook = hooks.sourceHooks({ dispatch }); }); + afterEach(() => { + jest.clearAllMocks(); + }); describe('updateVideoURL', () => { it('dispatches updateField action with new videoSource', () => { hook.updateVideoURL(e); @@ -37,6 +48,37 @@ describe('VideoEditorHandout hooks', () => { }), ); }); + it('dispatches checkTranscriptsForImport request with new YouTube videoSource', () => { + e.target.value = youtubeUrl; + hook.updateVideoURL(e, 'video-id'); + expect(requests.checkTranscriptsForImport).toHaveBeenCalledWith({ + videoId: 'video-id', + youTubeId: youtubeId, + onSuccess: expect.anything(), + }); + }); + it('dispatches updateField video action when checkTranscriptsForImport onSuccess command is import', () => { + e.target.value = youtubeUrl; + hook.updateVideoURL(e, 'video-id'); + + const { onSuccess } = requests.checkTranscriptsForImport.mock.calls[0][0]; + onSuccess({ data: { command: 'import' } }); + + expect(actions.video.updateField).toHaveBeenCalledWith({ + allowTranscriptImport: true, + }); + }); + it('does not dispatch updateField video action when checkTranscriptsForImport onSuccess command is not import', () => { + e.target.value = youtubeUrl; + hook.updateVideoURL(e, 'video-id'); + + const { onSuccess } = requests.checkTranscriptsForImport.mock.calls[0][0]; + onSuccess({ data: { command: 'anything else' } }); + + expect(actions.video.updateField).not.toHaveBeenCalledWith({ + allowTranscriptImport: true, + }); + }); }); describe('updateVideoId', () => { it('dispatches updateField action with new videoId', () => { diff --git a/src/editors/containers/VideoEditor/components/VideoSettingsModal/components/VideoSourceWidget/index.test.jsx b/src/editors/containers/VideoEditor/components/VideoSettingsModal/components/VideoSourceWidget/index.test.jsx index ac38ce3d9..427cdcf07 100644 --- a/src/editors/containers/VideoEditor/components/VideoSettingsModal/components/VideoSourceWidget/index.test.jsx +++ b/src/editors/containers/VideoEditor/components/VideoSettingsModal/components/VideoSourceWidget/index.test.jsx @@ -34,7 +34,7 @@ jest.mock('../hooks', () => ({ jest.mock('./hooks', () => ({ sourceHooks: jest.fn().mockReturnValue({ updateVideoId: (args) => ({ updateVideoId: args }), - updateVideoURL: (args) => ({ updateVideoURL: args }), + updateVideoURL: jest.fn().mockName('updateVideoURL'), }), fallbackHooks: jest.fn().mockReturnValue({ addFallbackVideo: jest.fn().mockName('addFallbackVideo'), @@ -92,11 +92,11 @@ describe('VideoSourceWidget', () => { .props().onBlur).toEqual(expected); }); test('updateVideoURL is tied to url field onBlur', () => { - const expected = hook.updateVideoURL; - expect(el + const { onBlur } = el // eslint-disable-next-line - .children().at(0).children().at(0).children().at(2) - .props().onBlur).toEqual(expected); + .children().at(0).children().at(0).children().at(2).props(); + onBlur('onBlur event'); + expect(hook.updateVideoURL).toHaveBeenCalledWith('onBlur event', ''); }); }); });