Skip to content

Commit

Permalink
fix: filtering action params logic, add test (#108)
Browse files Browse the repository at this point in the history
* refactor: fix filtering action params logic, add test

* fix: action params state and params tests
  • Loading branch information
Marginy605 authored Dec 14, 2023
1 parent 480d75d commit 5deded9
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 14 deletions.
15 changes: 1 addition & 14 deletions src/shared/modules/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
152 changes: 152 additions & 0 deletions src/shared/units/__tests__/state-and-params.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 5deded9

Please sign in to comment.