-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added api tests for deleteFileAttachments.
Added FileDeleteButtonIcon tests. Added useDeleteFileAttachment tests.
- Loading branch information
Showing
5 changed files
with
234 additions
and
2 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
x-pack/plugins/cases/public/components/files/file_delete_button_icon.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React from 'react'; | ||
|
||
import { screen, waitFor } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
import type { AppMockRenderer } from '../../common/mock'; | ||
|
||
import { createAppMockRenderer } from '../../common/mock'; | ||
import { basicCaseId, basicFileMock } from '../../containers/mock'; | ||
import { useDeleteFileAttachment } from '../../containers/use_delete_file_attachment'; | ||
import { FileDeleteButtonIcon } from './file_delete_button_icon'; | ||
|
||
jest.mock('../../containers/use_delete_file_attachment'); | ||
|
||
const useDeleteFileAttachmentMock = useDeleteFileAttachment as jest.Mock; | ||
|
||
describe('FileDeleteButtonIcon', () => { | ||
let appMockRender: AppMockRenderer; | ||
const mutate = jest.fn(); | ||
|
||
useDeleteFileAttachmentMock.mockReturnValue({ isLoading: false, mutate }); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
appMockRender = createAppMockRenderer(); | ||
}); | ||
|
||
it('renders delete button correctly', async () => { | ||
appMockRender.render(<FileDeleteButtonIcon caseId={basicCaseId} fileId={basicFileMock.id} />); | ||
|
||
expect(await screen.findByTestId('cases-files-delete-button')).toBeInTheDocument(); | ||
|
||
expect(useDeleteFileAttachmentMock).toBeCalledTimes(1); | ||
}); | ||
|
||
it('clicking delete button calls deleteFileAttachment with proper params', async () => { | ||
appMockRender.render(<FileDeleteButtonIcon caseId={basicCaseId} fileId={basicFileMock.id} />); | ||
|
||
const deleteButton = await screen.findByTestId('cases-files-delete-button'); | ||
|
||
expect(deleteButton).toBeInTheDocument(); | ||
|
||
userEvent.click(deleteButton); | ||
|
||
await waitFor(() => { | ||
expect(mutate).toHaveBeenCalledTimes(1); | ||
expect(mutate).toHaveBeenCalledWith({ | ||
caseId: basicCaseId, | ||
fileId: basicFileMock.id, | ||
successToasterTitle: 'File deleted successfully', | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
x-pack/plugins/cases/public/containers/use_delete_file_attachment.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { act, renderHook } from '@testing-library/react-hooks'; | ||
import * as api from './api'; | ||
import { basicCaseId, basicFileMock } from './mock'; | ||
import { useRefreshCaseViewPage } from '../components/case_view/use_on_refresh_case_view_page'; | ||
import { useToasts } from '../common/lib/kibana'; | ||
import type { AppMockRenderer } from '../common/mock'; | ||
import { createAppMockRenderer } from '../common/mock'; | ||
import { useDeleteFileAttachment } from './use_delete_file_attachment'; | ||
|
||
jest.mock('./api'); | ||
jest.mock('../common/lib/kibana'); | ||
jest.mock('../components/case_view/use_on_refresh_case_view_page'); | ||
|
||
const successToasterTitle = 'Deleted'; | ||
|
||
describe('useDeleteFileAttachment', () => { | ||
const addSuccess = jest.fn(); | ||
const addError = jest.fn(); | ||
|
||
(useToasts as jest.Mock).mockReturnValue({ addSuccess, addError }); | ||
|
||
let appMockRender: AppMockRenderer; | ||
|
||
beforeEach(() => { | ||
appMockRender = createAppMockRenderer(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('init', async () => { | ||
const { result } = renderHook(() => useDeleteFileAttachment(), { | ||
wrapper: appMockRender.AppWrapper, | ||
}); | ||
|
||
expect(result.current).toBeTruthy(); | ||
}); | ||
|
||
it('calls deleteFileAttachment with correct arguments - case', async () => { | ||
const spyOnDeleteFileAttachments = jest.spyOn(api, 'deleteFileAttachments'); | ||
|
||
const { waitForNextUpdate, result } = renderHook(() => useDeleteFileAttachment(), { | ||
wrapper: appMockRender.AppWrapper, | ||
}); | ||
|
||
act(() => { | ||
result.current.mutate({ | ||
caseId: basicCaseId, | ||
fileId: basicFileMock.id, | ||
successToasterTitle, | ||
}); | ||
}); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(spyOnDeleteFileAttachments).toHaveBeenCalledWith({ | ||
caseId: basicCaseId, | ||
fileIds: [basicFileMock.id], | ||
signal: expect.any(AbortSignal), | ||
}); | ||
}); | ||
|
||
it('refreshes the case page view', async () => { | ||
const { waitForNextUpdate, result } = renderHook(() => useDeleteFileAttachment(), { | ||
wrapper: appMockRender.AppWrapper, | ||
}); | ||
|
||
act(() => | ||
result.current.mutate({ | ||
caseId: basicCaseId, | ||
fileId: basicFileMock.id, | ||
successToasterTitle, | ||
}) | ||
); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(useRefreshCaseViewPage()).toBeCalled(); | ||
}); | ||
|
||
it('shows a success toaster correctly', async () => { | ||
const { waitForNextUpdate, result } = renderHook(() => useDeleteFileAttachment(), { | ||
wrapper: appMockRender.AppWrapper, | ||
}); | ||
|
||
act(() => | ||
result.current.mutate({ | ||
caseId: basicCaseId, | ||
fileId: basicFileMock.id, | ||
successToasterTitle, | ||
}) | ||
); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(addSuccess).toHaveBeenCalledWith({ | ||
title: successToasterTitle, | ||
className: 'eui-textBreakWord', | ||
}); | ||
}); | ||
|
||
it('sets isError when fails to delete a file attachment', async () => { | ||
const spyOnDeleteFileAttachments = jest.spyOn(api, 'deleteFileAttachments'); | ||
spyOnDeleteFileAttachments.mockRejectedValue(new Error('Error')); | ||
|
||
const { waitForNextUpdate, result } = renderHook(() => useDeleteFileAttachment(), { | ||
wrapper: appMockRender.AppWrapper, | ||
}); | ||
|
||
act(() => | ||
result.current.mutate({ | ||
caseId: basicCaseId, | ||
fileId: basicFileMock.id, | ||
successToasterTitle, | ||
}) | ||
); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(spyOnDeleteFileAttachments).toBeCalledWith({ | ||
caseId: basicCaseId, | ||
fileIds: [basicFileMock.id], | ||
signal: expect.any(AbortSignal), | ||
}); | ||
|
||
expect(addError).toHaveBeenCalled(); | ||
expect(result.current.isError).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters