FieldToConfigMappingEditor.test.tsx 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { fireEvent, render, screen, getByText, getByLabelText } 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 { toDataFrame, FieldType } from '@grafana/data';
  6. import { Props, FieldToConfigMappingEditor } from './FieldToConfigMappingEditor';
  7. beforeEach(() => {
  8. jest.clearAllMocks();
  9. });
  10. const frame = toDataFrame({
  11. fields: [
  12. { name: 'Unit', type: FieldType.string, values: ['degree', 'pressurebar'] },
  13. { name: 'Miiin', type: FieldType.number, values: [3, 100] },
  14. { name: 'max', type: FieldType.string, values: [15, 200] },
  15. ],
  16. });
  17. const mockOnChange = jest.fn();
  18. const props: Props = {
  19. frame: frame,
  20. onChange: mockOnChange,
  21. mappings: [],
  22. withReducers: true,
  23. };
  24. const setup = (testProps?: Partial<Props>) => {
  25. const editorProps = { ...props, ...testProps };
  26. return render(<FieldToConfigMappingEditor {...editorProps} />);
  27. };
  28. describe('FieldToConfigMappingEditor', () => {
  29. it('Should render fields', async () => {
  30. setup();
  31. expect(await screen.findByText('Unit')).toBeInTheDocument();
  32. expect(await screen.findByText('Miiin')).toBeInTheDocument();
  33. expect(await screen.findByText('max')).toBeInTheDocument();
  34. expect(await screen.findByText('Max (auto)')).toBeInTheDocument();
  35. });
  36. it('Can change mapping', async () => {
  37. setup();
  38. const select = (await screen.findByTestId('Miiin-config-key')).childNodes[0];
  39. await fireEvent.keyDown(select, { keyCode: 40 });
  40. await selectOptionInTest(select as HTMLElement, 'Min');
  41. expect(mockOnChange).toHaveBeenCalledWith(expect.arrayContaining([{ fieldName: 'Miiin', handlerKey: 'min' }]));
  42. });
  43. it('Can remove added mapping', async () => {
  44. setup({ mappings: [{ fieldName: 'max', handlerKey: 'min' }] });
  45. const select = (await screen.findByTestId('max-config-key')).childNodes[0];
  46. await userEvent.click(getByLabelText(select as HTMLElement, 'select-clear-value'));
  47. expect(mockOnChange).toHaveBeenCalledWith(expect.arrayContaining([]));
  48. });
  49. it('Automatic mapping is shown as placeholder', async () => {
  50. setup({ mappings: [] });
  51. const select = await screen.findByText('Max (auto)');
  52. expect(select).toBeInTheDocument();
  53. });
  54. it('Should show correct default reducer', async () => {
  55. setup({ mappings: [{ fieldName: 'max', handlerKey: 'mappings.value' }] });
  56. const reducer = await screen.findByTestId('max-reducer');
  57. expect(getByText(reducer, 'All values')).toBeInTheDocument();
  58. });
  59. it('Can change reducer', async () => {
  60. setup();
  61. const reducer = await (await screen.findByTestId('max-reducer')).childNodes[0];
  62. await fireEvent.keyDown(reducer, { keyCode: 40 });
  63. await selectOptionInTest(reducer as HTMLElement, 'Last');
  64. expect(mockOnChange).toHaveBeenCalledWith(
  65. expect.arrayContaining([{ fieldName: 'max', handlerKey: 'max', reducerId: 'last' }])
  66. );
  67. });
  68. });