| 1 |
/* eslint-disable @typescript-eslint/no-empty-function */ |
| 2 |
// Copyright (c) Jupyter Development Team. |
| 3 |
// Distributed under the terms of the Modified BSD License. |
| 4 |
/*----------------------------------------------------------------------------- |
| 5 |
| Copyright (c) 2014-2017, PhosphorJS Contributors |
| 6 |
| |
| 7 |
| Distributed under the terms of the BSD 3-Clause License. |
| 8 |
| |
| 9 |
| The full license is in the file LICENSE, distributed with this software. |
| 10 |
|----------------------------------------------------------------------------*/ |
| 11 |
import { expect } from 'chai'; |
| 12 |
|
| 13 |
import { CommandRegistry } from '@lumino/commands'; |
| 14 |
|
| 15 |
import { JSONObject } from '@lumino/coreutils'; |
| 16 |
|
| 17 |
import { ContextMenu } from '@lumino/widgets'; |
| 18 |
|
| 19 |
describe('@lumino/widgets', () => { |
| 20 |
let commands = new CommandRegistry(); |
| 21 |
|
| 22 |
before(() => { |
| 23 |
commands.addCommand('test-1', { |
| 24 |
execute: (args: JSONObject) => {}, |
| 25 |
label: 'Test 1 Label' |
| 26 |
}); |
| 27 |
commands.addCommand('test-2', { |
| 28 |
execute: (args: JSONObject) => {}, |
| 29 |
label: 'Test 2 Label' |
| 30 |
}); |
| 31 |
commands.addCommand('test-3', { |
| 32 |
execute: (args: JSONObject) => {}, |
| 33 |
label: 'Test 3 Label' |
| 34 |
}); |
| 35 |
commands.addCommand('test-4', { |
| 36 |
execute: (args: JSONObject) => {}, |
| 37 |
label: 'Test 4 Label' |
| 38 |
}); |
| 39 |
}); |
| 40 |
|
| 41 |
describe('ContextMenu', () => { |
| 42 |
describe('#open', () => { |
| 43 |
let menu: ContextMenu; |
| 44 |
const CLASSNAME = 'menu-1'; |
| 45 |
|
| 46 |
function addItems(menu: ContextMenu) { |
| 47 |
menu.addItem({ |
| 48 |
command: 'test-1', |
| 49 |
selector: `.${CLASSNAME}`, |
| 50 |
rank: 20 |
| 51 |
}); |
| 52 |
menu.addItem({ |
| 53 |
command: 'test-2', |
| 54 |
selector: `.${CLASSNAME}`, |
| 55 |
rank: 10 |
| 56 |
}); |
| 57 |
menu.addItem({ |
| 58 |
command: 'test-3', |
| 59 |
selector: `div.${CLASSNAME}`, |
| 60 |
rank: 30 |
| 61 |
}); |
| 62 |
menu.addItem({ |
| 63 |
command: 'test-4', |
| 64 |
selector: '.menu-2', |
| 65 |
rank: 1 |
| 66 |
}); |
| 67 |
menu.addItem({ |
| 68 |
command: 'test-5', |
| 69 |
selector: 'body', |
| 70 |
rank: 15 |
| 71 |
}); |
| 72 |
} |
| 73 |
|
| 74 |
afterEach(() => { |
| 75 |
menu && menu.menu.dispose(); |
| 76 |
}); |
| 77 |
|
| 78 |
it('should show items matching selector, grouped and ordered by selector and rank', () => { |
| 79 |
const target = document.createElement('div'); |
| 80 |
target.className = CLASSNAME; |
| 81 |
document.body.appendChild(target); |
| 82 |
|
| 83 |
menu = new ContextMenu({ commands }); |
| 84 |
addItems(menu); |
| 85 |
|
| 86 |
const bb = target.getBoundingClientRect() as DOMRect; |
| 87 |
|
| 88 |
menu.open({ |
| 89 |
target, |
| 90 |
currentTarget: document.body, |
| 91 |
clientX: bb.x, |
| 92 |
clientY: bb.y |
| 93 |
} as any); |
| 94 |
|
| 95 |
expect(menu.menu.items).to.have.length(4); |
| 96 |
expect(menu.menu.items[0].command).to.equal('test-3'); |
| 97 |
expect(menu.menu.items[1].command).to.equal('test-2'); |
| 98 |
expect(menu.menu.items[2].command).to.equal('test-1'); |
| 99 |
expect(menu.menu.items[3].command).to.equal('test-5'); |
| 100 |
}); |
| 101 |
|
| 102 |
it('should show items matching selector, grouped and ordered only by rank', () => { |
| 103 |
const target = document.createElement('div'); |
| 104 |
target.className = CLASSNAME; |
| 105 |
document.body.appendChild(target); |
| 106 |
|
| 107 |
menu = new ContextMenu({ commands, sortBySelector: false }); |
| 108 |
addItems(menu); |
| 109 |
|
| 110 |
const bb = target.getBoundingClientRect() as DOMRect; |
| 111 |
|
| 112 |
menu.open({ |
| 113 |
target, |
| 114 |
currentTarget: document.body, |
| 115 |
clientX: bb.x, |
| 116 |
clientY: bb.y |
| 117 |
} as any); |
| 118 |
|
| 119 |
expect(menu.menu.items).to.have.length(4); |
| 120 |
expect(menu.menu.items[0].command).to.equal('test-2'); |
| 121 |
expect(menu.menu.items[1].command).to.equal('test-1'); |
| 122 |
expect(menu.menu.items[2].command).to.equal('test-3'); |
| 123 |
expect(menu.menu.items[3].command).to.equal('test-5'); |
| 124 |
}); |
| 125 |
|
| 126 |
it('should show items matching selector, ungrouped and ordered by selector and rank', () => { |
| 127 |
const target = document.createElement('div'); |
| 128 |
target.className = CLASSNAME; |
| 129 |
document.body.appendChild(target); |
| 130 |
|
| 131 |
menu = new ContextMenu({ |
| 132 |
commands, |
| 133 |
groupByTarget: false, |
| 134 |
sortBySelector: false |
| 135 |
}); |
| 136 |
addItems(menu); |
| 137 |
|
| 138 |
const bb = target.getBoundingClientRect() as DOMRect; |
| 139 |
|
| 140 |
menu.open({ |
| 141 |
target, |
| 142 |
currentTarget: document.body, |
| 143 |
clientX: bb.x, |
| 144 |
clientY: bb.y |
| 145 |
} as any); |
| 146 |
|
| 147 |
expect(menu.menu.items).to.have.length(4); |
| 148 |
expect(menu.menu.items[1].command).to.equal('test-5'); |
| 149 |
expect(menu.menu.items[0].command).to.equal('test-2'); |
| 150 |
expect(menu.menu.items[2].command).to.equal('test-1'); |
| 151 |
expect(menu.menu.items[3].command).to.equal('test-3'); |
| 152 |
}); |
| 153 |
|
| 154 |
it('should show items matching selector, ungrouped and ordered only by rank', () => { |
| 155 |
const target = document.createElement('div'); |
| 156 |
target.className = CLASSNAME; |
| 157 |
document.body.appendChild(target); |
| 158 |
|
| 159 |
menu = new ContextMenu({ |
| 160 |
commands, |
| 161 |
groupByTarget: false, |
| 162 |
sortBySelector: false |
| 163 |
}); |
| 164 |
addItems(menu); |
| 165 |
|
| 166 |
const bb = target.getBoundingClientRect() as DOMRect; |
| 167 |
|
| 168 |
menu.open({ |
| 169 |
target, |
| 170 |
currentTarget: document.body, |
| 171 |
clientX: bb.x, |
| 172 |
clientY: bb.y |
| 173 |
} as any); |
| 174 |
|
| 175 |
expect(menu.menu.items).to.have.length(4); |
| 176 |
expect(menu.menu.items[0].command).to.equal('test-2'); |
| 177 |
expect(menu.menu.items[1].command).to.equal('test-5'); |
| 178 |
expect(menu.menu.items[2].command).to.equal('test-1'); |
| 179 |
expect(menu.menu.items[3].command).to.equal('test-3'); |
| 180 |
}); |
| 181 |
}); |
| 182 |
}); |
| 183 |
}); |