From f6c8e6aa03e1f0259331e0af4039e076695e893a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Kopycin=CC=81ski?= Date: Wed, 4 Mar 2020 15:15:23 +0100 Subject: [PATCH 1/5] [SIEM] Fix rule delete/duplicate actions --- .../rules/all/columns.test.tsx | 73 +++++++++++++++++++ .../detection_engine/rules/all/columns.tsx | 14 ++-- 2 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/columns.test.tsx diff --git a/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/columns.test.tsx b/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/columns.test.tsx new file mode 100644 index 0000000000000..6e2f236b52dc4 --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/columns.test.tsx @@ -0,0 +1,73 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import uuid from 'uuid'; +import { createMemoryHistory } from 'history'; + +const history = createMemoryHistory(); + +import { mockRule } from './__mocks__/mock'; +import { getActions } from './columns'; + +jest.mock('./actions', () => ({ + __esModule: true, // this property makes it work + duplicateRulesAction: jest.fn(), + deleteRulesAction: jest.fn(), +})); + +import { duplicateRulesAction, deleteRulesAction } from './actions'; + +describe('AllRulesTable Columns', () => { + describe('getActions', () => { + const rule = mockRule(uuid.v4()); + let actions: ReturnType; + let results: string[] = []; + const dispatch = jest.fn(); + const dispatchToaster = jest.fn(); + const reFetchRules = jest.fn(); + + beforeEach(() => { + results = []; + + reFetchRules.mockReturnValue( + new Promise(resolve => { + results.push('reFetchRules'); + resolve(); + }) + ); + }); + + test('duplicate rule onClick should call refetch after the rule is duplicated', async () => { + (duplicateRulesAction as jest.Mock).mockReturnValue( + new Promise(resolve => + setTimeout(() => { + results.push('duplicateRulesAction'); + resolve(); + }, 500) + ) + ); + + actions = getActions(dispatch, dispatchToaster, history, reFetchRules); + await actions[1].onClick(rule); + expect(results).toEqual(['reFetchRules', 'duplicateRulesAction']); + }); + + test('delete rule onClick should call refetch after the rule is deleted', async () => { + (deleteRulesAction as jest.Mock).mockReturnValue( + new Promise(resolve => + setTimeout(() => { + results.push('deleteRulesAction'); + resolve(); + }, 500) + ) + ); + + actions = getActions(dispatch, dispatchToaster, history, reFetchRules); + await actions[3].onClick(rule); + expect(results).toEqual(['reFetchRules', 'deleteRulesAction']); + }); + }); +}); diff --git a/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/columns.tsx b/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/columns.tsx index ff104f09d68ef..2214190de6a16 100644 --- a/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/columns.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/columns.tsx @@ -34,7 +34,7 @@ import { } from './actions'; import { Action } from './reducer'; -const getActions = ( +export const getActions = ( dispatch: React.Dispatch, dispatchToaster: Dispatch, history: H.History, @@ -51,9 +51,9 @@ const getActions = ( description: i18n.DUPLICATE_RULE, icon: 'copy', name: i18n.DUPLICATE_RULE, - onClick: (rowItem: Rule) => { - duplicateRulesAction([rowItem], [rowItem.id], dispatch, dispatchToaster); - reFetchRules(true); + onClick: async (rowItem: Rule) => { + await duplicateRulesAction([rowItem], [rowItem.id], dispatch, dispatchToaster); + await reFetchRules(true); }, }, { @@ -67,9 +67,9 @@ const getActions = ( description: i18n.DELETE_RULE, icon: 'trash', name: i18n.DELETE_RULE, - onClick: (rowItem: Rule) => { - deleteRulesAction([rowItem.id], dispatch, dispatchToaster); - reFetchRules(true); + onClick: async (rowItem: Rule) => { + await deleteRulesAction([rowItem.id], dispatch, dispatchToaster); + await reFetchRules(true); }, }, ]; From 409c8de351554b1a8f714173025093c59cbd8363 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Kopycin=CC=81ski?= Date: Wed, 4 Mar 2020 15:34:03 +0100 Subject: [PATCH 2/5] fix tests --- .../rules/all/columns.test.tsx | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/columns.test.tsx b/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/columns.test.tsx index 6e2f236b52dc4..28cb894c3f91c 100644 --- a/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/columns.test.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/columns.test.tsx @@ -13,7 +13,6 @@ import { mockRule } from './__mocks__/mock'; import { getActions } from './columns'; jest.mock('./actions', () => ({ - __esModule: true, // this property makes it work duplicateRulesAction: jest.fn(), deleteRulesAction: jest.fn(), })); @@ -32,42 +31,44 @@ describe('AllRulesTable Columns', () => { beforeEach(() => { results = []; - reFetchRules.mockReturnValue( - new Promise(resolve => { - results.push('reFetchRules'); - resolve(); - }) + reFetchRules.mockImplementation( + () => + new Promise(resolve => { + results.push('reFetchRules'); + resolve(); + }) ); }); test('duplicate rule onClick should call refetch after the rule is duplicated', async () => { - (duplicateRulesAction as jest.Mock).mockReturnValue( - new Promise(resolve => - setTimeout(() => { - results.push('duplicateRulesAction'); - resolve(); - }, 500) - ) + (duplicateRulesAction as jest.Mock).mockImplementation( + () => + new Promise(resolve => + setTimeout(() => { + results.push('duplicateRulesAction'); + resolve(); + }, 1000) + ) ); actions = getActions(dispatch, dispatchToaster, history, reFetchRules); await actions[1].onClick(rule); - expect(results).toEqual(['reFetchRules', 'duplicateRulesAction']); + expect(results).toEqual(['duplicateRulesAction', 'reFetchRules']); }); test('delete rule onClick should call refetch after the rule is deleted', async () => { - (deleteRulesAction as jest.Mock).mockReturnValue( - new Promise(resolve => - setTimeout(() => { - results.push('deleteRulesAction'); - resolve(); - }, 500) - ) + (deleteRulesAction as jest.Mock).mockImplementation( + () => + new Promise(resolve => + setTimeout(() => { + resolve(results.push('deleteRulesAction')); + }, 500) + ) ); actions = getActions(dispatch, dispatchToaster, history, reFetchRules); await actions[3].onClick(rule); - expect(results).toEqual(['reFetchRules', 'deleteRulesAction']); + expect(results).toEqual(['deleteRulesAction', 'reFetchRules']); }); }); }); From 28b20678f1f2ebb7c0425fac324b2df24585774b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Kopycin=CC=81ski?= Date: Wed, 4 Mar 2020 15:37:16 +0100 Subject: [PATCH 3/5] decrease timeout --- .../public/pages/detection_engine/rules/all/columns.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/columns.test.tsx b/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/columns.test.tsx index 28cb894c3f91c..50ecc57da6e29 100644 --- a/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/columns.test.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/columns.test.tsx @@ -47,7 +47,7 @@ describe('AllRulesTable Columns', () => { setTimeout(() => { results.push('duplicateRulesAction'); resolve(); - }, 1000) + }, 500) ) ); From 0472c1f081a0563ac16d7501084e27f7fafa7c60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Kopycin=CC=81ski?= Date: Wed, 4 Mar 2020 15:51:10 +0100 Subject: [PATCH 4/5] cleanup --- .../pages/detection_engine/rules/all/columns.test.tsx | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/columns.test.tsx b/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/columns.test.tsx index 50ecc57da6e29..3ae823af5c017 100644 --- a/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/columns.test.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/columns.test.tsx @@ -31,13 +31,7 @@ describe('AllRulesTable Columns', () => { beforeEach(() => { results = []; - reFetchRules.mockImplementation( - () => - new Promise(resolve => { - results.push('reFetchRules'); - resolve(); - }) - ); + reFetchRules.mockImplementation(() => Promise.resolve(results.push('reFetchRules'))); }); test('duplicate rule onClick should call refetch after the rule is duplicated', async () => { @@ -45,8 +39,7 @@ describe('AllRulesTable Columns', () => { () => new Promise(resolve => setTimeout(() => { - results.push('duplicateRulesAction'); - resolve(); + resolve(results.push('duplicateRulesAction')); }, 500) ) ); From 2f58f7145e1d49f54561d23b2295896632c251ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Kopycin=CC=81ski?= Date: Wed, 4 Mar 2020 18:44:38 +0100 Subject: [PATCH 5/5] PR comments --- .../rules/all/columns.test.tsx | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/columns.test.tsx b/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/columns.test.tsx index 3ae823af5c017..11becb14625a9 100644 --- a/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/columns.test.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/columns.test.tsx @@ -22,7 +22,6 @@ import { duplicateRulesAction, deleteRulesAction } from './actions'; describe('AllRulesTable Columns', () => { describe('getActions', () => { const rule = mockRule(uuid.v4()); - let actions: ReturnType; let results: string[] = []; const dispatch = jest.fn(); const dispatchToaster = jest.fn(); @@ -31,7 +30,10 @@ describe('AllRulesTable Columns', () => { beforeEach(() => { results = []; - reFetchRules.mockImplementation(() => Promise.resolve(results.push('reFetchRules'))); + reFetchRules.mockImplementation(() => { + results.push('reFetchRules'); + Promise.resolve(); + }); }); test('duplicate rule onClick should call refetch after the rule is duplicated', async () => { @@ -39,13 +41,19 @@ describe('AllRulesTable Columns', () => { () => new Promise(resolve => setTimeout(() => { - resolve(results.push('duplicateRulesAction')); + results.push('duplicateRulesAction'); + resolve(); }, 500) ) ); - actions = getActions(dispatch, dispatchToaster, history, reFetchRules); - await actions[1].onClick(rule); + const duplicateRulesActionObject = getActions( + dispatch, + dispatchToaster, + history, + reFetchRules + )[1]; + await duplicateRulesActionObject.onClick(rule); expect(results).toEqual(['duplicateRulesAction', 'reFetchRules']); }); @@ -54,13 +62,19 @@ describe('AllRulesTable Columns', () => { () => new Promise(resolve => setTimeout(() => { - resolve(results.push('deleteRulesAction')); + results.push('deleteRulesAction'); + resolve(); }, 500) ) ); - actions = getActions(dispatch, dispatchToaster, history, reFetchRules); - await actions[3].onClick(rule); + const deleteRulesActionObject = getActions( + dispatch, + dispatchToaster, + history, + reFetchRules + )[3]; + await deleteRulesActionObject.onClick(rule); expect(results).toEqual(['deleteRulesAction', 'reFetchRules']); }); });