QueryField.test.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { render, screen } from '@testing-library/react';
  2. import { act, renderHook } from '@testing-library/react-hooks';
  3. import React from 'react';
  4. import { CascaderOption } from '@grafana/ui';
  5. import { ZipkinQueryField, useLoadOptions, useServices } from './QueryField';
  6. import { ZipkinDatasource } from './datasource';
  7. import { ZipkinQuery } from './types';
  8. describe('QueryField', () => {
  9. it('renders properly', async () => {
  10. const ds = {} as ZipkinDatasource;
  11. render(
  12. <ZipkinQueryField
  13. history={[]}
  14. datasource={ds}
  15. query={{ query: '1234' } as ZipkinQuery}
  16. onRunQuery={() => {}}
  17. onChange={() => {}}
  18. />
  19. );
  20. expect(await screen.findByText(/1234/i)).toBeInTheDocument();
  21. expect(await screen.findByText(/Traces/i)).toBeInTheDocument();
  22. });
  23. });
  24. describe('useServices', () => {
  25. it('returns services from datasource', async () => {
  26. const ds = {
  27. async metadataRequest(url: string, params?: Record<string, any>): Promise<any> {
  28. if (url === '/api/v2/services') {
  29. return Promise.resolve(['service1', 'service2']);
  30. }
  31. },
  32. } as ZipkinDatasource;
  33. const { result, waitForNextUpdate } = renderHook(() => useServices(ds));
  34. await waitForNextUpdate();
  35. expect(result.current.value).toEqual([
  36. { label: 'service1', value: 'service1', isLeaf: false },
  37. { label: 'service2', value: 'service2', isLeaf: false },
  38. ]);
  39. });
  40. });
  41. describe('useLoadOptions', () => {
  42. it('loads spans and traces', async () => {
  43. const ds = {
  44. async metadataRequest(url: string, params?: Record<string, any>): Promise<any> {
  45. if (url === '/api/v2/spans' && params?.serviceName === 'service1') {
  46. return Promise.resolve(['span1', 'span2']);
  47. }
  48. if (url === '/api/v2/traces' && params?.serviceName === 'service1' && params?.spanName === 'span1') {
  49. return Promise.resolve([[{ name: 'trace1', duration: 10_000, traceId: 'traceId1' }]]);
  50. }
  51. },
  52. } as ZipkinDatasource;
  53. const { result, waitForNextUpdate } = renderHook(() => useLoadOptions(ds));
  54. expect(result.current.allOptions).toEqual({});
  55. act(() => {
  56. result.current.onLoadOptions([{ value: 'service1' } as CascaderOption]);
  57. });
  58. await waitForNextUpdate();
  59. expect(result.current.allOptions).toEqual({ service1: { span1: undefined, span2: undefined } });
  60. act(() => {
  61. result.current.onLoadOptions([{ value: 'service1' } as CascaderOption, { value: 'span1' } as CascaderOption]);
  62. });
  63. await waitForNextUpdate();
  64. expect(result.current.allOptions).toEqual({
  65. service1: { span1: { 'trace1 [10 ms]': 'traceId1' }, span2: undefined },
  66. });
  67. });
  68. });