2023-04-11 17:51:54 +08:00
|
|
|
import { renderHook } from '@testing-library/react';
|
2022-04-22 21:33:13 +08:00
|
|
|
import React, { PropsWithChildren } from 'react';
|
|
|
|
|
|
|
|
import { getDefaultTimeRange } from '@grafana/data';
|
|
|
|
|
2020-12-04 22:29:40 +08:00
|
|
|
import { ElasticsearchProvider } from '../components/QueryEditor/ElasticsearchQueryContext';
|
2022-08-10 15:25:35 +08:00
|
|
|
import { ElasticDatasource } from '../datasource';
|
2020-12-04 22:29:40 +08:00
|
|
|
import { ElasticsearchQuery } from '../types';
|
2022-04-22 21:33:13 +08:00
|
|
|
|
|
|
|
import { useNextId } from './useNextId';
|
2020-12-04 22:29:40 +08:00
|
|
|
|
|
|
|
describe('useNextId', () => {
|
|
|
|
it('Should return the next available id', () => {
|
|
|
|
const query: ElasticsearchQuery = {
|
|
|
|
refId: 'A',
|
2021-02-24 05:58:07 +08:00
|
|
|
query: '',
|
2020-12-04 22:29:40 +08:00
|
|
|
metrics: [{ id: '1', type: 'avg' }],
|
|
|
|
bucketAggs: [{ id: '2', type: 'date_histogram' }],
|
|
|
|
};
|
2021-05-04 23:23:19 +08:00
|
|
|
const wrapper = ({ children }: PropsWithChildren<{}>) => {
|
2020-12-04 22:29:40 +08:00
|
|
|
return (
|
2021-04-14 22:18:06 +08:00
|
|
|
<ElasticsearchProvider
|
|
|
|
query={query}
|
2022-08-10 15:25:35 +08:00
|
|
|
datasource={{} as ElasticDatasource}
|
2021-04-14 22:18:06 +08:00
|
|
|
onChange={() => {}}
|
|
|
|
onRunQuery={() => {}}
|
|
|
|
range={getDefaultTimeRange()}
|
|
|
|
>
|
2020-12-04 22:29:40 +08:00
|
|
|
{children}
|
|
|
|
</ElasticsearchProvider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const { result } = renderHook(() => useNextId(), {
|
|
|
|
wrapper,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(result.current).toBe('3');
|
|
|
|
});
|
|
|
|
});
|