Skip to content

Commit

Permalink
[Cases] Including owner when patching a comment Closes elastic#102732 (
Browse files Browse the repository at this point in the history
…elastic#103020)

* Including owner when patching a comment

* Fixing tests
  • Loading branch information
jonathan-buttner authored and kibanamachine committed Jun 24, 2021
1 parent 697f8ba commit 1bc8cf8
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 60 deletions.
31 changes: 17 additions & 14 deletions x-pack/plugins/cases/public/containers/api.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -363,33 +363,36 @@ describe('Case Configuration API', () => {
});

test('check url, method, signal', async () => {
await patchComment(
basicCase.id,
basicCase.comments[0].id,
'updated comment',
basicCase.comments[0].version,
abortCtrl.signal
);
await patchComment({
caseId: basicCase.id,
commentId: basicCase.comments[0].id,
commentUpdate: 'updated comment',
version: basicCase.comments[0].version,
signal: abortCtrl.signal,
owner: SECURITY_SOLUTION_OWNER,
});
expect(fetchMock).toHaveBeenCalledWith(`${CASES_URL}/${basicCase.id}/comments`, {
method: 'PATCH',
body: JSON.stringify({
comment: 'updated comment',
type: CommentType.user,
id: basicCase.comments[0].id,
version: basicCase.comments[0].version,
owner: SECURITY_SOLUTION_OWNER,
}),
signal: abortCtrl.signal,
});
});

test('happy path', async () => {
const resp = await patchComment(
basicCase.id,
basicCase.comments[0].id,
'updated comment',
basicCase.comments[0].version,
abortCtrl.signal
);
const resp = await patchComment({
caseId: basicCase.id,
commentId: basicCase.comments[0].id,
commentUpdate: 'updated comment',
version: basicCase.comments[0].version,
signal: abortCtrl.signal,
owner: SECURITY_SOLUTION_OWNER,
});
expect(resp).toEqual(basicCase);
});
});
Expand Down
26 changes: 18 additions & 8 deletions x-pack/plugins/cases/public/containers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,21 +283,31 @@ export const postComment = async (
return convertToCamelCase<CaseResponse, Case>(decodeCaseResponse(response));
};

export const patchComment = async (
caseId: string,
commentId: string,
commentUpdate: string,
version: string,
signal: AbortSignal,
subCaseId?: string
): Promise<Case> => {
export const patchComment = async ({
caseId,
commentId,
commentUpdate,
version,
signal,
owner,
subCaseId,
}: {
caseId: string;
commentId: string;
commentUpdate: string;
version: string;
signal: AbortSignal;
owner: string;
subCaseId?: string;
}): Promise<Case> => {
const response = await KibanaServices.get().http.fetch<CaseResponse>(getCaseCommentsUrl(caseId), {
method: 'PATCH',
body: JSON.stringify({
comment: commentUpdate,
type: CommentType.user,
id: commentId,
version,
owner,
}),
...(subCaseId ? { query: { subCaseId } } : {}),
signal,
Expand Down
67 changes: 33 additions & 34 deletions x-pack/plugins/cases/public/containers/use_update_comment.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
* 2.0.
*/

import React from 'react';
import { renderHook, act } from '@testing-library/react-hooks';
import { useUpdateComment, UseUpdateComment } from './use_update_comment';
import { basicCase, basicCaseCommentPatch, basicSubCaseId } from './mock';
import * as api from './api';
import { TestProviders } from '../common/mock';
import { SECURITY_SOLUTION_OWNER } from '../../common';

jest.mock('./api');
jest.mock('../common/lib/kibana');
Expand All @@ -25,16 +28,20 @@ describe('useUpdateComment', () => {
updateCase,
version: basicCase.comments[0].version,
};

const renderHookUseUpdateComment = () =>
renderHook<string, UseUpdateComment>(() => useUpdateComment(), {
wrapper: ({ children }) => <TestProviders>{children}</TestProviders>,
});

beforeEach(() => {
jest.clearAllMocks();
jest.restoreAllMocks();
});

it('init', async () => {
await act(async () => {
const { result, waitForNextUpdate } = renderHook<string, UseUpdateComment>(() =>
useUpdateComment()
);
const { result, waitForNextUpdate } = renderHookUseUpdateComment();
await waitForNextUpdate();
expect(result.current).toEqual({
isLoadingIds: [],
Expand All @@ -48,51 +55,47 @@ describe('useUpdateComment', () => {
const spyOnPatchComment = jest.spyOn(api, 'patchComment');

await act(async () => {
const { result, waitForNextUpdate } = renderHook<string, UseUpdateComment>(() =>
useUpdateComment()
);
const { result, waitForNextUpdate } = renderHookUseUpdateComment();
await waitForNextUpdate();

result.current.patchComment(sampleUpdate);
await waitForNextUpdate();
expect(spyOnPatchComment).toBeCalledWith(
basicCase.id,
basicCase.comments[0].id,
'updated comment',
basicCase.comments[0].version,
abortCtrl.signal,
undefined
);
expect(spyOnPatchComment).toBeCalledWith({
caseId: basicCase.id,
commentId: basicCase.comments[0].id,
commentUpdate: 'updated comment',
version: basicCase.comments[0].version,
signal: abortCtrl.signal,
owner: SECURITY_SOLUTION_OWNER,
subCaseId: undefined,
});
});
});

it('calls patchComment with correct arguments - sub case', async () => {
const spyOnPatchComment = jest.spyOn(api, 'patchComment');

await act(async () => {
const { result, waitForNextUpdate } = renderHook<string, UseUpdateComment>(() =>
useUpdateComment()
);
const { result, waitForNextUpdate } = renderHookUseUpdateComment();
await waitForNextUpdate();

result.current.patchComment({ ...sampleUpdate, subCaseId: basicSubCaseId });
await waitForNextUpdate();
expect(spyOnPatchComment).toBeCalledWith(
basicCase.id,
basicCase.comments[0].id,
'updated comment',
basicCase.comments[0].version,
abortCtrl.signal,
basicSubCaseId
);
expect(spyOnPatchComment).toBeCalledWith({
caseId: basicCase.id,
commentId: basicCase.comments[0].id,
commentUpdate: 'updated comment',
version: basicCase.comments[0].version,
signal: abortCtrl.signal,
owner: SECURITY_SOLUTION_OWNER,
subCaseId: basicSubCaseId,
});
});
});

it('patch comment', async () => {
await act(async () => {
const { result, waitForNextUpdate } = renderHook<string, UseUpdateComment>(() =>
useUpdateComment()
);
const { result, waitForNextUpdate } = renderHookUseUpdateComment();
await waitForNextUpdate();
result.current.patchComment(sampleUpdate);
await waitForNextUpdate();
Expand All @@ -108,9 +111,7 @@ describe('useUpdateComment', () => {

it('set isLoading to true when posting case', async () => {
await act(async () => {
const { result, waitForNextUpdate } = renderHook<string, UseUpdateComment>(() =>
useUpdateComment()
);
const { result, waitForNextUpdate } = renderHookUseUpdateComment();
await waitForNextUpdate();
result.current.patchComment(sampleUpdate);

Expand All @@ -125,9 +126,7 @@ describe('useUpdateComment', () => {
});

await act(async () => {
const { result, waitForNextUpdate } = renderHook<string, UseUpdateComment>(() =>
useUpdateComment()
);
const { result, waitForNextUpdate } = renderHookUseUpdateComment();
await waitForNextUpdate();
result.current.patchComment(sampleUpdate);

Expand Down
13 changes: 9 additions & 4 deletions x-pack/plugins/cases/public/containers/use_update_comment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { useReducer, useCallback, useRef, useEffect } from 'react';
import { useToasts } from '../common/lib/kibana';
import { useOwnerContext } from '../components/owner_context/use_owner_context';
import { patchComment } from './api';
import * as i18n from './translations';
import { Case } from './types';
Expand Down Expand Up @@ -72,6 +73,9 @@ export const useUpdateComment = (): UseUpdateComment => {
const toasts = useToasts();
const isCancelledRef = useRef(false);
const abortCtrlRef = useRef(new AbortController());
// this hook guarantees that there will be at least one value in the owner array, we'll
// just use the first entry just in case there are more than one entry
const owner = useOwnerContext()[0];

const dispatchUpdateComment = useCallback(
async ({
Expand All @@ -89,14 +93,15 @@ export const useUpdateComment = (): UseUpdateComment => {
abortCtrlRef.current = new AbortController();
dispatch({ type: 'FETCH_INIT', payload: commentId });

const response = await patchComment(
const response = await patchComment({
caseId,
commentId,
commentUpdate,
version,
abortCtrlRef.current.signal,
subCaseId
);
signal: abortCtrlRef.current.signal,
subCaseId,
owner,
});

if (!isCancelledRef.current) {
updateCase(response);
Expand Down

0 comments on commit 1bc8cf8

Please sign in to comment.