commandlinker.spec.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import 'jest';
  4. import { CommandRegistry } from '@lumino/commands';
  5. import { h, VirtualNode, VirtualDOM } from '@lumino/virtualdom';
  6. import { simulate } from 'simulate-event';
  7. import { CommandLinker } from '@jupyterlab/apputils';
  8. describe('@jupyterlab/apputils', () => {
  9. describe('CommandLinker', () => {
  10. describe('#constructor()', () => {
  11. it('should create a command linker', () => {
  12. const linker = new CommandLinker({ commands: new CommandRegistry() });
  13. expect(linker).toBeInstanceOf(CommandLinker);
  14. linker.dispose();
  15. });
  16. });
  17. describe('#isDisposed', () => {
  18. it('should test whether a command linker has been disposed', () => {
  19. const linker = new CommandLinker({ commands: new CommandRegistry() });
  20. expect(linker.isDisposed).toBe(false);
  21. linker.dispose();
  22. expect(linker.isDisposed).toBe(true);
  23. });
  24. });
  25. describe('#connectNode()', () => {
  26. it('should connect a node to a command', () => {
  27. let called = false;
  28. const command = 'commandlinker:connect-node';
  29. const commands = new CommandRegistry();
  30. const linker = new CommandLinker({ commands });
  31. const node = document.createElement('div');
  32. const disposable = commands.addCommand(command, {
  33. execute: () => {
  34. called = true;
  35. }
  36. });
  37. document.body.appendChild(node);
  38. linker.connectNode(node, command, undefined);
  39. expect(called).toBe(false);
  40. simulate(node, 'click');
  41. expect(called).toBe(true);
  42. document.body.removeChild(node);
  43. linker.dispose();
  44. disposable.dispose();
  45. });
  46. });
  47. describe('#disconnectNode()', () => {
  48. it('should disconnect a node from a command', () => {
  49. let called = false;
  50. const command = 'commandlinker:disconnect-node';
  51. const commands = new CommandRegistry();
  52. const linker = new CommandLinker({ commands });
  53. const node = document.createElement('div');
  54. const disposable = commands.addCommand(command, {
  55. execute: () => {
  56. called = true;
  57. }
  58. });
  59. document.body.appendChild(node);
  60. linker.connectNode(node, command, undefined);
  61. // Make sure connection is working.
  62. expect(called).toBe(false);
  63. simulate(node, 'click');
  64. expect(called).toBe(true);
  65. // Reset flag.
  66. called = false;
  67. // Make sure disconnection is working.
  68. linker.disconnectNode(node);
  69. expect(called).toBe(false);
  70. simulate(node, 'click');
  71. expect(called).toBe(false);
  72. document.body.removeChild(node);
  73. linker.dispose();
  74. disposable.dispose();
  75. });
  76. });
  77. describe('#dispose()', () => {
  78. it('should dispose the resources held by the linker', () => {
  79. const linker = new CommandLinker({ commands: new CommandRegistry() });
  80. expect(linker.isDisposed).toBe(false);
  81. linker.dispose();
  82. expect(linker.isDisposed).toBe(true);
  83. });
  84. });
  85. describe('#populateVNodeDataset()', () => {
  86. it('should connect a node to a command', () => {
  87. let called = false;
  88. const command = 'commandlinker:connect-node';
  89. const commands = new CommandRegistry();
  90. const linker = new CommandLinker({ commands });
  91. let node: HTMLElement;
  92. let vnode: VirtualNode;
  93. const disposable = commands.addCommand(command, {
  94. execute: () => {
  95. called = true;
  96. }
  97. });
  98. vnode = h.div({
  99. dataset: linker.populateVNodeDataset(command, undefined)
  100. });
  101. node = VirtualDOM.realize(vnode);
  102. document.body.appendChild(node);
  103. expect(called).toBe(false);
  104. simulate(node, 'click');
  105. expect(called).toBe(true);
  106. document.body.removeChild(node);
  107. linker.dispose();
  108. disposable.dispose();
  109. });
  110. });
  111. });
  112. });