useNextId.test.tsx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { renderHook } from '@testing-library/react-hooks';
  2. import React, { PropsWithChildren } from 'react';
  3. import { getDefaultTimeRange } from '@grafana/data';
  4. import { ElasticsearchProvider } from '../components/QueryEditor/ElasticsearchQueryContext';
  5. import { ElasticsearchQuery } from '../types';
  6. import { useNextId } from './useNextId';
  7. describe('useNextId', () => {
  8. it('Should return the next available id', () => {
  9. const query: ElasticsearchQuery = {
  10. refId: 'A',
  11. query: '',
  12. metrics: [{ id: '1', type: 'avg' }],
  13. bucketAggs: [{ id: '2', type: 'date_histogram' }],
  14. };
  15. const wrapper = ({ children }: PropsWithChildren<{}>) => {
  16. return (
  17. <ElasticsearchProvider
  18. query={query}
  19. datasource={{} as any}
  20. onChange={() => {}}
  21. onRunQuery={() => {}}
  22. range={getDefaultTimeRange()}
  23. >
  24. {children}
  25. </ElasticsearchProvider>
  26. );
  27. };
  28. const { result } = renderHook(() => useNextId(), {
  29. wrapper,
  30. });
  31. expect(result.current).toBe('3');
  32. });
  33. });