commandpalette.spec.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. // import { expect } from 'chai';
  4. import { CommandRegistry } from '@lumino/commands';
  5. import { JSONObject } from '@lumino/coreutils';
  6. import { MessageLoop } from '@lumino/messaging';
  7. import { Widget } from '@lumino/widgets';
  8. import { CommandPalette } from '@lumino/widgets';
  9. import { CommandPaletteSvg, paletteIcon } from '@jupyterlab/ui-components';
  10. import { ModalCommandPalette } from '@jupyterlab/apputils';
  11. import { nullTranslator, ITranslator } from '@jupyterlab/translation';
  12. import { simulate } from 'simulate-event';
  13. describe('@jupyterlab/apputils', () => {
  14. describe('ModalCommandPalette', () => {
  15. let commands: CommandRegistry;
  16. let translator: ITranslator;
  17. let palette: CommandPalette;
  18. let modalPalette: ModalCommandPalette;
  19. beforeEach(() => {
  20. commands = new CommandRegistry();
  21. translator = nullTranslator;
  22. palette = new CommandPalette({
  23. commands: commands,
  24. renderer: CommandPaletteSvg.defaultRenderer
  25. });
  26. palette.id = 'command-palette';
  27. palette.title.icon = paletteIcon;
  28. const trans = translator.load('jupyterlab');
  29. palette.title.label = trans.__('Commands');
  30. modalPalette = new ModalCommandPalette({ commandPalette: palette });
  31. modalPalette.attach();
  32. });
  33. describe('#constructor()', () => {
  34. it('should create a new command palette', () => {
  35. expect(palette).toBeInstanceOf(CommandPalette);
  36. });
  37. it('should create a new modal command palette', () => {
  38. expect(modalPalette).toBeInstanceOf(ModalCommandPalette);
  39. });
  40. it('should attach to the document body', () => {
  41. expect(document.body.contains(modalPalette.node)).toBe(true);
  42. });
  43. it('should start hidden', () => {
  44. expect(modalPalette.isHidden).toBe(true);
  45. });
  46. });
  47. describe('#activate()', () => {
  48. it('should become visible when activated', () => {
  49. MessageLoop.sendMessage(modalPalette, Widget.Msg.ActivateRequest);
  50. expect(modalPalette.isVisible).toBe(true);
  51. });
  52. });
  53. describe('#hideAndReset()', () => {
  54. it('should become hidden and clear the input when calling hideAndReset', () => {
  55. MessageLoop.sendMessage(modalPalette, Widget.Msg.ActivateRequest);
  56. palette.inputNode.value = 'Search string...';
  57. modalPalette.hideAndReset();
  58. expect(modalPalette.isVisible).toBe(false);
  59. expect(palette.inputNode.value).toEqual('');
  60. });
  61. });
  62. describe('#blur()', () => {
  63. it('should hide and reset when blurred', () => {
  64. MessageLoop.sendMessage(modalPalette, Widget.Msg.ActivateRequest);
  65. palette.inputNode.value = 'Search string...';
  66. simulate(modalPalette.node, 'blur');
  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. });