From 5deded96d51d38472c4edb9f0e6fedd75412cc32 Mon Sep 17 00:00:00 2001 From: Elena Martynova Date: Thu, 14 Dec 2023 13:00:45 +0300 Subject: [PATCH] fix: filtering action params logic, add test (#108) * refactor: fix filtering action params logic, add test * fix: action params state and params tests --- src/shared/modules/helpers.ts | 15 +- .../units/__tests__/state-and-params.test.ts | 152 ++++++++++++++++++ 2 files changed, 153 insertions(+), 14 deletions(-) diff --git a/src/shared/modules/helpers.ts b/src/shared/modules/helpers.ts index 68056d5..84306e1 100644 --- a/src/shared/modules/helpers.ts +++ b/src/shared/modules/helpers.ts @@ -109,25 +109,12 @@ export function formQueueData({ const itemQueueParams: StringParams = get(itemsStateAndParams, [item.id, 'params'], {}); const filteredParamsByDefaults = pick(itemQueueParams, Object.keys(itemDefaultParams)); - /** - * filtering actionParams without prefixes by defaults params - * ex.: - * itemDefaultParams contains 'Country' in defaults - * and we receive '_ap_Country' and '_ap_City' in itemQueueParams - * we need to ignore '_ap_City' param because we don't have actionParam without prefix ('City') in defaults - */ - const actionParams = pickActionParamsFromParams(itemQueueParams, false) || {}; - const filteredActionParamsByDefaults = pick( - actionParams, - Object.keys(itemDefaultParams), - ); - /** * merging filtered params and filtered actionParams with prefixes */ const params = { ...filteredParamsByDefaults, - ...transformParamsToActionParams(filteredActionParamsByDefaults), + ...(pickActionParamsFromParams(itemQueueParams, true) || {}), }; return { diff --git a/src/shared/units/__tests__/state-and-params.test.ts b/src/shared/units/__tests__/state-and-params.test.ts index 37531c0..3236c29 100644 --- a/src/shared/units/__tests__/state-and-params.test.ts +++ b/src/shared/units/__tests__/state-and-params.test.ts @@ -599,3 +599,155 @@ describe('getItemsStateAndParams actionParams variants check', () => { }); }); }); + +const config = { + id: '7kw', + items: [ + { + id: 'nvp', + data: { + tabs: [ + { + id: 'rkq', + title: 'test', + params: {}, + chartId: 'some-chart', + isDefault: true, + autoHeight: false, + description: '', + enableActionParams: true, + }, + ], + hideTitle: true, + }, + type: 'widget', + layout: { + h: 12, + w: 12, + }, + namespace: 'default', + }, + { + id: 'Q9K', + data: { + title: 'Year', + source: {}, + }, + type: 'control', + layout: { + h: 2, + w: 8, + }, + defaults: { + year_some_id: '', + }, + namespace: 'default', + }, + ], + title: 'test', + layout: [ + { + h: 12, + i: 'nvp', + w: 12, + x: 0, + y: 0, + }, + { + h: 2, + i: 'Q9K', + w: 8, + x: 12, + y: 0, + }, + ], + aliases: {}, + connections: [], + salt: '123', + counter: 1, +}; +const itemsStateAndParamsActionParamTest1 = { + nvp: { + params: { + _ap_year_some_id: ['2022'], + }, + }, + __meta__: { + queue: [ + { + id: 'nvp', + tabId: 'rkq', + }, + ], + version: 2, + }, +}; +const stateAndParamsActionParamsRes1 = { + Q9K: { + params: { + year_some_id: ['2022'], + }, + state: {}, + }, + nvp: { + params: { + _ap_year_some_id: ['2022'], + year_some_id: '', + }, + state: {}, + }, +}; + +const itemsStateAndParamsActionParamTest2 = { + nvp: { + params: { + _ap_another: ['2022'], + }, + }, + __meta__: { + queue: [ + { + id: 'nvp', + tabId: 'rkq', + }, + ], + version: 2, + }, +}; + +const stateAndParamsActionParamsRes2 = { + Q9K: { + params: { + another: ['2022'], + year_some_id: '', + }, + state: {}, + }, + nvp: { + params: { + _ap_another: ['2022'], + year_some_id: '', + }, + state: {}, + }, +}; + +describe('state and params with actionParams', () => { + it('check state', () => { + const res1 = getItemsStateAndParamsDL({ + defaultGlobalParams: {}, + globalParams: {}, + config, + itemsStateAndParams: itemsStateAndParamsActionParamTest1, + }); + expect(res1).toEqual(stateAndParamsActionParamsRes1); + + const res2 = getItemsStateAndParamsDL({ + defaultGlobalParams: {}, + globalParams: {}, + config, + itemsStateAndParams: itemsStateAndParamsActionParamTest2, + }); + expect(res2).toEqual(stateAndParamsActionParamsRes2); + }); +});