Skip to content

Commit

Permalink
test: Fix + add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yusuf-musleh committed Aug 13, 2024
1 parent 8f6ab4b commit 34fe4b1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/library-authoring/LibraryAuthoringPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => (
<AppProvider store={store}>
<IntlProvider locale="en" messages={{}}>
Expand Down
41 changes: 40 additions & 1 deletion src/library-authoring/add-content/AddContentContainer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = () => (
<AppProvider store={store}>
<IntlProvider locale="en" messages={{}}>
Expand Down Expand Up @@ -69,6 +79,7 @@ describe('<AddContentContainer />', () => {
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 () => {
Expand All @@ -82,4 +93,32 @@ describe('<AddContentContainer />', () => {

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(<RootWrapper />);

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(<RootWrapper />);

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));
});
});

0 comments on commit 34fe4b1

Please sign in to comment.