diff --git a/ui/mocks/ifql/apis/index.ts b/ui/mocks/ifql/apis/index.ts index 2751cf48b4..2fd7bba2ef 100644 --- a/ui/mocks/ifql/apis/index.ts +++ b/ui/mocks/ifql/apis/index.ts @@ -1,3 +1,4 @@ jest.mock('src/utils/ajax', () => require('mocks/utils/ajax')) export const getSuggestions = jest.fn(() => Promise.resolve([])) +export const getAST = jest.fn(() => Promise.resolve({})) diff --git a/ui/src/ifql/ast/walker.ts b/ui/src/ifql/ast/walker.ts index f42c90fb41..d04755af59 100644 --- a/ui/src/ifql/ast/walker.ts +++ b/ui/src/ifql/ast/walker.ts @@ -1,5 +1,5 @@ // Texas Ranger -import {get} from 'lodash' +import _ from 'lodash' interface Expression { expression: object @@ -32,6 +32,10 @@ export default class Walker { } private walk = currentNode => { + if (_.isEmpty(currentNode)) { + return [] + } + let name let args if (currentNode.call) { @@ -57,11 +61,15 @@ export default class Walker { private getProperties = props => { return props.map(prop => ({ key: prop.key.name, - value: get(prop, 'value.value', get(prop, 'value.location.source', '')), + value: _.get( + prop, + 'value.value', + _.get(prop, 'value.location.source', '') + ), })) } private get baseExpression() { - return this.ast.body[0].expression + return _.get(this.ast, 'body.0.expression', {}) } } diff --git a/ui/src/ifql/components/FuncSelector.tsx b/ui/src/ifql/components/FuncSelector.tsx index 46ca1711d0..67b2e1f4c0 100644 --- a/ui/src/ifql/components/FuncSelector.tsx +++ b/ui/src/ifql/components/FuncSelector.tsx @@ -24,7 +24,6 @@ export class FuncSelector extends PureComponent { } public render() { - const {onAddNode} = this.props const {isOpen, inputText} = this.state return ( diff --git a/ui/test/ifql/ast/walker.test.ts b/ui/test/ifql/ast/walker.test.ts index 535023d6b4..b614bbe1b6 100644 --- a/ui/test/ifql/ast/walker.test.ts +++ b/ui/test/ifql/ast/walker.test.ts @@ -12,7 +12,7 @@ describe('IFQL.AST.Walker', () => { name: 'from', arguments: [ { - name: 'db', + key: 'db', value: 'telegraf', }, ], @@ -27,20 +27,20 @@ describe('IFQL.AST.Walker', () => { expect(walker.functions).toEqual([ { name: 'from', - arguments: [{name: 'db', value: 'telegraf'}], + arguments: [{key: 'db', value: 'telegraf'}], }, { name: 'filter', arguments: [ { - name: 'fn', + key: 'fn', value: '(r) => r["_measurement"] == "cpu"', }, ], }, { name: 'range', - arguments: [{name: 'start', value: '-1m'}], + arguments: [{key: 'start', value: '-1m'}], }, ]) }) diff --git a/ui/test/ifql/components/FuncSelector.test.tsx b/ui/test/ifql/components/FuncSelector.test.tsx index 0eb205bf2d..ad0a1be368 100644 --- a/ui/test/ifql/components/FuncSelector.test.tsx +++ b/ui/test/ifql/components/FuncSelector.test.tsx @@ -2,6 +2,8 @@ import React from 'react' import {shallow} from 'enzyme' import {FuncSelector} from 'src/ifql/components/FuncSelector' import DropdownInput from 'src/shared/components/DropdownInput' +import FuncListItem from 'src/ifql/components/FuncListItem' +import FuncList from 'src/ifql/components/FuncList' const setup = (override = {}) => { const props = { @@ -44,11 +46,17 @@ describe('IFQL.Components.FuncsButton', () => { const dropdownButton = wrapper.find('button') dropdownButton.simulate('click') - const list = wrapper.find('.func') + const list = wrapper + .find(FuncList) + .dive() + .find(FuncListItem) + + const first = list.first().dive() + const last = list.last().dive() expect(list.length).toBe(2) - expect(list.first().text()).toBe('f1') - expect(list.last().text()).toBe('f2') + expect(first.text()).toBe('f1') + expect(last.text()).toBe('f2') }) }) @@ -59,13 +67,21 @@ describe('IFQL.Components.FuncsButton', () => { const dropdownButton = wrapper.find('button') dropdownButton.simulate('click') - let list = wrapper.find('.func') + let list = wrapper + .find(FuncList) + .dive() + .find(FuncListItem) + + const first = list.first().dive() + const last = list.last().dive() expect(list.length).toBe(2) - expect(list.first().text()).toBe('f1') - expect(list.last().text()).toBe('f2') + expect(first.text()).toBe('f1') + expect(last.text()).toBe('f2') const input = wrapper + .find(FuncList) + .dive() .find(DropdownInput) .dive() .find('input') @@ -73,10 +89,15 @@ describe('IFQL.Components.FuncsButton', () => { input.simulate('change', {target: {value: '2'}}) wrapper.update() - list = wrapper.find('.func') + list = wrapper + .find(FuncList) + .dive() + .find(FuncListItem) + + const func = list.first().dive() expect(list.length).toBe(1) - expect(list.first().text()).toBe('f2') + expect(func.text()).toBe('f2') }) }) @@ -86,11 +107,17 @@ describe('IFQL.Components.FuncsButton', () => { const dropdownButton = wrapper.find('button') dropdownButton.simulate('click') - let list = wrapper.find('.func') + + let list = wrapper + .find(FuncList) + .dive() + .find(FuncListItem) expect(list.exists()).toBe(true) const input = wrapper + .find(FuncList) + .dive() .find(DropdownInput) .dive() .find('input') @@ -98,7 +125,10 @@ describe('IFQL.Components.FuncsButton', () => { input.simulate('keyDown', {key: 'Escape'}) wrapper.update() - list = wrapper.find('.func') + list = wrapper + .find(FuncList) + .dive() + .find(FuncListItem) expect(list.exists()).toBe(false) }) diff --git a/ui/test/ifql/components/TimeMachine.test.tsx b/ui/test/ifql/components/TimeMachine.test.tsx index 2e64c0677e..92a448bbe6 100644 --- a/ui/test/ifql/components/TimeMachine.test.tsx +++ b/ui/test/ifql/components/TimeMachine.test.tsx @@ -6,6 +6,7 @@ const setup = () => { const props = { funcs: [], ast: {}, + nodes: [], } const wrapper = shallow()