Skip to content

Commit

Permalink
test: LibraryInfo, LibraryInfoHeader, api and apiHooks
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisChV committed Aug 2, 2024
1 parent a55d106 commit 2ad5b1f
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 3 deletions.
29 changes: 28 additions & 1 deletion src/library-authoring/data/api.test.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -21,6 +27,7 @@ describe('library api calls', () => {

afterEach(() => {
jest.clearAllMocks();
axiosMock.restore();
});

it('should create library block', async () => {
Expand All @@ -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);
});
});
24 changes: 22 additions & 2 deletions src/library-authoring/data/apiHooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
});
});
41 changes: 41 additions & 0 deletions src/library-authoring/library-info/LibraryInfo.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@ describe('<LibraryInfo />', () => {
expect(screen.getByText('June 26, 2024')).toBeInTheDocument();
});

it('should render Library info in draft state without user', () => {
const data = {
...libraryData,
lastDraftCreatedBy: null,
};

render(<RootWrapper data={data} />);

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,
Expand All @@ -111,6 +125,19 @@ describe('<LibraryInfo />', () => {
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(<RootWrapper data={data} />);

expect(screen.getByText('Draft')).toBeInTheDocument();
expect(screen.getByText('(Never Published)')).toBeInTheDocument();
});

it('should render draft library info sidebar', () => {
const data = {
...libraryData,
Expand Down Expand Up @@ -138,6 +165,20 @@ describe('<LibraryInfo />', () => {
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(<RootWrapper data={data} />);
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);
Expand Down
17 changes: 17 additions & 0 deletions src/library-authoring/library-info/LibraryInfoHeader.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,28 @@ describe('<LibraryInfoHeader />', () => {
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(<RootWrapper data={libraryData} />);

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);
Expand Down

0 comments on commit 2ad5b1f

Please sign in to comment.