123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- // Copyright (c) Jupyter Development Team.
- import 'jest';
- import { CommandRegistry } from '@lumino/commands';
- import { h, VirtualNode, VirtualDOM } from '@lumino/virtualdom';
- import { simulate } from 'simulate-event';
- import { CommandLinker } from '@jupyterlab/apputils';
- describe('@jupyterlab/apputils', () => {
- describe('CommandLinker', () => {
- describe('#constructor()', () => {
- it('should create a command linker', () => {
- const linker = new CommandLinker({ commands: new CommandRegistry() });
- expect(linker).toBeInstanceOf(CommandLinker);
- linker.dispose();
- });
- });
- describe('#isDisposed', () => {
- it('should test whether a command linker has been disposed', () => {
- const linker = new CommandLinker({ commands: new CommandRegistry() });
- expect(linker.isDisposed).toBe(false);
- linker.dispose();
- expect(linker.isDisposed).toBe(true);
- });
- });
- describe('#connectNode()', () => {
- it('should connect a node to a command', () => {
- let called = false;
- const command = 'commandlinker:connect-node';
- const commands = new CommandRegistry();
- const linker = new CommandLinker({ commands });
- const node = document.createElement('div');
- const disposable = commands.addCommand(command, {
- execute: () => {
- called = true;
- }
- });
- document.body.appendChild(node);
- linker.connectNode(node, command, undefined);
- expect(called).toBe(false);
- simulate(node, 'click');
- expect(called).toBe(true);
- document.body.removeChild(node);
- linker.dispose();
- disposable.dispose();
- });
- });
- describe('#disconnectNode()', () => {
- it('should disconnect a node from a command', () => {
- let called = false;
- const command = 'commandlinker:disconnect-node';
- const commands = new CommandRegistry();
- const linker = new CommandLinker({ commands });
- const node = document.createElement('div');
- const disposable = commands.addCommand(command, {
- execute: () => {
- called = true;
- }
- });
- document.body.appendChild(node);
- linker.connectNode(node, command, undefined);
- // Make sure connection is working.
- expect(called).toBe(false);
- simulate(node, 'click');
- expect(called).toBe(true);
- // Reset flag.
- called = false;
- // Make sure disconnection is working.
- linker.disconnectNode(node);
- expect(called).toBe(false);
- simulate(node, 'click');
- expect(called).toBe(false);
- document.body.removeChild(node);
- linker.dispose();
- disposable.dispose();
- });
- });
- describe('#dispose()', () => {
- it('should dispose the resources held by the linker', () => {
- const linker = new CommandLinker({ commands: new CommandRegistry() });
- expect(linker.isDisposed).toBe(false);
- linker.dispose();
- expect(linker.isDisposed).toBe(true);
- });
- });
- describe('#populateVNodeDataset()', () => {
- it('should connect a node to a command', () => {
- let called = false;
- const command = 'commandlinker:connect-node';
- const commands = new CommandRegistry();
- const linker = new CommandLinker({ commands });
- let node: HTMLElement;
- let vnode: VirtualNode;
- const disposable = commands.addCommand(command, {
- execute: () => {
- called = true;
- }
- });
- vnode = h.div({
- dataset: linker.populateVNodeDataset(command, undefined)
- });
- node = VirtualDOM.realize(vnode);
- document.body.appendChild(node);
- expect(called).toBe(false);
- simulate(node, 'click');
- expect(called).toBe(true);
- document.body.removeChild(node);
- linker.dispose();
- disposable.dispose();
- });
- });
- });
- });
|