commandlinker.spec.ts 3.9 KB

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