From 34fe4b1de19e2abc7018e08f8fd36b5857b1be0c Mon Sep 17 00:00:00 2001 From: Yusuf Musleh Date: Fri, 2 Aug 2024 15:05:57 +0300 Subject: [PATCH] test: Fix + add tests --- .../LibraryAuthoringPage.test.tsx | 7 ++++ .../add-content/AddContentContainer.test.tsx | 41 ++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/library-authoring/LibraryAuthoringPage.test.tsx b/src/library-authoring/LibraryAuthoringPage.test.tsx index 8e7369184b..3dbd737bc5 100644 --- a/src/library-authoring/LibraryAuthoringPage.test.tsx +++ b/src/library-authoring/LibraryAuthoringPage.test.tsx @@ -97,6 +97,13 @@ const libraryData: ContentLibrary = { updated: '2024-07-20', }; +const clipboardBroadcastChannelMock = { + postMessage: jest.fn(), + close: jest.fn(), +}; + +(global as any).BroadcastChannel = jest.fn(() => clipboardBroadcastChannelMock); + const RootWrapper = () => ( diff --git a/src/library-authoring/add-content/AddContentContainer.test.tsx b/src/library-authoring/add-content/AddContentContainer.test.tsx index 51b07843c9..ad20963942 100644 --- a/src/library-authoring/add-content/AddContentContainer.test.tsx +++ b/src/library-authoring/add-content/AddContentContainer.test.tsx @@ -10,7 +10,10 @@ import MockAdapter from 'axios-mock-adapter'; import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth'; import AddContentContainer from './AddContentContainer'; import initializeStore from '../../store'; -import { getCreateLibraryBlockUrl } from '../data/api'; +import { getCreateLibraryBlockUrl, getLibraryPasteClipboardUrl } from '../data/api'; +import { getClipboardUrl } from '../../generic/data/api'; + +import { clipboardXBlock } from '../../__mocks__'; const mockUseParams = jest.fn(); let axiosMock; @@ -31,6 +34,13 @@ const queryClient = new QueryClient({ }, }); +const clipboardBroadcastChannelMock = { + postMessage: jest.fn(), + close: jest.fn(), +}; + +(global as any).BroadcastChannel = jest.fn(() => clipboardBroadcastChannelMock); + const RootWrapper = () => ( @@ -69,6 +79,7 @@ describe('', () => { expect(screen.getByRole('button', { name: /drag drop/i })).toBeInTheDocument(); expect(screen.getByRole('button', { name: /video/i })).toBeInTheDocument(); expect(screen.getByRole('button', { name: /advanced \/ other/i })).toBeInTheDocument(); + expect(screen.queryByRole('button', { name: /copy from clipboard/i })).not.toBeInTheDocument(); }); it('should create a content', async () => { @@ -82,4 +93,32 @@ describe('', () => { await waitFor(() => expect(axiosMock.history.post[0].url).toEqual(url)); }); + + it('should render paste button if clipboard contains pastable xblock', async () => { + const url = getClipboardUrl(); + axiosMock.onGet(url).reply(200, clipboardXBlock); + + render(); + + await waitFor(() => expect(axiosMock.history.get[0].url).toEqual(url)); + + expect(screen.getByRole('button', { name: /paste from clipboard/i })).toBeInTheDocument(); + }); + + it('should paste content', async () => { + const clipboardUrl = getClipboardUrl(); + axiosMock.onGet(clipboardUrl).reply(200, clipboardXBlock); + + const pasteUrl = getLibraryPasteClipboardUrl(libraryId); + axiosMock.onPost(pasteUrl).reply(200); + + render(); + + await waitFor(() => expect(axiosMock.history.get[0].url).toEqual(clipboardUrl)); + + const pasteButton = screen.getByRole('button', { name: /paste from clipboard/i }); + fireEvent.click(pasteButton); + + await waitFor(() => expect(axiosMock.history.post[0].url).toEqual(pasteUrl)); + }); });