QueryField.test.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import React from 'react';
  2. import { shallow } from 'enzyme';
  3. import { QueryField } from './QueryField';
  4. describe('<QueryField />', () => {
  5. it('should render with null initial value', () => {
  6. const wrapper = shallow(<QueryField initialQuery={null} />);
  7. expect(wrapper.find('div').exists()).toBeTruthy();
  8. });
  9. it('should render with empty initial value', () => {
  10. const wrapper = shallow(<QueryField initialQuery="" />);
  11. expect(wrapper.find('div').exists()).toBeTruthy();
  12. });
  13. it('should render with initial value', () => {
  14. const wrapper = shallow(<QueryField initialQuery="my query" />);
  15. expect(wrapper.find('div').exists()).toBeTruthy();
  16. });
  17. it('should execute query when enter is pressed and there are no suggestions visible', () => {
  18. const wrapper = shallow(<QueryField initialQuery="my query" />);
  19. const instance = wrapper.instance() as QueryField;
  20. instance.executeOnChangeAndRunQueries = jest.fn();
  21. const handleEnterAndTabKeySpy = jest.spyOn(instance, 'handleEnterKey');
  22. instance.onKeyDown({ key: 'Enter', preventDefault: () => {} } as KeyboardEvent, {});
  23. expect(handleEnterAndTabKeySpy).toBeCalled();
  24. expect(instance.executeOnChangeAndRunQueries).toBeCalled();
  25. });
  26. it('should copy selected text', () => {
  27. const wrapper = shallow(<QueryField initialQuery="" />);
  28. const instance = wrapper.instance() as QueryField;
  29. const textBlocks = ['ignore this text. copy this text'];
  30. const copiedText = instance.getCopiedText(textBlocks, 18, 32);
  31. expect(copiedText).toBe('copy this text');
  32. });
  33. it('should copy selected text across 2 lines', () => {
  34. const wrapper = shallow(<QueryField initialQuery="" />);
  35. const instance = wrapper.instance() as QueryField;
  36. const textBlocks = ['ignore this text. start copying here', 'lorem ipsum. stop copying here. lorem ipsum'];
  37. const copiedText = instance.getCopiedText(textBlocks, 18, 30);
  38. expect(copiedText).toBe('start copying here\nlorem ipsum. stop copying here');
  39. });
  40. it('should copy selected text across > 2 lines', () => {
  41. const wrapper = shallow(<QueryField initialQuery="" />);
  42. const instance = wrapper.instance() as QueryField;
  43. const textBlocks = [
  44. 'ignore this text. start copying here',
  45. 'lorem ipsum doler sit amet',
  46. 'lorem ipsum. stop copying here. lorem ipsum',
  47. ];
  48. const copiedText = instance.getCopiedText(textBlocks, 18, 30);
  49. expect(copiedText).toBe('start copying here\nlorem ipsum doler sit amet\nlorem ipsum. stop copying here');
  50. });
  51. });