Skip to content

Commit

Permalink
Test: element filter expressions (#33908) (#34126)
Browse files Browse the repository at this point in the history
* test: add more filter elements

* test: add getGlobalFilterExpression test

* chore: only iterate filter elements once
  • Loading branch information
w33ble authored Mar 28, 2019
1 parent 9abeb16 commit 91625d0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
56 changes: 56 additions & 0 deletions x-pack/plugins/canvas/public/state/selectors/__tests__/workpad.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,39 @@ describe('workpad selectors', () => {
},
],
},
'element-3': {
type: 'expression',
chain: [
{
type: 'function',
function: 'demodata',
arguments: {},
},
{
type: 'function',
function: 'dropdownControl',
arguments: {
valueColumn: ['project'],
filterColumn: ['project'],
},
},
{
type: 'function',
function: 'render',
arguments: {},
},
],
},
'element-4': {
type: 'expression',
chain: [
{
type: 'function',
function: 'timefilterControl',
arguments: { compact: [true], column: ['@timestamp'] },
},
],
},
};

state = {
Expand Down Expand Up @@ -63,10 +96,23 @@ describe('workpad selectors', () => {
{
id: 'element-0',
expression: 'markdown',
filter: '',
},
{
id: 'element-1',
expression: 'demodata',
filter: '',
},
{
id: 'element-3',
expression:
'demodata | dropdownControl valueColumn=project filterColumn=project | render',
filter: 'exactly value="beats" column="project"',
},
{
id: 'element-4',
expression: 'timefilterControl compact=true column=@timestamp',
filter: 'timefilter column=@timestamp from=now-24h to=now',
},
],
},
Expand Down Expand Up @@ -135,6 +181,7 @@ describe('workpad selectors', () => {

it('returns all elements on the page', () => {
const { elements } = state.persistent.workpad.pages[0];

const expected = elements.map(element => ({
...element,
ast: asts[element.id],
Expand Down Expand Up @@ -198,6 +245,15 @@ describe('workpad selectors', () => {
});
});

describe('getGlobalFilterExpression', () => {
it('gets filters from all elements', () => {
const filters = selector.getGlobalFilterExpression(state);
expect(filters).to.equal(
'exactly value="beats" column="project" | timefilter column=@timestamp from=now-24h to=now'
);
});
});

describe('isWriteable', () => {
it('returns boolean for if the workpad is writeable', () => {
expect(selector.isWriteable(state)).to.equal(false);
Expand Down
10 changes: 8 additions & 2 deletions x-pack/plugins/canvas/public/state/selectors/workpad.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,14 @@ export function getElementStats(state) {

export function getGlobalFilterExpression(state) {
return getAllElements(state)
.map(el => el.filter)
.filter(str => str != null && str.length)
.reduce((acc, el) => {
// check that a filter is defined
if (el.filter != null && el.filter.length) {
return acc.concat(el.filter);
}

return acc;
}, [])
.join(' | ');
}

Expand Down

0 comments on commit 91625d0

Please sign in to comment.