SearchForm.test.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import { render, screen, waitFor } from '@testing-library/react';
  2. import userEvent from '@testing-library/user-event';
  3. import { UserEvent } from '@testing-library/user-event/dist/types/setup';
  4. import React from 'react';
  5. import { of } from 'rxjs';
  6. import { createFetchResponse } from 'test/helpers/createFetchResponse';
  7. import { DataQueryRequest, DataSourceInstanceSettings, dateTime, PluginType } from '@grafana/data';
  8. import { backendSrv } from 'app/core/services/backend_srv';
  9. import { JaegerDatasource, JaegerJsonData } from '../datasource';
  10. import { testResponse } from '../testResponse';
  11. import { JaegerQuery } from '../types';
  12. import SearchForm from './SearchForm';
  13. describe('SearchForm', () => {
  14. it('should call the `onChange` function on click of the Input', async () => {
  15. const promise = Promise.resolve();
  16. const handleOnChange = jest.fn(() => promise);
  17. const query = {
  18. ...defaultQuery,
  19. refId: '121314',
  20. };
  21. const ds = {
  22. async metadataRequest(url: string, params?: Record<string, any>): Promise<any> {
  23. if (url === '/api/services') {
  24. return Promise.resolve(['jaeger-query', 'service2', 'service3']);
  25. }
  26. },
  27. } as JaegerDatasource;
  28. setupFetchMock({ data: [testResponse] });
  29. render(<SearchForm datasource={ds} query={query} onChange={handleOnChange} />);
  30. const asyncServiceSelect = await waitFor(() => screen.getByRole('combobox', { name: 'select-service-name' }));
  31. expect(asyncServiceSelect).toBeInTheDocument();
  32. await userEvent.click(asyncServiceSelect);
  33. const jaegerService = await screen.findByText('jaeger-query');
  34. expect(jaegerService).toBeInTheDocument();
  35. });
  36. it('should be able to select operation name if query.service exists', async () => {
  37. const promise = Promise.resolve();
  38. const handleOnChange = jest.fn(() => promise);
  39. const query2 = {
  40. ...defaultQuery,
  41. refId: '121314',
  42. service: 'jaeger-query',
  43. };
  44. setupFetchMock({ data: [testResponse] });
  45. render(<SearchForm datasource={{} as JaegerDatasource} query={query2} onChange={handleOnChange} />);
  46. const asyncOperationSelect2 = await waitFor(() => screen.getByRole('combobox', { name: 'select-operation-name' }));
  47. expect(asyncOperationSelect2).toBeInTheDocument();
  48. });
  49. });
  50. describe('SearchForm', () => {
  51. let user: UserEvent;
  52. let query: JaegerQuery;
  53. let ds: JaegerDatasource;
  54. beforeEach(() => {
  55. jest.useFakeTimers();
  56. // Need to use delay: null here to work with fakeTimers
  57. // see https://github.com/testing-library/user-event/issues/833
  58. user = userEvent.setup({ delay: null });
  59. query = {
  60. ...defaultQuery,
  61. refId: '121314',
  62. service: 'jaeger-query',
  63. };
  64. ds = new JaegerDatasource(defaultSettings);
  65. setupFetchMock({ data: [testResponse] });
  66. jest.spyOn(ds, 'metadataRequest').mockImplementation(() => {
  67. return new Promise((resolve) => {
  68. setTimeout(() => {
  69. resolve(['jaeger-query']);
  70. }, 3000);
  71. });
  72. });
  73. });
  74. afterEach(() => {
  75. jest.useRealTimers();
  76. });
  77. it('should show loader if there is a delay fetching options', async () => {
  78. const handleOnChange = jest.fn();
  79. render(<SearchForm datasource={ds} query={query} onChange={handleOnChange} />);
  80. const asyncServiceSelect = screen.getByRole('combobox', { name: 'select-service-name' });
  81. await user.click(asyncServiceSelect);
  82. expect(screen.getByText('Loading options...')).toBeInTheDocument();
  83. jest.advanceTimersByTime(3000);
  84. await waitFor(() => expect(screen.queryByText('Loading options...')).not.toBeInTheDocument());
  85. });
  86. it('should filter the span dropdown when user types a search value', async () => {
  87. render(<SearchForm datasource={ds} query={query} onChange={() => {}} />);
  88. const asyncServiceSelect = screen.getByRole('combobox', { name: 'select-service-name' });
  89. await user.click(asyncServiceSelect);
  90. jest.advanceTimersByTime(3000);
  91. expect(asyncServiceSelect).toBeInTheDocument();
  92. await user.type(asyncServiceSelect, 'j');
  93. var option = await screen.findByText('jaeger-query');
  94. expect(option).toBeDefined();
  95. await user.type(asyncServiceSelect, 'c');
  96. option = await screen.findByText('No options found');
  97. expect(option).toBeDefined();
  98. });
  99. });
  100. function setupFetchMock(response: any, mock?: any) {
  101. const defaultMock = () => mock ?? of(createFetchResponse(response));
  102. const fetchMock = jest.spyOn(backendSrv, 'fetch');
  103. fetchMock.mockImplementation(defaultMock);
  104. return fetchMock;
  105. }
  106. const defaultSettings: DataSourceInstanceSettings<JaegerJsonData> = {
  107. id: 0,
  108. uid: '0',
  109. type: 'tracing',
  110. name: 'jaeger',
  111. url: 'http://grafana.com',
  112. access: 'proxy',
  113. meta: {
  114. id: 'jaeger',
  115. name: 'jaeger',
  116. type: PluginType.datasource,
  117. info: {} as any,
  118. module: '',
  119. baseUrl: '',
  120. },
  121. jsonData: {
  122. nodeGraph: {
  123. enabled: true,
  124. },
  125. },
  126. };
  127. const defaultQuery: DataQueryRequest<JaegerQuery> = {
  128. requestId: '1',
  129. dashboardId: 0,
  130. interval: '0',
  131. intervalMs: 10,
  132. panelId: 0,
  133. scopedVars: {},
  134. range: {
  135. from: dateTime().subtract(1, 'h'),
  136. to: dateTime(),
  137. raw: { from: '1h', to: 'now' },
  138. },
  139. timezone: 'browser',
  140. app: 'explore',
  141. startTime: 0,
  142. targets: [
  143. {
  144. query: '12345',
  145. refId: '1',
  146. },
  147. ],
  148. };