mirror of
https://github.com/grafana/grafana.git
synced 2025-09-22 10:42:52 +08:00

* Add and configure eslint-plugin-import * Fix the lint:ts npm command * Autofix + prettier all the files * Manually fix remaining files * Move jquery code in jest-setup to external file to safely reorder imports * Resolve issue caused by circular dependencies within Prometheus * Update .betterer.results * Fix missing // @ts-ignore * ignore iconBundle.ts * Fix missing // @ts-ignore
76 lines
2.3 KiB
TypeScript
76 lines
2.3 KiB
TypeScript
import { render } from '@testing-library/react';
|
|
import { renderHook } from '@testing-library/react-hooks';
|
|
import React, { PropsWithChildren } from 'react';
|
|
|
|
import { getDefaultTimeRange } from '@grafana/data';
|
|
|
|
import { ElasticDatasource } from '../../datasource';
|
|
import { ElasticsearchQuery } from '../../types';
|
|
|
|
import { ElasticsearchProvider, useQuery } from './ElasticsearchQueryContext';
|
|
|
|
const query: ElasticsearchQuery = {
|
|
refId: 'A',
|
|
query: '',
|
|
metrics: [{ id: '1', type: 'count' }],
|
|
bucketAggs: [{ type: 'date_histogram', id: '2' }],
|
|
};
|
|
|
|
describe('ElasticsearchQueryContext', () => {
|
|
it('Should call onChange and onRunQuery with the default query when the query is empty', () => {
|
|
const datasource = { timeField: 'TIMEFIELD' } as ElasticDatasource;
|
|
const onChange = jest.fn();
|
|
const onRunQuery = jest.fn();
|
|
|
|
render(
|
|
<ElasticsearchProvider
|
|
query={{ refId: 'A' }}
|
|
onChange={onChange}
|
|
datasource={datasource}
|
|
onRunQuery={onRunQuery}
|
|
range={getDefaultTimeRange()}
|
|
/>
|
|
);
|
|
|
|
const changedQuery: ElasticsearchQuery = onChange.mock.calls[0][0];
|
|
expect(changedQuery.query).toBeDefined();
|
|
expect(changedQuery.alias).toBeDefined();
|
|
expect(changedQuery.metrics).toBeDefined();
|
|
expect(changedQuery.bucketAggs).toBeDefined();
|
|
|
|
// Should also set timeField to the configured `timeField` option in datasource configuration
|
|
expect(changedQuery.timeField).toBe(datasource.timeField);
|
|
|
|
expect(onRunQuery).toHaveBeenCalled();
|
|
});
|
|
|
|
// the following applies to all hooks in ElasticsearchQueryContext as they all share the same code.
|
|
describe('useQuery Hook', () => {
|
|
it('Should throw when used outside of ElasticsearchQueryContext', () => {
|
|
const { result } = renderHook(() => useQuery());
|
|
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('Should return the current query object', () => {
|
|
const wrapper = ({ children }: PropsWithChildren<{}>) => (
|
|
<ElasticsearchProvider
|
|
datasource={{} as ElasticDatasource}
|
|
query={query}
|
|
onChange={() => {}}
|
|
onRunQuery={() => {}}
|
|
range={getDefaultTimeRange()}
|
|
>
|
|
{children}
|
|
</ElasticsearchProvider>
|
|
);
|
|
|
|
const { result } = renderHook(() => useQuery(), {
|
|
wrapper,
|
|
});
|
|
|
|
expect(result.current).toBe(query);
|
|
});
|
|
});
|
|
});
|