clear.test.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import Plain from 'slate-plain-serializer';
  2. import React from 'react';
  3. import { Editor } from '@grafana/slate-react';
  4. import { shallow } from 'enzyme';
  5. import ClearPlugin from './clear';
  6. describe('clear', () => {
  7. const handler = ClearPlugin().onKeyDown;
  8. it('does not change the empty value', () => {
  9. const value = Plain.deserialize('');
  10. const editor = shallow<Editor>(<Editor value={value} />);
  11. const event = new window.KeyboardEvent('keydown', {
  12. key: 'k',
  13. ctrlKey: true,
  14. });
  15. handler(event as Event, editor.instance() as any, () => {});
  16. expect(Plain.serialize(editor.instance().value)).toEqual('');
  17. });
  18. it('clears to the end of the line', () => {
  19. const value = Plain.deserialize('foo');
  20. const editor = shallow<Editor>(<Editor value={value} />);
  21. const event = new window.KeyboardEvent('keydown', {
  22. key: 'k',
  23. ctrlKey: true,
  24. });
  25. handler(event as Event, editor.instance() as any, () => {});
  26. expect(Plain.serialize(editor.instance().value)).toEqual('');
  27. });
  28. it('clears from the middle to the end of the line', () => {
  29. const value = Plain.deserialize('foo bar');
  30. const editor = shallow<Editor>(<Editor value={value} />);
  31. const event = new window.KeyboardEvent('keydown', {
  32. key: 'k',
  33. ctrlKey: true,
  34. });
  35. handler(event as Event, editor.instance().moveForward(4) as any, () => {});
  36. expect(Plain.serialize(editor.instance().value)).toEqual('foo ');
  37. });
  38. });