Test: element filter expressions (#33908)

* test: add more filter elements

* test: add getGlobalFilterExpression test

* chore: only iterate filter elements once
This commit is contained in:
Joe Fleming 2019-03-28 15:03:32 -07:00 committed by GitHub
parent 7420e2ee65
commit 65a5d528b1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 2 deletions

View file

@ -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 = {
@ -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',
},
],
},
@ -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],
@ -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);

View file

@ -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(' | ');
}