Dimensions.test.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { fireEvent, render, screen, within } from '@testing-library/react';
  2. import userEvent from '@testing-library/user-event';
  3. import React from 'react';
  4. import { Dimensions } from '..';
  5. import { setupMockedDataSource } from '../../__mocks__/CloudWatchDataSource';
  6. import { CloudWatchMetricsQuery } from '../../types';
  7. const ds = setupMockedDataSource({
  8. variables: [],
  9. });
  10. ds.datasource.getNamespaces = jest.fn().mockResolvedValue([]);
  11. ds.datasource.getMetrics = jest.fn().mockResolvedValue([]);
  12. ds.datasource.getDimensionKeys = jest.fn().mockResolvedValue([]);
  13. ds.datasource.getVariables = jest.fn().mockReturnValue([]);
  14. const q: CloudWatchMetricsQuery = {
  15. id: '',
  16. region: 'us-east-2',
  17. namespace: '',
  18. period: '',
  19. alias: '',
  20. metricName: '',
  21. dimensions: {},
  22. matchExact: true,
  23. statistic: '',
  24. expression: '',
  25. refId: '',
  26. };
  27. const props = {
  28. datasource: ds.datasource,
  29. query: q,
  30. disableExpressions: false,
  31. onChange: jest.fn(),
  32. onRunQuery: jest.fn(),
  33. };
  34. describe('Dimensions', () => {
  35. describe('when rendered with two existing dimensions', () => {
  36. it('should render two filter items', async () => {
  37. props.query.dimensions = {
  38. InstanceId: '*',
  39. InstanceGroup: 'Group1',
  40. };
  41. render(<Dimensions {...props} metricStat={props.query} dimensionKeys={[]} />);
  42. const filterItems = screen.getAllByTestId('cloudwatch-dimensions-filter-item');
  43. expect(filterItems.length).toBe(2);
  44. expect(within(filterItems[0]).getByText('InstanceId')).toBeInTheDocument();
  45. expect(within(filterItems[0]).getByText('*')).toBeInTheDocument();
  46. expect(within(filterItems[1]).getByText('InstanceGroup')).toBeInTheDocument();
  47. expect(within(filterItems[1]).getByText('Group1')).toBeInTheDocument();
  48. });
  49. });
  50. describe('when adding a new filter item', () => {
  51. it('it should add the new item but not call onChange', async () => {
  52. props.query.dimensions = {};
  53. const onChange = jest.fn();
  54. render(<Dimensions {...props} metricStat={props.query} onChange={onChange} dimensionKeys={[]} />);
  55. await userEvent.click(screen.getByLabelText('Add'));
  56. expect(screen.getByTestId('cloudwatch-dimensions-filter-item')).toBeInTheDocument();
  57. expect(onChange).not.toHaveBeenCalled();
  58. });
  59. });
  60. describe('when adding a new filter item with key', () => {
  61. it('it should add the new item but not call onChange', async () => {
  62. props.query.dimensions = {};
  63. const onChange = jest.fn();
  64. const { container } = render(
  65. <Dimensions {...props} metricStat={props.query} onChange={onChange} dimensionKeys={[]} />
  66. );
  67. await userEvent.click(screen.getByLabelText('Add'));
  68. const filterItemElement = screen.getByTestId('cloudwatch-dimensions-filter-item');
  69. expect(filterItemElement).toBeInTheDocument();
  70. const keyElement = container.querySelector('#cloudwatch-dimensions-filter-item-key');
  71. expect(keyElement).toBeInTheDocument();
  72. await userEvent.type(keyElement!, 'my-key');
  73. fireEvent.keyDown(keyElement!, { keyCode: 13 });
  74. expect(onChange).not.toHaveBeenCalled();
  75. });
  76. });
  77. describe('when adding a new filter item with key and value', () => {
  78. it('it should add the new item and trigger onChange', async () => {
  79. props.query.dimensions = {};
  80. const onChange = jest.fn();
  81. const { container } = render(
  82. <Dimensions {...props} metricStat={props.query} onChange={onChange} dimensionKeys={[]} />
  83. );
  84. const label = await screen.findByLabelText('Add');
  85. await userEvent.click(label);
  86. const filterItemElement = screen.getByTestId('cloudwatch-dimensions-filter-item');
  87. expect(filterItemElement).toBeInTheDocument();
  88. const keyElement = container.querySelector('#cloudwatch-dimensions-filter-item-key');
  89. expect(keyElement).toBeInTheDocument();
  90. await userEvent.type(keyElement!, 'my-key');
  91. fireEvent.keyDown(keyElement!, { keyCode: 13 });
  92. expect(onChange).not.toHaveBeenCalled();
  93. const valueElement = container.querySelector('#cloudwatch-dimensions-filter-item-value');
  94. expect(valueElement).toBeInTheDocument();
  95. await userEvent.type(valueElement!, 'my-value');
  96. fireEvent.keyDown(valueElement!, { keyCode: 13 });
  97. expect(onChange).not.toHaveBeenCalledWith({
  98. ...props.query,
  99. dimensions: {
  100. 'my-key': 'my-value',
  101. },
  102. });
  103. });
  104. });
  105. });