mirror of
https://github.com/grafana/grafana.git
synced 2025-09-22 20:37:05 +08:00

Co-authored-by: Zoltán Bedi <zoltan.bedi@gmail.com> Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import React, { FunctionComponent } from 'react';
|
|
import { renderHook } from '@testing-library/react-hooks';
|
|
import { ElasticsearchProvider, useDatasource, useQuery } from './ElasticsearchQueryContext';
|
|
import { ElasticsearchQuery } from '../../types';
|
|
import { ElasticDatasource } from '../../datasource';
|
|
|
|
const query: ElasticsearchQuery = {
|
|
refId: 'A',
|
|
metrics: [{ id: '1', type: 'count' }],
|
|
bucketAggs: [{ type: 'date_histogram', id: '2' }],
|
|
};
|
|
|
|
describe('ElasticsearchQueryContext', () => {
|
|
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: FunctionComponent = ({ children }) => (
|
|
<ElasticsearchProvider datasource={{} as ElasticDatasource} query={query} onChange={() => {}}>
|
|
{children}
|
|
</ElasticsearchProvider>
|
|
);
|
|
|
|
const { result } = renderHook(() => useQuery(), {
|
|
wrapper,
|
|
});
|
|
|
|
expect(result.current).toBe(query);
|
|
});
|
|
});
|
|
|
|
describe('useDatasource Hook', () => {
|
|
it('Should throw when used outside of ElasticsearchQueryContext', () => {
|
|
const { result } = renderHook(() => useDatasource());
|
|
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('Should return the current datasource instance', () => {
|
|
const datasource = {} as ElasticDatasource;
|
|
|
|
const wrapper: FunctionComponent = ({ children }) => (
|
|
<ElasticsearchProvider datasource={datasource} query={query} onChange={() => {}}>
|
|
{children}
|
|
</ElasticsearchProvider>
|
|
);
|
|
|
|
const { result } = renderHook(() => useDatasource(), {
|
|
wrapper,
|
|
});
|
|
|
|
expect(result.current).toBe(datasource);
|
|
});
|
|
});
|
|
});
|