QueryEditor.test.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { fireEvent, render, screen } from '@testing-library/react';
  2. import userEvent from '@testing-library/user-event';
  3. import React from 'react';
  4. import { QueryEditor, Props } from './QueryEditor';
  5. import { scenarios } from './__mocks__/scenarios';
  6. import { defaultQuery } from './constants';
  7. import { defaultStreamQuery } from './runStreams';
  8. beforeEach(() => {
  9. jest.clearAllMocks();
  10. });
  11. const mockOnChange = jest.fn();
  12. const props = {
  13. onRunQuery: jest.fn(),
  14. query: defaultQuery,
  15. onChange: mockOnChange,
  16. datasource: {
  17. getScenarios: () => Promise.resolve(scenarios),
  18. } as any,
  19. };
  20. const setup = (testProps?: Partial<Props>) => {
  21. const editorProps = { ...props, ...testProps };
  22. return render(<QueryEditor {...editorProps} />);
  23. };
  24. describe('Test Datasource Query Editor', () => {
  25. it('should render with default scenario', async () => {
  26. setup();
  27. expect(await screen.findByText(/random walk/i)).toBeInTheDocument();
  28. expect(screen.getByRole('textbox', { name: 'Alias' })).toBeInTheDocument();
  29. expect(screen.getByRole('textbox', { name: 'Labels' })).toBeInTheDocument();
  30. });
  31. it('should switch scenario and display its default values', async () => {
  32. const { rerender } = setup();
  33. let select = (await screen.findByText('Scenario')).nextSibling!.firstChild!;
  34. await fireEvent.keyDown(select, { keyCode: 40 });
  35. const scs = screen.getAllByLabelText('Select option');
  36. expect(scs).toHaveLength(scenarios.length);
  37. await userEvent.click(screen.getByText('CSV Metric Values'));
  38. expect(mockOnChange).toHaveBeenCalledWith(expect.objectContaining({ scenarioId: 'csv_metric_values' }));
  39. await rerender(
  40. <QueryEditor
  41. {...props}
  42. query={{ ...defaultQuery, scenarioId: 'csv_metric_values', stringInput: '1,20,90,30,5,0' }}
  43. />
  44. );
  45. expect(await screen.findByRole('textbox', { name: /string input/i })).toBeInTheDocument();
  46. expect(screen.getByRole('textbox', { name: /string input/i })).toHaveValue('1,20,90,30,5,0');
  47. await fireEvent.keyDown(select, { keyCode: 40 });
  48. await userEvent.click(screen.getByText('Grafana API'));
  49. expect(mockOnChange).toHaveBeenCalledWith(
  50. expect.objectContaining({ scenarioId: 'grafana_api', stringInput: 'datasources' })
  51. );
  52. rerender(
  53. <QueryEditor {...props} query={{ ...defaultQuery, scenarioId: 'grafana_api', stringInput: 'datasources' }} />
  54. );
  55. expect(await screen.findByText('Grafana API')).toBeInTheDocument();
  56. expect(screen.getByText('Data Sources')).toBeInTheDocument();
  57. await fireEvent.keyDown(select, { keyCode: 40 });
  58. await userEvent.click(screen.getByText('Streaming Client'));
  59. expect(mockOnChange).toHaveBeenCalledWith(
  60. expect.objectContaining({ scenarioId: 'streaming_client', stream: defaultStreamQuery })
  61. );
  62. const streamQuery = { ...defaultQuery, stream: defaultStreamQuery, scenarioId: 'streaming_client' };
  63. rerender(<QueryEditor {...props} query={streamQuery} />);
  64. expect(await screen.findByText('Streaming Client')).toBeInTheDocument();
  65. expect(screen.getByText('Type')).toBeInTheDocument();
  66. expect(screen.getByLabelText('Noise')).toHaveValue(2.2);
  67. expect(screen.getByLabelText('Speed (ms)')).toHaveValue(250);
  68. expect(screen.getByLabelText('Spread')).toHaveValue(3.5);
  69. expect(screen.getByLabelText('Bands')).toHaveValue(1);
  70. });
  71. it('persists the datasource from the query when switching scenario', async () => {
  72. const mockDatasource = {
  73. type: 'test',
  74. uid: 'foo',
  75. };
  76. setup({
  77. query: {
  78. ...defaultQuery,
  79. datasource: mockDatasource,
  80. },
  81. });
  82. let select = (await screen.findByText('Scenario')).nextSibling!.firstChild!;
  83. await fireEvent.keyDown(select, { keyCode: 40 });
  84. await userEvent.click(screen.getByText('Grafana API'));
  85. expect(mockOnChange).toHaveBeenCalledWith(expect.objectContaining({ datasource: mockDatasource }));
  86. });
  87. });