DerivedFields.test.tsx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { mount } from 'enzyme';
  2. import React from 'react';
  3. import { act } from 'react-dom/test-utils';
  4. import { Button } from '@grafana/ui';
  5. import { DerivedField } from './DerivedField';
  6. import { DerivedFields } from './DerivedFields';
  7. describe('DerivedFields', () => {
  8. let originalGetSelection: typeof window.getSelection;
  9. beforeAll(() => {
  10. originalGetSelection = window.getSelection;
  11. window.getSelection = () => null;
  12. });
  13. afterAll(() => {
  14. window.getSelection = originalGetSelection;
  15. });
  16. it('renders correctly when no fields', async () => {
  17. let wrapper: any;
  18. //@ts-ignore
  19. await act(async () => {
  20. wrapper = await mount(<DerivedFields onChange={() => {}} />);
  21. });
  22. expect(wrapper.find(Button).length).toBe(1);
  23. expect(wrapper.find(Button).contains('Add')).toBeTruthy();
  24. expect(wrapper.find(DerivedField).length).toBe(0);
  25. });
  26. it('renders correctly when there are fields', async () => {
  27. let wrapper: any;
  28. //@ts-ignore
  29. await act(async () => {
  30. wrapper = await mount(<DerivedFields value={testValue} onChange={() => {}} />);
  31. });
  32. expect(wrapper.find(Button).filterWhere((button: any) => button.contains('Add')).length).toBe(1);
  33. expect(wrapper.find(Button).filterWhere((button: any) => button.contains('Show example log message')).length).toBe(
  34. 1
  35. );
  36. expect(
  37. wrapper
  38. .find(Button)
  39. .filterWhere((button: any) => button.contains('Show example log message'))
  40. .getDOMNode()
  41. ).toHaveAttribute('type', 'button');
  42. expect(wrapper.find(DerivedField).length).toBe(2);
  43. });
  44. it('adds new field', async () => {
  45. const onChangeMock = jest.fn();
  46. let wrapper: any;
  47. //@ts-ignore
  48. await act(async () => {
  49. wrapper = await mount(<DerivedFields onChange={onChangeMock} />);
  50. });
  51. const addButton = wrapper.find(Button).filterWhere((button: any) => button.contains('Add'));
  52. addButton.simulate('click');
  53. expect(onChangeMock.mock.calls[0][0].length).toBe(1);
  54. });
  55. it('removes field', async () => {
  56. const onChangeMock = jest.fn();
  57. let wrapper: any;
  58. //@ts-ignore
  59. await act(async () => {
  60. wrapper = await mount(<DerivedFields value={testValue} onChange={onChangeMock} />);
  61. });
  62. const removeButton = wrapper.find(DerivedField).at(0).find(Button);
  63. removeButton.simulate('click');
  64. const newValue = onChangeMock.mock.calls[0][0];
  65. expect(newValue.length).toBe(1);
  66. expect(newValue[0]).toMatchObject({
  67. matcherRegex: 'regex2',
  68. name: 'test2',
  69. url: 'localhost2',
  70. });
  71. });
  72. });
  73. const testValue = [
  74. {
  75. matcherRegex: 'regex1',
  76. name: 'test1',
  77. url: 'localhost1',
  78. },
  79. {
  80. matcherRegex: 'regex2',
  81. name: 'test2',
  82. url: 'localhost2',
  83. },
  84. ];