Files
grafana/public/app/core/components/QueryOperationRow/QueryOperationRow.test.tsx
Dominik Prokop 712564f66a NewPanelEdit: Add unified UI to queries and transformations (#23478)
* Do not use pointer cursor on icon by default

* Allow items alignment in the HorizontalGroup layout

* Add util for rendering components based on their type (element or function)

* Components for rendering query and transformation rows in a unified way

* Apply new UI fo query and transformation rows

* Add some tests

* Minor fix for scroll area

Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
2020-04-09 21:23:22 +02:00

137 lines
4.1 KiB
TypeScript

import React from 'react';
import { QueryOperationRow } from './QueryOperationRow';
import { shallow, mount } from 'enzyme';
import { act } from 'react-dom/test-utils';
describe('QueryOperationRow', () => {
it('renders', () => {
expect(() =>
shallow(
<QueryOperationRow>
<div>Test</div>
</QueryOperationRow>
)
).not.toThrow();
});
describe('callbacks', () => {
it('should not call onOpen when component is shallowed', async () => {
const onOpenSpy = jest.fn();
await act(async () => {
shallow(
<QueryOperationRow onOpen={onOpenSpy}>
<div>Test</div>
</QueryOperationRow>
);
});
expect(onOpenSpy).not.toBeCalled();
});
it('should call onOpen when row is opened and onClose when row is collapsed', async () => {
const onOpenSpy = jest.fn();
const onCloseSpy = jest.fn();
const wrapper = mount(
<QueryOperationRow onOpen={onOpenSpy} onClose={onCloseSpy} isOpen={false}>
<div>Test</div>
</QueryOperationRow>
);
const titleEl = wrapper.find({ 'aria-label': 'Query operation row title' });
expect(titleEl).toHaveLength(1);
await act(async () => {
// open
titleEl.first().simulate('click');
});
await act(async () => {
// close
titleEl.first().simulate('click');
});
expect(onOpenSpy).toBeCalledTimes(1);
expect(onCloseSpy).toBeCalledTimes(1);
});
});
describe('title rendering', () => {
it('should render title provided as element', () => {
const title = <div aria-label="test title">Test</div>;
const wrapper = shallow(
<QueryOperationRow title={title}>
<div>Test</div>
</QueryOperationRow>
);
const titleEl = wrapper.find({ 'aria-label': 'test title' });
expect(titleEl).toHaveLength(1);
});
it('should render title provided as function', () => {
const title = () => <div aria-label="test title">Test</div>;
const wrapper = shallow(
<QueryOperationRow title={title}>
<div>Test</div>
</QueryOperationRow>
);
const titleEl = wrapper.find({ 'aria-label': 'test title' });
expect(titleEl).toHaveLength(1);
});
it('should expose api to title rendered as function', () => {
const propsSpy = jest.fn();
const title = (props: any) => {
propsSpy(props);
return <div aria-label="test title">Test</div>;
};
shallow(
<QueryOperationRow title={title}>
<div>Test</div>
</QueryOperationRow>
);
expect(Object.keys(propsSpy.mock.calls[0][0])).toContain('isOpen');
});
});
describe('actions rendering', () => {
it('should render actions provided as element', () => {
const actions = <div aria-label="test actions">Test</div>;
const wrapper = shallow(
<QueryOperationRow actions={actions}>
<div>Test</div>
</QueryOperationRow>
);
const actionsEl = wrapper.find({ 'aria-label': 'test actions' });
expect(actionsEl).toHaveLength(1);
});
it('should render actions provided as function', () => {
const actions = () => <div aria-label="test actions">Test</div>;
const wrapper = shallow(
<QueryOperationRow actions={actions}>
<div>Test</div>
</QueryOperationRow>
);
const actionsEl = wrapper.find({ 'aria-label': 'test actions' });
expect(actionsEl).toHaveLength(1);
});
it('should expose api to title rendered as function', () => {
const propsSpy = jest.fn();
const actions = (props: any) => {
propsSpy(props);
return <div aria-label="test actions">Test</div>;
};
shallow(
<QueryOperationRow actions={actions}>
<div>Test</div>
</QueryOperationRow>
);
expect(Object.keys(propsSpy.mock.calls[0][0])).toContainEqual('isOpen');
expect(Object.keys(propsSpy.mock.calls[0][0])).toContainEqual('openRow');
expect(Object.keys(propsSpy.mock.calls[0][0])).toContainEqual('closeRow');
});
});
});