DataLinks.test.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 { DataLink } from './DataLink';
  6. import { DataLinks } from './DataLinks';
  7. describe('DataLinks', () => {
  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. await act(
  19. // @ts-ignore we shouldn't use Promises in act => the "void | undefined" is here to forbid any sneaky "Promise" returns.
  20. async () => {
  21. wrapper = await mount(<DataLinks onChange={() => {}} />);
  22. }
  23. );
  24. expect(wrapper.find(Button).length).toBe(1);
  25. expect(wrapper.find(Button).contains('Add')).toBeTruthy();
  26. expect(wrapper.find(DataLink).length).toBe(0);
  27. });
  28. it('renders correctly when there are fields', async () => {
  29. let wrapper: any;
  30. await act(
  31. // @ts-ignore we shouldn't use Promises in act => the "void | undefined" is here to forbid any sneaky "Promise" returns.
  32. async () => {
  33. wrapper = await mount(<DataLinks value={testValue} onChange={() => {}} />);
  34. }
  35. );
  36. expect(wrapper.find(Button).filterWhere((button: any) => button.contains('Add')).length).toBe(1);
  37. expect(wrapper.find(DataLink).length).toBe(2);
  38. });
  39. it('adds new field', async () => {
  40. const onChangeMock = jest.fn();
  41. let wrapper: any;
  42. await act(
  43. // @ts-ignore we shouldn't use Promises in act => the "void | undefined" is here to forbid any sneaky "Promise" returns.
  44. async () => {
  45. wrapper = await mount(<DataLinks onChange={onChangeMock} />);
  46. }
  47. );
  48. const addButton = wrapper.find(Button).filterWhere((button: any) => button.contains('Add'));
  49. addButton.simulate('click');
  50. expect(onChangeMock.mock.calls[0][0].length).toBe(1);
  51. });
  52. it('removes field', async () => {
  53. const onChangeMock = jest.fn();
  54. let wrapper: any;
  55. await act(
  56. // @ts-ignore we shouldn't use Promises in act => the "void | undefined" is here to forbid any sneaky "Promise" returns.
  57. async () => {
  58. wrapper = await mount(<DataLinks value={testValue} onChange={onChangeMock} />);
  59. }
  60. );
  61. const removeButton = wrapper.find(DataLink).at(0).find(Button);
  62. removeButton.simulate('click');
  63. const newValue = onChangeMock.mock.calls[0][0];
  64. expect(newValue.length).toBe(1);
  65. expect(newValue[0]).toMatchObject({
  66. field: 'regex2',
  67. url: 'localhost2',
  68. });
  69. });
  70. });
  71. const testValue = [
  72. {
  73. field: 'regex1',
  74. url: 'localhost1',
  75. },
  76. {
  77. field: 'regex2',
  78. url: 'localhost2',
  79. },
  80. ];