braces.test.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import React from 'react';
  2. import Plain from 'slate-plain-serializer';
  3. import { Editor } from '@grafana/slate-react';
  4. import { shallow } from 'enzyme';
  5. import BracesPlugin from './braces';
  6. declare global {
  7. interface Window {
  8. KeyboardEvent: any;
  9. }
  10. }
  11. describe('braces', () => {
  12. const handler = BracesPlugin().onKeyDown;
  13. const nextMock = () => {};
  14. it('adds closing braces around empty value', () => {
  15. const value = Plain.deserialize('');
  16. const editor = shallow<Editor>(<Editor value={value} />);
  17. const event = new window.KeyboardEvent('keydown', { key: '(' });
  18. handler(event as Event, editor.instance() as any, nextMock);
  19. expect(Plain.serialize(editor.instance().value)).toEqual('()');
  20. });
  21. it('removes closing brace when opening brace is removed', () => {
  22. const value = Plain.deserialize('time()');
  23. const editor = shallow<Editor>(<Editor value={value} />);
  24. const event = new window.KeyboardEvent('keydown', { key: 'Backspace' });
  25. handler(event as Event, editor.instance().moveForward(5) as any, nextMock);
  26. expect(Plain.serialize(editor.instance().value)).toEqual('time');
  27. });
  28. it('keeps closing brace when opening brace is removed and inner values exist', () => {
  29. const value = Plain.deserialize('time(value)');
  30. const editor = shallow<Editor>(<Editor value={value} />);
  31. const event = new window.KeyboardEvent('keydown', { key: 'Backspace' });
  32. const handled = handler(event as Event, editor.instance().moveForward(5) as any, nextMock);
  33. expect(handled).toBeFalsy();
  34. });
  35. });