|
|
|
@ -1,30 +1,43 @@ |
|
|
|
|
import React from 'react'; |
|
|
|
|
import { shallow } from 'enzyme'; |
|
|
|
|
import { QueryField } from './QueryField'; |
|
|
|
|
import { UnThemedQueryField } from './QueryField'; |
|
|
|
|
import { Editor } from 'slate'; |
|
|
|
|
import { createTheme } from '@grafana/data'; |
|
|
|
|
|
|
|
|
|
describe('<QueryField />', () => { |
|
|
|
|
it('should render with null initial value', () => { |
|
|
|
|
const wrapper = shallow(<QueryField query={null} onTypeahead={jest.fn()} portalOrigin="mock-origin" />); |
|
|
|
|
const wrapper = shallow( |
|
|
|
|
<UnThemedQueryField theme={createTheme()} query={null} onTypeahead={jest.fn()} portalOrigin="mock-origin" /> |
|
|
|
|
); |
|
|
|
|
expect(wrapper.find('div').exists()).toBeTruthy(); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should render with empty initial value', () => { |
|
|
|
|
const wrapper = shallow(<QueryField query="" onTypeahead={jest.fn()} portalOrigin="mock-origin" />); |
|
|
|
|
const wrapper = shallow( |
|
|
|
|
<UnThemedQueryField theme={createTheme()} query="" onTypeahead={jest.fn()} portalOrigin="mock-origin" /> |
|
|
|
|
); |
|
|
|
|
expect(wrapper.find('div').exists()).toBeTruthy(); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should render with initial value', () => { |
|
|
|
|
const wrapper = shallow(<QueryField query="my query" onTypeahead={jest.fn()} portalOrigin="mock-origin" />); |
|
|
|
|
const wrapper = shallow( |
|
|
|
|
<UnThemedQueryField theme={createTheme()} query="my query" onTypeahead={jest.fn()} portalOrigin="mock-origin" /> |
|
|
|
|
); |
|
|
|
|
expect(wrapper.find('div').exists()).toBeTruthy(); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should execute query on blur', () => { |
|
|
|
|
const onRun = jest.fn(); |
|
|
|
|
const wrapper = shallow( |
|
|
|
|
<QueryField query="my query" onTypeahead={jest.fn()} onRunQuery={onRun} portalOrigin="mock-origin" /> |
|
|
|
|
<UnThemedQueryField |
|
|
|
|
theme={createTheme()} |
|
|
|
|
query="my query" |
|
|
|
|
onTypeahead={jest.fn()} |
|
|
|
|
onRunQuery={onRun} |
|
|
|
|
portalOrigin="mock-origin" |
|
|
|
|
/> |
|
|
|
|
); |
|
|
|
|
const field = wrapper.instance() as QueryField; |
|
|
|
|
const field = wrapper.instance() as UnThemedQueryField; |
|
|
|
|
expect(onRun.mock.calls.length).toBe(0); |
|
|
|
|
field.handleBlur(new Event('bogus'), new Editor({}), () => {}); |
|
|
|
|
expect(onRun.mock.calls.length).toBe(1); |
|
|
|
@ -33,9 +46,15 @@ describe('<QueryField />', () => { |
|
|
|
|
it('should run onChange with clean text', () => { |
|
|
|
|
const onChange = jest.fn(); |
|
|
|
|
const wrapper = shallow( |
|
|
|
|
<QueryField query={`my\r clean query `} onTypeahead={jest.fn()} onChange={onChange} portalOrigin="mock-origin" /> |
|
|
|
|
<UnThemedQueryField |
|
|
|
|
theme={createTheme()} |
|
|
|
|
query={`my\r clean query `} |
|
|
|
|
onTypeahead={jest.fn()} |
|
|
|
|
onChange={onChange} |
|
|
|
|
portalOrigin="mock-origin" |
|
|
|
|
/> |
|
|
|
|
); |
|
|
|
|
const field = wrapper.instance() as QueryField; |
|
|
|
|
const field = wrapper.instance() as UnThemedQueryField; |
|
|
|
|
field.runOnChange(); |
|
|
|
|
expect(onChange.mock.calls.length).toBe(1); |
|
|
|
|
expect(onChange.mock.calls[0][0]).toBe('my clean query '); |
|
|
|
@ -45,7 +64,8 @@ describe('<QueryField />', () => { |
|
|
|
|
const onBlur = jest.fn(); |
|
|
|
|
const onRun = jest.fn(); |
|
|
|
|
const wrapper = shallow( |
|
|
|
|
<QueryField |
|
|
|
|
<UnThemedQueryField |
|
|
|
|
theme={createTheme()} |
|
|
|
|
query="my query" |
|
|
|
|
onTypeahead={jest.fn()} |
|
|
|
|
onBlur={onBlur} |
|
|
|
@ -53,7 +73,7 @@ describe('<QueryField />', () => { |
|
|
|
|
portalOrigin="mock-origin" |
|
|
|
|
/> |
|
|
|
|
); |
|
|
|
|
const field = wrapper.instance() as QueryField; |
|
|
|
|
const field = wrapper.instance() as UnThemedQueryField; |
|
|
|
|
expect(onBlur.mock.calls.length).toBe(0); |
|
|
|
|
expect(onRun.mock.calls.length).toBe(0); |
|
|
|
|
field.handleBlur(new Event('bogus'), new Editor({}), () => {}); |
|
|
|
@ -62,14 +82,18 @@ describe('<QueryField />', () => { |
|
|
|
|
}); |
|
|
|
|
describe('syntaxLoaded', () => { |
|
|
|
|
it('should re-render the editor after syntax has fully loaded', () => { |
|
|
|
|
const wrapper: any = shallow(<QueryField query="my query" portalOrigin="mock-origin" />); |
|
|
|
|
const wrapper: any = shallow( |
|
|
|
|
<UnThemedQueryField theme={createTheme()} query="my query" portalOrigin="mock-origin" /> |
|
|
|
|
); |
|
|
|
|
const spyOnChange = jest.spyOn(wrapper.instance(), 'onChange').mockImplementation(jest.fn()); |
|
|
|
|
wrapper.instance().editor = { insertText: () => ({ deleteBackward: () => ({ value: 'fooo' }) }) }; |
|
|
|
|
wrapper.setProps({ syntaxLoaded: true }); |
|
|
|
|
expect(spyOnChange).toHaveBeenCalledWith('fooo', true); |
|
|
|
|
}); |
|
|
|
|
it('should not re-render the editor if syntax is already loaded', () => { |
|
|
|
|
const wrapper: any = shallow(<QueryField query="my query" portalOrigin="mock-origin" />); |
|
|
|
|
const wrapper: any = shallow( |
|
|
|
|
<UnThemedQueryField theme={createTheme()} query="my query" portalOrigin="mock-origin" /> |
|
|
|
|
); |
|
|
|
|
const spyOnChange = jest.spyOn(wrapper.instance(), 'onChange').mockImplementation(jest.fn()); |
|
|
|
|
wrapper.setProps({ syntaxLoaded: true }); |
|
|
|
|
wrapper.instance().editor = {}; |
|
|
|
@ -77,14 +101,18 @@ describe('<QueryField />', () => { |
|
|
|
|
expect(spyOnChange).not.toBeCalled(); |
|
|
|
|
}); |
|
|
|
|
it('should not re-render the editor if editor itself is not defined', () => { |
|
|
|
|
const wrapper: any = shallow(<QueryField query="my query" portalOrigin="mock-origin" />); |
|
|
|
|
const wrapper: any = shallow( |
|
|
|
|
<UnThemedQueryField theme={createTheme()} query="my query" portalOrigin="mock-origin" /> |
|
|
|
|
); |
|
|
|
|
const spyOnChange = jest.spyOn(wrapper.instance(), 'onChange').mockImplementation(jest.fn()); |
|
|
|
|
wrapper.setProps({ syntaxLoaded: true }); |
|
|
|
|
expect(wrapper.instance().editor).toBeFalsy(); |
|
|
|
|
expect(spyOnChange).not.toBeCalled(); |
|
|
|
|
}); |
|
|
|
|
it('should not re-render the editor twice once syntax is fully loaded', () => { |
|
|
|
|
const wrapper: any = shallow(<QueryField query="my query" portalOrigin="mock-origin" />); |
|
|
|
|
const wrapper: any = shallow( |
|
|
|
|
<UnThemedQueryField theme={createTheme()} query="my query" portalOrigin="mock-origin" /> |
|
|
|
|
); |
|
|
|
|
const spyOnChange = jest.spyOn(wrapper.instance(), 'onChange').mockImplementation(jest.fn()); |
|
|
|
|
wrapper.instance().editor = { insertText: () => ({ deleteBackward: () => ({ value: 'fooo' }) }) }; |
|
|
|
|
wrapper.setProps({ syntaxLoaded: true }); |
|
|
|
|