commandpalette.spec.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { ModalCommandPalette } from '@jupyterlab/apputils';
  4. import { ITranslator, nullTranslator } from '@jupyterlab/translation';
  5. import { CommandPaletteSvg, paletteIcon } from '@jupyterlab/ui-components';
  6. import { CommandRegistry } from '@lumino/commands';
  7. import { JSONObject } from '@lumino/coreutils';
  8. import { MessageLoop } from '@lumino/messaging';
  9. import { CommandPalette, Widget } from '@lumino/widgets';
  10. import { simulate } from 'simulate-event';
  11. describe('@jupyterlab/apputils', () => {
  12. describe('ModalCommandPalette', () => {
  13. let commands: CommandRegistry;
  14. let translator: ITranslator;
  15. let palette: CommandPalette;
  16. let modalPalette: ModalCommandPalette;
  17. beforeEach(() => {
  18. commands = new CommandRegistry();
  19. translator = nullTranslator;
  20. palette = new CommandPalette({
  21. commands: commands,
  22. renderer: CommandPaletteSvg.defaultRenderer
  23. });
  24. palette.id = 'command-palette';
  25. palette.title.icon = paletteIcon;
  26. const trans = translator.load('jupyterlab');
  27. palette.title.label = trans.__('Commands');
  28. modalPalette = new ModalCommandPalette({ commandPalette: palette });
  29. modalPalette.attach();
  30. });
  31. describe('#constructor()', () => {
  32. it('should create a new command palette', () => {
  33. expect(palette).toBeInstanceOf(CommandPalette);
  34. });
  35. it('should create a new modal command palette', () => {
  36. expect(modalPalette).toBeInstanceOf(ModalCommandPalette);
  37. });
  38. it('should attach to the document body', () => {
  39. expect(document.body.contains(modalPalette.node)).toBe(true);
  40. });
  41. it('should start hidden', () => {
  42. expect(modalPalette.isHidden).toBe(true);
  43. });
  44. });
  45. describe('#activate()', () => {
  46. it('should become visible when activated', () => {
  47. MessageLoop.sendMessage(modalPalette, Widget.Msg.ActivateRequest);
  48. expect(modalPalette.isVisible).toBe(true);
  49. });
  50. });
  51. describe('#hideAndReset()', () => {
  52. it('should become hidden and clear the input when calling hideAndReset', () => {
  53. MessageLoop.sendMessage(modalPalette, Widget.Msg.ActivateRequest);
  54. palette.inputNode.value = 'Search string…';
  55. modalPalette.hideAndReset();
  56. expect(modalPalette.isVisible).toBe(false);
  57. expect(palette.inputNode.value).toEqual('');
  58. });
  59. });
  60. describe('#blur()', () => {
  61. it('should hide and reset when focus is shifted', () => {
  62. MessageLoop.sendMessage(modalPalette, Widget.Msg.ActivateRequest);
  63. palette.inputNode.value = 'Search string…';
  64. simulate(modalPalette.node, 'blur', {
  65. relatedTarget: document.body
  66. });
  67. expect(modalPalette.isVisible).toBe(false);
  68. expect(palette.inputNode.value).toEqual('');
  69. });
  70. });
  71. describe('#escape()', () => {
  72. it('should hide and reset when ESC is pressed', () => {
  73. MessageLoop.sendMessage(modalPalette, Widget.Msg.ActivateRequest);
  74. palette.inputNode.value = 'Search string…';
  75. simulate(modalPalette.node, 'keydown', { keyCode: 27 });
  76. expect(modalPalette.isVisible).toBe(false);
  77. expect(palette.inputNode.value).toEqual('');
  78. });
  79. });
  80. describe('#execute()', () => {
  81. it('should hide and reset when a command is executed', () => {
  82. commands.addCommand('mock-command', {
  83. execute: (args: JSONObject) => {
  84. return args;
  85. }
  86. });
  87. MessageLoop.sendMessage(modalPalette, Widget.Msg.ActivateRequest);
  88. void commands.execute('mock-command');
  89. expect(modalPalette.isVisible).toBe(false);
  90. expect(palette.inputNode.value).toEqual('');
  91. });
  92. });
  93. });
  94. });