RawInfluxQLEditor.test.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { render, screen } from '@testing-library/react';
  2. import userEvent from '@testing-library/user-event';
  3. import React from 'react';
  4. import { selectOptionInTest } from 'test/helpers/selectOptionInTest';
  5. import { InfluxQuery } from '../types';
  6. import { RawInfluxQLEditor } from './RawInfluxQLEditor';
  7. const query: InfluxQuery = {
  8. refId: 'A',
  9. query: 'test query 1',
  10. resultFormat: 'table',
  11. alias: 'alias42',
  12. };
  13. describe('RawInfluxQLEditor', () => {
  14. it('should render', () => {
  15. render(<RawInfluxQLEditor onRunQuery={() => null} onChange={() => null} query={query} />);
  16. const queryTextarea = screen.getByLabelText('query');
  17. const aliasInput = screen.getByLabelText('Alias by');
  18. const formatSelect = screen.getByLabelText('Format as');
  19. expect(formatSelect).toBeInTheDocument();
  20. expect(queryTextarea).toBeInTheDocument();
  21. expect(aliasInput).toBeInTheDocument();
  22. expect(queryTextarea).toHaveValue('test query 1');
  23. expect(aliasInput).toHaveValue('alias42');
  24. // the only way to validate the text-displayed on the select-box
  25. expect(screen.getByText('Table')).toBeInTheDocument();
  26. });
  27. it('should handle no-alias, no-query, no-resultFormat', () => {
  28. const emptyQuery = { refId: 'B' };
  29. render(<RawInfluxQLEditor onRunQuery={() => null} onChange={() => null} query={emptyQuery} />);
  30. const queryTextarea = screen.getByLabelText('query');
  31. const aliasInput = screen.getByLabelText('Alias by');
  32. const formatSelect = screen.getByLabelText('Format as');
  33. expect(formatSelect).toBeInTheDocument();
  34. expect(queryTextarea).toBeInTheDocument();
  35. expect(aliasInput).toBeInTheDocument();
  36. expect(queryTextarea).toHaveValue('');
  37. expect(aliasInput).toHaveValue('');
  38. // the only way to validate the text-displayed on the select-box
  39. expect(screen.getByText('Time series')).toBeInTheDocument();
  40. });
  41. it('should call onChange immediately when resultFormat change', async () => {
  42. const onChange = jest.fn();
  43. render(<RawInfluxQLEditor onRunQuery={() => null} onChange={onChange} query={query} />);
  44. const formatSelect = screen.getByLabelText('Format as');
  45. expect(formatSelect).toBeInTheDocument();
  46. await selectOptionInTest(formatSelect, 'Time series');
  47. expect(onChange).toHaveBeenCalledWith({ ...query, resultFormat: 'time_series' });
  48. });
  49. it('should only call onChange on blur when query changes', async () => {
  50. const onChange = jest.fn();
  51. render(<RawInfluxQLEditor onRunQuery={() => null} onChange={onChange} query={query} />);
  52. const queryTextarea = screen.getByLabelText('query');
  53. expect(queryTextarea).toBeInTheDocument();
  54. const aliasInput = screen.getByLabelText('Alias by');
  55. expect(aliasInput).toBeInTheDocument();
  56. // value before
  57. expect(queryTextarea).toHaveValue('test query 1');
  58. await userEvent.type(queryTextarea, 'new changes');
  59. // the field should have a new value, but no onChange yet.
  60. expect(queryTextarea).toHaveValue('test query 1new changes');
  61. expect(onChange).toHaveBeenCalledTimes(0);
  62. aliasInput.focus(); // this should trigger blur on queryTextarea
  63. expect(onChange).toHaveBeenCalledTimes(1);
  64. expect(onChange).toHaveBeenCalledWith({ ...query, query: 'test query 1new changes' });
  65. });
  66. it('should only call onChange on blur when alias changes', async () => {
  67. const onChange = jest.fn();
  68. render(<RawInfluxQLEditor onRunQuery={() => null} onChange={onChange} query={query} />);
  69. const queryTextarea = screen.getByLabelText('query');
  70. expect(queryTextarea).toBeInTheDocument();
  71. const aliasInput = screen.getByLabelText('Alias by');
  72. expect(aliasInput).toBeInTheDocument();
  73. // value before
  74. expect(aliasInput).toHaveValue('alias42');
  75. await userEvent.type(aliasInput, 'new changes');
  76. // the field should have a new value, but no onChange yet.
  77. expect(aliasInput).toHaveValue('alias42new changes');
  78. expect(onChange).toHaveBeenCalledTimes(0);
  79. queryTextarea.focus(); // this should trigger blur on queryTextarea
  80. expect(onChange).toHaveBeenCalledTimes(1);
  81. expect(onChange).toHaveBeenCalledWith({ ...query, alias: 'alias42new changes' });
  82. });
  83. });