From 50ca309e8723c5388f1fdaed6833b5c601ee3ec9 Mon Sep 17 00:00:00 2001 From: Ian Yenien Serrano <63758389+yenienserrano@users.noreply.github.com> Date: Tue, 1 Aug 2023 14:58:05 +0200 Subject: [PATCH] Fix error adding or deleting new fields (#5591) * Fix error adding or deleting new fields * Add test * Fix test * Linter * Add changelog * create visualization of processed events * create visualization events dropped * create visualization queue usage * create visualization of events decoded summary * Add optional chaining * Revert "create visualization of events decoded summary" This reverts commit cce97bdf152378bc65034582879eda0461abd6eb. * Revert "create visualization queue usage" This reverts commit 9fa4d40573ddacf4ca559c44dd42bce0a96a0b20. * Revert "create visualization events dropped" This reverts commit a6b35b13267b6fdb71a49bf9fff3732165e738eb. * Revert "create visualization of processed events" This reverts commit 7397b978df93465f47a4e41d89827cf1930152bc. --------- Co-authored-by: chantal.kelm --- CHANGELOG.md | 3 +- .../__snapshots__/agent-table.test.tsx.snap | 3055 +++++++++++++++++ .../agent/components/agent-table.test.tsx | 870 +++++ .../agent/components/agents-table.js | 35 +- 4 files changed, 3954 insertions(+), 9 deletions(-) create mode 100644 plugins/main/public/controllers/agent/components/__snapshots__/agent-table.test.tsx.snap create mode 100644 plugins/main/public/controllers/agent/components/agent-table.test.tsx diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f69bb0440..71c0da2dc2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,8 +10,9 @@ All notable changes to the Wazuh app project will be documented in this file. ### Fixed -- Fixed the propagation event so that the flyout data, in the decoders, does not change when the button is pressed. [#5597](https://github.com/wazuh/wazuh-kibana-app/pull/5597) +- Fixed problem with new or missing columns in agent table. [#5591](https://github.com/wazuh/wazuh-kibana-app/pull/5591) - Fixed the color of the agent name in the groups section in dark mode. [#5676](https://github.com/wazuh/wazuh-kibana-app/pull/5676) +- Fixed the propagation event so that the flyout data, in the decoders, does not change when the button is pressed. [#5597](https://github.com/wazuh/wazuh-kibana-app/pull/5597) ## Wazuh v4.6.0 - OpenSearch Dashboards 2.6.0 - Revision 01 diff --git a/plugins/main/public/controllers/agent/components/__snapshots__/agent-table.test.tsx.snap b/plugins/main/public/controllers/agent/components/__snapshots__/agent-table.test.tsx.snap new file mode 100644 index 0000000000..95dd06bcb8 --- /dev/null +++ b/plugins/main/public/controllers/agent/components/__snapshots__/agent-table.test.tsx.snap @@ -0,0 +1,3055 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`AgentsTable component Renders correctly to match the snapshot 1`] = ` +
+
+
+
+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+
+
+
+ +
+
+ +
+
+ + + +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+
+ + + + + + + + + + + + + + + + + + + +
+
+ + + + + + + + + + + + + + + + + + + Actions + + +
+
+ + No agents found + +
+
+
+
+
+
+
+
+`; + +exports[`AgentsTable component Renders correctly to match the snapshot with custom columns 1`] = ` +
+
+
+
+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+
+
+
+ +
+
+ +
+
+ + + +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+
+ + + + + + + + + + + + + + + + + +
+
+ + + + + + + + + + + + + + + Actions + + +
+
+ + No agents found + +
+
+
+
+
+
+
+
+`; + +exports[`AgentsTable component Renders correctly to match the snapshot with less columns 1`] = ` +
+
+
+
+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+
+
+
+ +
+
+ +
+
+ + + +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+
+ + + + + + + + + + + + + + + + + + + +
+
+ + + + + + + + + + + + + + + + + + + Actions + + +
+
+ + No agents found + +
+
+
+
+
+
+
+
+`; + +exports[`AgentsTable component Renders correctly to match the snapshot with less custom columns 1`] = ` +
+
+
+
+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+
+
+
+ +
+
+ +
+
+ + + +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+
+ + + + + + + + + + + + + + + + + +
+
+ + + + + + + + + + + + + + + Actions + + +
+
+ + No agents found + +
+
+
+
+
+
+
+
+`; + +exports[`AgentsTable component Renders correctly to match the snapshot with more columns 1`] = ` +
+
+
+
+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+
+
+
+ +
+
+ +
+
+ + + +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+
+ + + + + + + + + + + + + + + + + + + +
+
+ + + + + + + + + + + + + + + + + + + Actions + + +
+
+ + No agents found + +
+
+
+
+
+
+
+
+`; + +exports[`AgentsTable component Renders correctly to match the snapshot with more custom columns 1`] = ` +
+
+
+
+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+
+
+
+ +
+
+ +
+
+ + + +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+
+ + + + + + + + + + + + + + + + + +
+
+ + + + + + + + + + + + + + + Actions + + +
+
+ + No agents found + +
+
+
+
+
+
+
+
+`; diff --git a/plugins/main/public/controllers/agent/components/agent-table.test.tsx b/plugins/main/public/controllers/agent/components/agent-table.test.tsx new file mode 100644 index 0000000000..16f1787f61 --- /dev/null +++ b/plugins/main/public/controllers/agent/components/agent-table.test.tsx @@ -0,0 +1,870 @@ +import React from 'react'; +import { render } from 'enzyme'; +import { AgentsTable } from './agents-table'; +import { WzRequest } from '../../../react-services/wz-request'; +import { AgentSynced } from '../../../components/agents/agent-synced'; +import { AgentStatus } from '../../../components/agents/agent_status'; +import configureMockStore from 'redux-mock-store'; +import { Provider } from 'react-redux'; + +const data = [ + { + id: '001', + name: 'Debian agent', + ip: '127.0.0.1', + status: 'active', + group_config_status: 'not synced', + group: [ + 'default', + 'test', + 'test2', + 'test3', + 'test4', + 'test5', + 'test6', + 'test7', + 'test8', + ], + os_name: { + os: { + arch: 'x86_64', + major: '2', + name: 'Amazon Linux', + platform: 'amzn', + uname: + 'Linux |wazuh-manager-master-0 |4.14.114-105.126.amzn2.x86_64 |#1 SMP Tue May 7 02:26:40 UTC 2019 |x86_64', + version: '2', + }, + ip: '127.0.0.1', + id: '001', + group: [ + 'default', + 'test', + 'test2', + 'test3', + 'test4', + 'test5', + 'test6', + 'test7', + 'test8', + ], + registerIP: '127.0.0.1', + dateAdd: '2022-08-25T16:17:46Z', + name: 'Debian agent', + status: 'active', + manager: 'wazuh-manager-master-0', + node_name: 'master', + lastKeepAlive: '9999-12-31T23:59:59Z', + version: 'Wazuh v4.5.0', + group_config_status: 'not synced', + }, + version: 'v4.5.0', + node_name: 'master', + dateAdd: 'Aug 25, 2022 @ 18:17:46.000', + lastKeepAlive: 'Jan 1, 10000 @ 00:59:59.000', + actions: { + os: { + arch: 'x86_64', + major: '2', + name: 'Amazon Linux', + platform: 'amzn', + uname: + 'Linux |wazuh-manager-master-0 |4.14.114-105.126.amzn2.x86_64 |#1 SMP Tue May 7 02:26:40 UTC 2019 |x86_64', + version: '2', + }, + ip: '127.0.0.1', + id: '001', + group: [ + 'default', + 'test', + 'test2', + 'test3', + 'test4', + 'test5', + 'test6', + 'test7', + 'test8', + ], + registerIP: '127.0.0.1', + dateAdd: '2022-08-25T16:17:46Z', + name: 'Debian agent', + status: 'active', + manager: 'wazuh-manager-master-0', + node_name: 'master', + lastKeepAlive: '9999-12-31T23:59:59Z', + version: 'Wazuh v4.5.0', + group_config_status: 'not synced', + }, + upgrading: true, + }, + { + id: '002', + name: 'wazuh-manager-master-0', + ip: '127.0.0.1', + status: 'active', + group_config_status: 'synced', + group: ['default', 'test', 'test2', 'test3', 'test4'], + os_name: { + os: { + arch: 'x86_64', + major: '2', + name: 'Amazon Linux', + platform: 'amzn', + uname: + 'Linux |wazuh-manager-master-0 |4.14.114-105.126.amzn2.x86_64 |#1 SMP Tue May 7 02:26:40 UTC 2019 |x86_64', + version: '2', + }, + ip: '127.0.0.1', + id: '002', + group: ['default', 'test', 'test2', 'test3', 'test4'], + registerIP: '127.0.0.1', + dateAdd: '2022-08-25T16:17:46Z', + name: 'wazuh-manager-master-0', + status: 'active', + manager: 'wazuh-manager-master-0', + node_name: 'master', + lastKeepAlive: '9999-12-31T23:59:59Z', + version: 'Wazuh v4.5.0', + group_config_status: 'synced', + }, + version: 'v4.5.0', + node_name: 'master', + dateAdd: 'Aug 25, 2022 @ 18:17:46.000', + lastKeepAlive: 'Jan 1, 10000 @ 00:59:59.000', + actions: { + os: { + arch: 'x86_64', + major: '2', + name: 'Amazon Linux', + platform: 'amzn', + uname: + 'Linux |wazuh-manager-master-0 |4.14.114-105.126.amzn2.x86_64 |#1 SMP Tue May 7 02:26:40 UTC 2019 |x86_64', + version: '2', + }, + ip: '127.0.0.1', + id: '002', + group: ['default', 'test', 'test2', 'test3', 'test4'], + registerIP: '127.0.0.1', + dateAdd: '2022-08-25T16:17:46Z', + name: 'wazuh-manager-master-0', + status: 'active', + manager: 'wazuh-manager-master-0', + node_name: 'master', + lastKeepAlive: '9999-12-31T23:59:59Z', + version: 'Wazuh v4.5.0', + group_config_status: 'synced', + }, + upgrading: false, + }, + { + id: '003', + name: 'disconnected-agent', + ip: '111.111.1.111', + status: 'disconnected', + group_config_status: 'not synced', + group: ['default', 'test'], + os_name: { + os: { + build: '19045', + major: '10', + minor: '0', + name: 'Microsoft Windows 10 Home Single Language', + platform: 'windows', + uname: 'Microsoft Windows 10 Home Single Language', + version: '10.0.19045', + }, + disconnection_time: '2023-03-14T04:37:42Z', + manager: 'test.com', + status: 'disconnected', + name: 'disconnected-agent', + dateAdd: '1970-01-01T00:00:00Z', + group: ['default', 'test'], + lastKeepAlive: '2023-03-14T04:20:51Z', + node_name: 'node01', + registerIP: 'any', + id: '003', + version: 'Wazuh v4.3.10', + ip: '111.111.1.111', + mergedSum: 'e669d89eba52f6897060fc65a45300ac', + configSum: '97fccbb67e250b7c80aadc8d0dc59abe', + group_config_status: 'not synced', + }, + version: 'v4.3.10', + node_name: 'node01', + dateAdd: 'Jan 1, 1970 @ 01:00:00.000', + lastKeepAlive: 'Mar 14, 2023 @ 05:20:51.000', + actions: { + os: { + build: '19045', + major: '10', + minor: '0', + name: 'Microsoft Windows 10 Home Single Language', + platform: 'windows', + uname: 'Microsoft Windows 10 Home Single Language', + version: '10.0.19045', + }, + disconnection_time: '2023-03-14T04:37:42Z', + manager: 'test.com', + status: 'disconnected', + name: 'disconnected-agent', + dateAdd: '1970-01-01T00:00:00Z', + group: ['default', 'test'], + lastKeepAlive: '2023-03-14T04:20:51Z', + node_name: 'node01', + registerIP: 'any', + id: '003', + version: 'Wazuh v4.3.10', + ip: '111.111.1.111', + mergedSum: 'e669d89eba52f6897060fc65a45300ac', + configSum: '97fccbb67e250b7c80aadc8d0dc59abe', + group_config_status: 'not synced', + }, + upgrading: false, + }, +]; + +const defaultColumns = [ + { + field: 'id', + name: 'ID', + sortable: true, + show: true, + }, + { + field: 'name', + name: 'Name', + sortable: true, + show: true, + }, + { + field: 'ip', + name: 'IP address', + sortable: true, + show: true, + }, + { + field: 'group', + name: 'Group(s)', + sortable: true, + show: true, + render: groups => (groups !== '-' ? jest.fn() : '-'), + }, + { + field: 'os_name', + name: 'Operating system', + sortable: true, + show: true, + render: jest.fn(), + }, + { + field: 'node_name', + name: 'Cluster node', + sortable: true, + show: true, + }, + { + field: 'version', + name: 'Version', + sortable: true, + show: true, + }, + { + field: 'dateAdd', + name: 'Registration date', + sortable: true, + show: false, + }, + { + field: 'lastKeepAlive', + name: 'Last keep alive', + sortable: true, + show: false, + }, + { + field: 'status', + name: 'Status', + truncateText: true, + sortable: true, + show: true, + render: status => ( + + ), + }, + { + field: 'group_config_status', + name: 'Synced', + sortable: true, + show: false, + render: synced => , + }, + { + align: 'right', + width: '5%', + field: 'actions', + name: 'Actions', + show: true, + render: agent => jest.fn(), + }, +]; + +const customColumns = [ + { + field: 'id', + name: 'ID', + sortable: true, + show: false, + }, + { + field: 'name', + name: 'Name', + sortable: true, + show: false, + }, + { + field: 'ip', + name: 'IP address', + sortable: true, + show: true, + }, + { + field: 'group', + name: 'Group(s)', + sortable: true, + show: true, + render: groups => (groups !== '-' ? jest.fn() : '-'), + }, + { + field: 'os_name', + name: 'Operating system', + sortable: true, + show: true, + render: jest.fn(), + }, + { + field: 'node_name', + name: 'Cluster node', + sortable: true, + show: true, + }, + { + field: 'version', + name: 'Version', + sortable: true, + show: true, + }, + { + field: 'dateAdd', + name: 'Registration date', + sortable: true, + show: false, + }, + { + field: 'lastKeepAlive', + name: 'Last keep alive', + sortable: true, + show: false, + }, + { + field: 'status', + name: 'Status', + truncateText: true, + sortable: true, + show: true, + render: status => ( + + ), + }, + { + field: 'group_config_status', + name: 'Synced', + sortable: true, + show: false, + render: synced => , + }, + { + align: 'right', + width: '5%', + field: 'actions', + name: 'Actions', + show: true, + render: agent => jest.fn(), + }, +]; + +const moreColumns = [ + { + field: 'id', + name: 'ID', + sortable: true, + show: true, + }, + { + field: 'name', + name: 'Name', + sortable: true, + show: true, + }, + { + field: 'ip', + name: 'IP address', + sortable: true, + show: true, + }, + { + field: 'group', + name: 'Group(s)', + sortable: true, + show: true, + render: groups => (groups !== '-' ? jest.fn() : '-'), + }, + { + field: 'os_name', + name: 'Operating system', + sortable: true, + show: true, + render: jest.fn(), + }, + { + field: 'node_name', + name: 'Cluster node', + sortable: true, + show: true, + }, + { + field: 'version', + name: 'Version', + sortable: true, + show: true, + }, + { + field: 'dateAdd', + name: 'Registration date', + sortable: true, + show: false, + }, + { + field: 'lastKeepAlive', + name: 'Last keep alive', + sortable: true, + show: false, + }, + { + field: 'status', + name: 'Status', + truncateText: true, + sortable: true, + show: true, + render: status => ( + + ), + }, + { + field: 'group_config_status', + name: 'Synced', + sortable: true, + show: false, + render: synced => , + }, + { + align: 'right', + width: '5%', + field: 'actions', + name: 'Actions', + show: true, + render: agent => jest.fn(), + }, + { + field: 'Test', + name: 'Test', + show: true, + }, +]; + +const moreCustomColumns = [ + { + field: 'id', + name: 'ID', + sortable: true, + show: false, + }, + { + field: 'name', + name: 'Name', + sortable: true, + show: false, + }, + { + field: 'ip', + name: 'IP address', + sortable: true, + show: true, + }, + { + field: 'group', + name: 'Group(s)', + sortable: true, + show: true, + render: groups => (groups !== '-' ? jest.fn() : '-'), + }, + { + field: 'os_name', + name: 'Operating system', + sortable: true, + show: true, + render: jest.fn(), + }, + { + field: 'node_name', + name: 'Cluster node', + sortable: true, + show: true, + }, + { + field: 'version', + name: 'Version', + sortable: true, + show: true, + }, + { + field: 'dateAdd', + name: 'Registration date', + sortable: true, + show: false, + }, + { + field: 'lastKeepAlive', + name: 'Last keep alive', + sortable: true, + show: false, + }, + { + field: 'status', + name: 'Status', + truncateText: true, + sortable: true, + show: true, + render: status => ( + + ), + }, + { + field: 'group_config_status', + name: 'Synced', + sortable: true, + show: false, + render: synced => , + }, + { + align: 'right', + width: '5%', + field: 'actions', + name: 'Actions', + show: true, + render: agent => jest.fn(), + }, + { + field: 'Test', + name: 'Test', + show: true, + }, +]; + +const lessColumns = [ + { + field: 'id', + name: 'ID', + sortable: true, + show: true, + }, + { + field: 'name', + name: 'Name', + sortable: true, + show: true, + }, +]; + +const lessCustomColumns = [ + { + field: 'id', + name: 'ID', + sortable: true, + show: false, + }, + { + field: 'name', + name: 'Name', + sortable: true, + show: false, + }, +]; +const localStorageMock = (function () { + let store = { + columnsSelectedTableAgent: JSON.stringify(defaultColumns), + }; + + return { + getItem(key) { + return store[key]; + }, + + setItem(key, value) { + store[key] = value; + }, + + clear() { + store = { columnsSelectedTableAgent: JSON.stringify(defaultColumns) }; + }, + + removeItem(key) { + delete store[key]; + }, + + getAll() { + return store; + }, + }; +})(); + +Object.defineProperty(window, 'localStorage', { value: localStorageMock }); + +jest.mock('../../../react-services/common-services', () => ({ + getErrorOrchestrator: () => ({ + handleError: options => {}, + }), +})); + +jest.mock('../../../redux/reducers/appStateReducers', () => ({ + appStateReducers: state => state, +})); + +const permissionsStore = { + appStateReducers: { + withUserLogged: true, + userRoles: ['administrator'], + userPermissions: { + 'agent:create': { '*:*:*': 'allow' }, + rbac_mode: 'black', + }, + }, +}; + +const mockStore = configureMockStore(); + +jest.mock('react', () => ({ + ...jest.requireActual('react'), + useLayoutEffect: jest.requireActual('react').useEffect, +})); + +describe('AgentsTable component', () => { + WzRequest.apiReq = jest.fn(AgentsTable, 'wzReq').mockResolvedValue({ + data: { + data: { affected_items: data }, + }, + }); + + beforeEach(() => { + window.localStorage.clear(); + }); + + const store = mockStore(permissionsStore); + it('Renders correctly to match the snapshot', () => { + const wrapper = render( + + jest.fn()} + wzReq={WzRequest.apiReq} + addingNewAgent={() => jest.fn()} + downloadCsv={() => jest.fn()} + clickAction={() => jest.fn()} + formatUIDate={date => jest.fn()} + reload={() => jest.fn()} + /> + , + ); + + // Set table id to avoid snapshot changes + + const tableId = '__table_d1f8f8c3-1198-11ee-ab9b-75fc624fc672'; + + wrapper.find('table')[0]['attribs']['id'] = tableId; + + expect(wrapper).toMatchSnapshot(); + expect(window.localStorage.getItem('columnsSelectedTableAgent')).toEqual( + JSON.stringify(defaultColumns), + ); + }); + + it('Renders correctly to match the snapshot with less columns', () => { + window.localStorage.setItem( + 'columnsSelectedTableAgent', + JSON.stringify(lessColumns), + ); + const wrapper = render( + + jest.fn()} + wzReq={WzRequest.apiReq} + addingNewAgent={() => jest.fn()} + downloadCsv={() => jest.fn()} + clickAction={() => jest.fn()} + formatUIDate={date => jest.fn()} + reload={() => jest.fn()} + /> + , + ); + + // Set table id to avoid snapshot changes + + const tableId = '__table_d1fddac1-1198-11ee-ab9b-75fc624fc672'; + + wrapper.find('table')[0]['attribs']['id'] = tableId; + + expect(wrapper).toMatchSnapshot(); + expect(window.localStorage.getItem('columnsSelectedTableAgent')).toEqual( + JSON.stringify(defaultColumns), + ); + }); + + it('Renders correctly to match the snapshot with more columns', () => { + window.localStorage.setItem( + 'columnsSelectedTableAgent', + JSON.stringify(moreColumns), + ); + const wrapper = render( + + jest.fn()} + wzReq={WzRequest.apiReq} + addingNewAgent={() => jest.fn()} + downloadCsv={() => jest.fn()} + clickAction={() => jest.fn()} + formatUIDate={date => jest.fn()} + reload={() => jest.fn()} + /> + , + ); + + // Set table id to avoid snapshot changes + + const tableId = '__table_d2010f13-1198-11ee-ab9b-75fc624fc672'; + + wrapper.find('table')[0]['attribs']['id'] = tableId; + + expect(wrapper).toMatchSnapshot(); + expect(window.localStorage.getItem('columnsSelectedTableAgent')).toEqual( + JSON.stringify(defaultColumns), + ); + }); + + it('Renders correctly to match the snapshot with custom columns', () => { + window.localStorage.setItem( + 'columnsSelectedTableAgent', + JSON.stringify(customColumns), + ); + const wrapper = render( + + jest.fn()} + wzReq={WzRequest.apiReq} + addingNewAgent={() => jest.fn()} + downloadCsv={() => jest.fn()} + clickAction={() => jest.fn()} + formatUIDate={date => jest.fn()} + reload={() => jest.fn()} + /> + , + ); + + // Set table id to avoid snapshot changes + + const tableId = '__table_d203a723-1198-11ee-ab9b-75fc624fc672'; + + wrapper.find('table')[0]['attribs']['id'] = tableId; + + expect(wrapper).toMatchSnapshot(); + expect(window.localStorage.getItem('columnsSelectedTableAgent')).toEqual( + JSON.stringify(customColumns), + ); + }); + + it('Renders correctly to match the snapshot with less custom columns', () => { + window.localStorage.setItem( + 'columnsSelectedTableAgent', + JSON.stringify(lessCustomColumns), + ); + const wrapper = render( + + jest.fn()} + wzReq={WzRequest.apiReq} + addingNewAgent={() => jest.fn()} + downloadCsv={() => jest.fn()} + clickAction={() => jest.fn()} + formatUIDate={date => jest.fn()} + reload={() => jest.fn()} + /> + , + ); + + // Set table id to avoid snapshot changes + + const tableId = '__table_d2068d51-1198-11ee-ab9b-75fc624fc672'; + + wrapper.find('table')[0]['attribs']['id'] = tableId; + + expect(wrapper).toMatchSnapshot(); + expect(window.localStorage.getItem('columnsSelectedTableAgent')).toEqual( + JSON.stringify(customColumns), + ); + }); + + it('Renders correctly to match the snapshot with more custom columns', () => { + window.localStorage.setItem( + 'columnsSelectedTableAgent', + JSON.stringify(moreCustomColumns), + ); + const wrapper = render( + + jest.fn()} + wzReq={WzRequest.apiReq} + addingNewAgent={() => jest.fn()} + downloadCsv={() => jest.fn()} + clickAction={() => jest.fn()} + formatUIDate={date => jest.fn()} + reload={() => jest.fn()} + /> + , + ); + + // Set table id to avoid snapshot changes + + const tableId = '__table_d2097381-1198-11ee-ab9b-75fc624fc672'; + + wrapper.find('table')[0]['attribs']['id'] = tableId; + + expect(wrapper).toMatchSnapshot(); + expect(window.localStorage.getItem('columnsSelectedTableAgent')).toEqual( + JSON.stringify(customColumns), + ); + }); +}); diff --git a/plugins/main/public/controllers/agent/components/agents-table.js b/plugins/main/public/controllers/agent/components/agents-table.js index 09141ac9d6..58edc92ea6 100644 --- a/plugins/main/public/controllers/agent/components/agents-table.js +++ b/plugins/main/public/controllers/agent/components/agents-table.js @@ -252,15 +252,14 @@ export const AgentsTable = withErrorBoundary( const rawAgents = await this.props.wzReq('GET', '/agents', { params: { ...this.buildFilter(), select: selectFields }, }); - const formatedAgents = ( - ((rawAgents || {}).data || {}).data || {} - ).affected_items.map(this.formatAgent.bind(this)); + const formatedAgents = rawAgents?.data?.data?.affected_items?.map( + this.formatAgent.bind(this), + ); this._isMount && this.setState({ agents: formatedAgents, - totalItems: (((rawAgents || {}).data || {}).data || {}) - .total_affected_items, + totalItems: rawAgents?.data?.data?.total_affected_items, isLoading: false, }); } catch (error) { @@ -495,11 +494,31 @@ export const AgentsTable = withErrorBoundary( } } + // Check in the localstorage what is the column configuration + // and check that the columns are valid. + // If it is not a valid column it ignores it and if any column is missing it adds it. + getTableColumnsSelected() { - return ( - JSON.parse(window.localStorage.getItem('columnsSelectedTableAgent')) || - [] + const columnsSelected = JSON.parse( + window.localStorage.getItem('columnsSelectedTableAgent'), + ); + if (!columnsSelected) { + return []; + } + const customColumns = this.defaultColumns.map(field => { + const fieldConfig = columnsSelected.filter( + column => column?.field === field?.field, + ); + if (fieldConfig.length > 0) { + return fieldConfig[0]; + } + return field; + }); + window.localStorage.setItem( + 'columnsSelectedTableAgent', + JSON.stringify(customColumns), ); + return customColumns; } setTableColumnsSelected(data) {