commandpalette.spec.ts 3.8 KB

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