Skip to content

Commit

Permalink
test: undo component delete
Browse files Browse the repository at this point in the history
  • Loading branch information
navinkarkera committed Dec 9, 2024
1 parent 5516285 commit d8eb58e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/library-authoring/components/ComponentDeleter.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getLibraryId } from '../../generic/key-utils';
import { ToastActionData } from '../../generic/toast-context';
import {
fireEvent,
render,
Expand All @@ -7,12 +8,15 @@ import {
waitFor,
} from '../../testUtils';
import { LibraryProvider } from '../common/context';
import { mockContentLibrary, mockDeleteLibraryBlock, mockLibraryBlockMetadata } from '../data/api.mocks';
import {
mockContentLibrary, mockDeleteLibraryBlock, mockLibraryBlockMetadata, mockRestoreLibraryBlock,
} from '../data/api.mocks';
import ComponentDeleter from './ComponentDeleter';

mockContentLibrary.applyMock(); // Not required, but avoids 404 errors in the logs when <LibraryProvider> loads data
mockLibraryBlockMetadata.applyMock();
const mockDelete = mockDeleteLibraryBlock.applyMock();
const mockRestore = mockRestoreLibraryBlock.applyMock();

const usageKey = mockLibraryBlockMetadata.usageKeyPublished;

Expand All @@ -22,9 +26,12 @@ const renderArgs = {
),
};

let mockShowToast: { (message: string, action?: ToastActionData | undefined): void; mock?: any; };

describe('<ComponentDeleter />', () => {
beforeEach(() => {
initializeMocks();
const mocks = initializeMocks();
mockShowToast = mocks.mockShowToast;
});

it('is invisible when isConfirmingDelete is false', async () => {
Expand All @@ -51,7 +58,7 @@ describe('<ComponentDeleter />', () => {
expect(mockCancel).toHaveBeenCalled();
});

it('deletes the block when confirmed', async () => {
it('deletes the block when confirmed, shows a toast with undo option and restores block on undo', async () => {
const mockCancel = jest.fn();
render(<ComponentDeleter usageKey={usageKey} isConfirmingDelete cancelDelete={mockCancel} />, renderArgs);

Expand All @@ -64,5 +71,13 @@ describe('<ComponentDeleter />', () => {
expect(mockDelete).toHaveBeenCalled();
});
expect(mockCancel).toHaveBeenCalled(); // In order to close the modal, this also gets called.
expect(mockShowToast).toHaveBeenCalled();
// Get restore / undo func from the toast
const restoreFn = mockShowToast.mock.calls[0][1].onClick;
restoreFn();
await waitFor(() => {
expect(mockRestore).toHaveBeenCalled();
expect(mockShowToast).toHaveBeenCalledWith('Undo successful');
});
});
});
11 changes: 11 additions & 0 deletions src/library-authoring/data/api.mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,17 @@ mockDeleteLibraryBlock.applyMock = () => (
jest.spyOn(api, 'deleteLibraryBlock').mockImplementation(mockDeleteLibraryBlock)
);

/**
* Mock for `restoreLibraryBlock()`
*/
export async function mockRestoreLibraryBlock(): ReturnType<typeof api.restoreLibraryBlock> {
// no-op
}
/** Apply this mock. Returns a spy object that can tell you if it's been called. */
mockRestoreLibraryBlock.applyMock = () => (
jest.spyOn(api, 'restoreLibraryBlock').mockImplementation(mockRestoreLibraryBlock)
);

/**
* Mock for `getXBlockFields()`
*
Expand Down
11 changes: 11 additions & 0 deletions src/library-authoring/data/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ describe('library data API', () => {
});
});

describe('restoreLibraryBlock', () => {
it('should restore a soft-deleted library block', async () => {
const { axiosMock } = initializeMocks();
const usageKey = 'lib:org:1';
const url = api.getLibraryBlockRestoreUrl(usageKey);
axiosMock.onPost(url).reply(200);
await api.restoreLibraryBlock({ usageKey });
expect(axiosMock.history.post[0].url).toEqual(url);
});
});

describe('commitLibraryChanges', () => {
it('should commit library changes', async () => {
const { axiosMock } = initializeMocks();
Expand Down

0 comments on commit d8eb58e

Please sign in to comment.