From 2ad5b1f22cf8f8cec7622710b5e3e04c5e594d8d Mon Sep 17 00:00:00 2001 From: XnpioChV Date: Fri, 2 Aug 2024 13:21:38 -0500 Subject: [PATCH] test: LibraryInfo, LibraryInfoHeader, api and apiHooks --- src/library-authoring/data/api.test.ts | 29 ++++++++++++- src/library-authoring/data/apiHooks.test.tsx | 24 ++++++++++- .../library-info/LibraryInfo.test.tsx | 41 +++++++++++++++++++ .../library-info/LibraryInfoHeader.test.tsx | 17 ++++++++ 4 files changed, 108 insertions(+), 3 deletions(-) diff --git a/src/library-authoring/data/api.test.ts b/src/library-authoring/data/api.test.ts index 66736ad249..557488900d 100644 --- a/src/library-authoring/data/api.test.ts +++ b/src/library-authoring/data/api.test.ts @@ -1,7 +1,13 @@ import MockAdapter from 'axios-mock-adapter'; import { initializeMockApp } from '@edx/frontend-platform'; import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth'; -import { createLibraryBlock, getCreateLibraryBlockUrl } from './api'; +import { + commitLibraryChanges, + createLibraryBlock, + getCommitLibraryChangesUrl, + getCreateLibraryBlockUrl, + revertLibraryChanges, +} from './api'; let axiosMock; @@ -21,6 +27,7 @@ describe('library api calls', () => { afterEach(() => { jest.clearAllMocks(); + axiosMock.restore(); }); it('should create library block', async () => { @@ -35,4 +42,24 @@ describe('library api calls', () => { expect(axiosMock.history.post[0].url).toEqual(url); }); + + it('should commit library changes', async () => { + const libraryId = 'lib:org:1'; + const url = getCommitLibraryChangesUrl(libraryId); + axiosMock.onPost(url).reply(200); + + await commitLibraryChanges(libraryId); + + expect(axiosMock.history.post[0].url).toEqual(url); + }); + + it('should revert library changes', async () => { + const libraryId = 'lib:org:1'; + const url = getCommitLibraryChangesUrl(libraryId); + axiosMock.onDelete(url).reply(200); + + await revertLibraryChanges(libraryId); + + expect(axiosMock.history.delete[0].url).toEqual(url); + }); }); diff --git a/src/library-authoring/data/apiHooks.test.tsx b/src/library-authoring/data/apiHooks.test.tsx index 6798423767..686e114018 100644 --- a/src/library-authoring/data/apiHooks.test.tsx +++ b/src/library-authoring/data/apiHooks.test.tsx @@ -5,8 +5,8 @@ import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth'; import { renderHook } from '@testing-library/react-hooks'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; import MockAdapter from 'axios-mock-adapter'; -import { getCreateLibraryBlockUrl } from './api'; -import { useCreateLibraryBlock } from './apiHooks'; +import { getCommitLibraryChangesUrl, getCreateLibraryBlockUrl } from './api'; +import { useCommitLibraryChanges, useCreateLibraryBlock, useRevertLibraryChanges } from './apiHooks'; let axiosMock; @@ -50,4 +50,24 @@ describe('library api hooks', () => { expect(axiosMock.history.post[0].url).toEqual(url); }); + + it('should commit library changes', async () => { + const libraryId = 'lib:org:1'; + const url = getCommitLibraryChangesUrl(libraryId); + axiosMock.onPost(url).reply(200); + const { result } = renderHook(() => useCommitLibraryChanges(), { wrapper }); + await result.current.mutateAsync(libraryId); + + expect(axiosMock.history.post[0].url).toEqual(url); + }); + + it('should revert library changes', async () => { + const libraryId = 'lib:org:1'; + const url = getCommitLibraryChangesUrl(libraryId); + axiosMock.onDelete(url).reply(200); + const { result } = renderHook(() => useRevertLibraryChanges(), { wrapper }); + await result.current.mutateAsync(libraryId); + + expect(axiosMock.history.delete[0].url).toEqual(url); + }); }); diff --git a/src/library-authoring/library-info/LibraryInfo.test.tsx b/src/library-authoring/library-info/LibraryInfo.test.tsx index d5976d6c97..5ace15eb94 100644 --- a/src/library-authoring/library-info/LibraryInfo.test.tsx +++ b/src/library-authoring/library-info/LibraryInfo.test.tsx @@ -97,6 +97,20 @@ describe('', () => { expect(screen.getByText('June 26, 2024')).toBeInTheDocument(); }); + it('should render Library info in draft state without user', () => { + const data = { + ...libraryData, + lastDraftCreatedBy: null, + }; + + render(); + + expect(screen.getByText('Draft')).toBeInTheDocument(); + expect(screen.getByText('(Never Published)')).toBeInTheDocument(); + expect(screen.getByText('July 22, 2024')).toBeInTheDocument(); + expect(screen.queryByText('staff')).not.toBeInTheDocument(); + }); + it('should render Library creation date if last draft created date is null', () => { const data = { ...libraryData, @@ -111,6 +125,19 @@ describe('', () => { expect(screen.getAllByText('June 26, 2024')[1]).toBeInTheDocument(); }); + it('should render library info in draft state without date', () => { + const data = { + ...libraryData, + lastDraftCreated: null, + created: null, + }; + + render(); + + expect(screen.getByText('Draft')).toBeInTheDocument(); + expect(screen.getByText('(Never Published)')).toBeInTheDocument(); + }); + it('should render draft library info sidebar', () => { const data = { ...libraryData, @@ -138,6 +165,20 @@ describe('', () => { expect(screen.getByText('staff')).toBeInTheDocument(); }); + it('should render published library info without user', () => { + const data = { + ...libraryData, + lastPublished: '2024-07-26', + hasUnpublishedChanges: false, + publishedBy: null, + }; + + render(); + expect(screen.getByText('Published')).toBeInTheDocument(); + expect(screen.getByText('July 26, 2024')).toBeInTheDocument(); + expect(screen.queryByText('staff')).not.toBeInTheDocument(); + }); + it('should publish library', async () => { const url = getCommitLibraryChangesUrl(libraryData.id); axiosMock.onPost(url).reply(200); diff --git a/src/library-authoring/library-info/LibraryInfoHeader.test.tsx b/src/library-authoring/library-info/LibraryInfoHeader.test.tsx index ac7221e279..cf77f62617 100644 --- a/src/library-authoring/library-info/LibraryInfoHeader.test.tsx +++ b/src/library-authoring/library-info/LibraryInfoHeader.test.tsx @@ -116,11 +116,28 @@ describe('', () => { fireEvent.change(textBox, { target: { value: 'New Library Title' } }); fireEvent.keyDown(textBox, { key: 'Enter', code: 'Enter', charCode: 13 }); + expect(textBox).not.toBeInTheDocument(); expect(await screen.findByText('Library updated successfully')).toBeInTheDocument(); await waitFor(() => expect(axiosMock.history.patch[0].url).toEqual(url)); }); + it('should close edit library title on press Escape', async () => { + const url = getContentLibraryApiUrl(libraryData.id); + axiosMock.onPatch(url).reply(200); + render(); + + const editTitleButton = screen.getByRole('button', { name: /edit library name/i }); + fireEvent.click(editTitleButton); + + const textBox = screen.getByRole('textbox', { name: /title input/i }); + fireEvent.keyDown(textBox, { key: 'Escape', code: 'Escape', charCode: 27 }); + + expect(textBox).not.toBeInTheDocument(); + + await waitFor(() => expect(axiosMock.history.patch.length).toEqual(0)); + }); + it('should show error on edit library tittle', async () => { const url = getContentLibraryApiUrl(libraryData.id); axiosMock.onPatch(url).reply(500);