forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chore: convert last test to RTL and remove Enzyme references (grafana…
…#61918) convert last test to RTL and remove enzyme references
- Loading branch information
1 parent
479da46
commit 92a750a
Showing
10 changed files
with
98 additions
and
452 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 79 additions & 98 deletions
177
packages/grafana-ui/src/components/QueryField/QueryField.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,127 +1,108 @@ | ||
import { mount, shallow } from 'enzyme'; | ||
import { render } from '@testing-library/react'; | ||
import React from 'react'; | ||
import { Editor } from 'slate-react'; | ||
|
||
import { createTheme } from '@grafana/data'; | ||
|
||
import { UnThemedQueryField } from './QueryField'; | ||
|
||
describe('<QueryField />', () => { | ||
it('should render with null initial value', () => { | ||
const wrapper = shallow( | ||
<UnThemedQueryField theme={createTheme()} query={null} onTypeahead={jest.fn()} portalOrigin="mock-origin" /> | ||
); | ||
expect(wrapper.find('div').exists()).toBeTruthy(); | ||
expect(() => | ||
render( | ||
<UnThemedQueryField theme={createTheme()} query={null} onTypeahead={jest.fn()} portalOrigin="mock-origin" /> | ||
) | ||
).not.toThrow(); | ||
}); | ||
|
||
it('should render with empty initial value', () => { | ||
const wrapper = shallow( | ||
<UnThemedQueryField theme={createTheme()} query="" onTypeahead={jest.fn()} portalOrigin="mock-origin" /> | ||
); | ||
expect(wrapper.find('div').exists()).toBeTruthy(); | ||
expect(() => | ||
render(<UnThemedQueryField theme={createTheme()} query="" onTypeahead={jest.fn()} portalOrigin="mock-origin" />) | ||
).not.toThrow(); | ||
}); | ||
|
||
it('should render with initial value', () => { | ||
const wrapper = shallow( | ||
<UnThemedQueryField theme={createTheme()} query="my query" onTypeahead={jest.fn()} portalOrigin="mock-origin" /> | ||
); | ||
expect(wrapper.find('div').exists()).toBeTruthy(); | ||
expect(() => | ||
render( | ||
<UnThemedQueryField theme={createTheme()} query="my query" onTypeahead={jest.fn()} portalOrigin="mock-origin" /> | ||
) | ||
).not.toThrow(); | ||
}); | ||
|
||
it('should execute query on blur', () => { | ||
const onRun = jest.fn(); | ||
const wrapper = mount( | ||
<UnThemedQueryField | ||
theme={createTheme()} | ||
query="my query" | ||
onTypeahead={jest.fn()} | ||
onRunQuery={onRun} | ||
portalOrigin="mock-origin" | ||
/> | ||
); | ||
const field = wrapper.instance() as UnThemedQueryField; | ||
const ed = wrapper.find(Editor).instance() as Editor; | ||
expect(onRun.mock.calls.length).toBe(0); | ||
field.handleBlur(undefined, ed, () => {}); | ||
expect(onRun.mock.calls.length).toBe(1); | ||
}); | ||
|
||
it('should run onChange with clean text', () => { | ||
const onChange = jest.fn(); | ||
const wrapper = shallow( | ||
<UnThemedQueryField | ||
theme={createTheme()} | ||
query={`my\r clean query `} | ||
onTypeahead={jest.fn()} | ||
onChange={onChange} | ||
portalOrigin="mock-origin" | ||
/> | ||
); | ||
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 '); | ||
}); | ||
|
||
it('should run custom on blur, but not necessarily execute query', () => { | ||
const onBlur = jest.fn(); | ||
const onRun = jest.fn(); | ||
const wrapper = mount( | ||
<UnThemedQueryField | ||
theme={createTheme()} | ||
query="my query" | ||
onTypeahead={jest.fn()} | ||
onBlur={onBlur} | ||
onRunQuery={onRun} | ||
portalOrigin="mock-origin" | ||
/> | ||
); | ||
const field = wrapper.instance() as UnThemedQueryField; | ||
const ed = wrapper.find(Editor).instance() as Editor; | ||
expect(onBlur.mock.calls.length).toBe(0); | ||
expect(onRun.mock.calls.length).toBe(0); | ||
field.handleBlur(undefined, ed, () => {}); | ||
expect(onBlur.mock.calls.length).toBe(1); | ||
expect(onRun.mock.calls.length).toBe(0); | ||
}); | ||
describe('syntaxLoaded', () => { | ||
it('should re-render the editor after syntax has fully loaded', () => { | ||
const wrapper: any = shallow( | ||
<UnThemedQueryField theme={createTheme()} query="my query" portalOrigin="mock-origin" /> | ||
const mockOnRichValueChange = jest.fn(); | ||
const { rerender } = render( | ||
<UnThemedQueryField | ||
theme={createTheme()} | ||
query="my query" | ||
onRichValueChange={mockOnRichValueChange} | ||
portalOrigin="mock-origin" | ||
/> | ||
); | ||
rerender( | ||
<UnThemedQueryField | ||
theme={createTheme()} | ||
query="my query" | ||
syntaxLoaded | ||
onRichValueChange={mockOnRichValueChange} | ||
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); | ||
expect(mockOnRichValueChange).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not re-render the editor if syntax is already loaded', () => { | ||
const wrapper: any = shallow( | ||
<UnThemedQueryField theme={createTheme()} query="my query" portalOrigin="mock-origin" /> | ||
const mockOnRichValueChange = jest.fn(); | ||
const { rerender } = render( | ||
<UnThemedQueryField | ||
theme={createTheme()} | ||
query="my query" | ||
onRichValueChange={mockOnRichValueChange} | ||
syntaxLoaded | ||
portalOrigin="mock-origin" | ||
/> | ||
); | ||
const spyOnChange = jest.spyOn(wrapper.instance(), 'onChange').mockImplementation(jest.fn()); | ||
wrapper.setProps({ syntaxLoaded: true }); | ||
wrapper.instance().editor = {}; | ||
wrapper.setProps({ syntaxLoaded: true }); | ||
expect(spyOnChange).not.toBeCalled(); | ||
}); | ||
it('should not re-render the editor if editor itself is not defined', () => { | ||
const wrapper: any = shallow( | ||
<UnThemedQueryField theme={createTheme()} query="my query" portalOrigin="mock-origin" /> | ||
rerender( | ||
<UnThemedQueryField | ||
theme={createTheme()} | ||
query="my query" | ||
onRichValueChange={mockOnRichValueChange} | ||
syntaxLoaded | ||
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(); | ||
expect(mockOnRichValueChange).not.toBeCalled(); | ||
}); | ||
|
||
it('should not re-render the editor twice once syntax is fully loaded', () => { | ||
const wrapper: any = shallow( | ||
<UnThemedQueryField theme={createTheme()} query="my query" portalOrigin="mock-origin" /> | ||
const mockOnRichValueChange = jest.fn(); | ||
const { rerender } = render( | ||
<UnThemedQueryField | ||
theme={createTheme()} | ||
onRichValueChange={mockOnRichValueChange} | ||
query="my query" | ||
portalOrigin="mock-origin" | ||
/> | ||
); | ||
rerender( | ||
<UnThemedQueryField | ||
theme={createTheme()} | ||
syntaxLoaded | ||
onRichValueChange={mockOnRichValueChange} | ||
query="my query" | ||
portalOrigin="mock-origin" | ||
/> | ||
); | ||
rerender( | ||
<UnThemedQueryField | ||
theme={createTheme()} | ||
syntaxLoaded | ||
onRichValueChange={mockOnRichValueChange} | ||
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 }); | ||
wrapper.setProps({ syntaxLoaded: true }); | ||
expect(spyOnChange).toBeCalledTimes(1); | ||
expect(mockOnRichValueChange).toBeCalledTimes(1); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.