| 1 |
// Copyright (c) Jupyter Development Team. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
/*----------------------------------------------------------------------------- |
| 4 |
| Copyright (c) 2014-2017, PhosphorJS Contributors |
| 5 |
| |
| 6 |
| Distributed under the terms of the BSD 3-Clause License. |
| 7 |
| |
| 8 |
| The full license is in the file LICENSE, distributed with this software. |
| 9 |
|----------------------------------------------------------------------------*/ |
| 10 |
import { expect } from 'chai'; |
| 11 |
|
| 12 |
import { StackedPanel, TabBar, TabPanel, Widget } from '@lumino/widgets'; |
| 13 |
|
| 14 |
import { LogWidget } from './widget.spec'; |
| 15 |
|
| 16 |
describe('@lumino/widgets', () => { |
| 17 |
describe('TabPanel', () => { |
| 18 |
describe('#constructor()', () => { |
| 19 |
it('should construct a new tab panel and take no arguments', () => { |
| 20 |
let panel = new TabPanel(); |
| 21 |
expect(panel).to.be.an.instanceof(TabPanel); |
| 22 |
}); |
| 23 |
|
| 24 |
it('should accept options', () => { |
| 25 |
let renderer = Object.create(TabBar.defaultRenderer); |
| 26 |
let panel = new TabPanel({ |
| 27 |
tabPlacement: 'left', |
| 28 |
tabsMovable: true, |
| 29 |
renderer |
| 30 |
}); |
| 31 |
expect(panel.tabBar.tabsMovable).to.equal(true); |
| 32 |
expect(panel.tabBar.renderer).to.equal(renderer); |
| 33 |
}); |
| 34 |
|
| 35 |
it('should add a `lm-TabPanel` class', () => { |
| 36 |
let panel = new TabPanel(); |
| 37 |
expect(panel.hasClass('lm-TabPanel')).to.equal(true); |
| 38 |
}); |
| 39 |
}); |
| 40 |
|
| 41 |
describe('#dispose()', () => { |
| 42 |
it('should dispose of the resources held by the widget', () => { |
| 43 |
let panel = new TabPanel(); |
| 44 |
panel.addWidget(new Widget()); |
| 45 |
panel.dispose(); |
| 46 |
expect(panel.isDisposed).to.equal(true); |
| 47 |
panel.dispose(); |
| 48 |
expect(panel.isDisposed).to.equal(true); |
| 49 |
}); |
| 50 |
}); |
| 51 |
|
| 52 |
describe('#currentChanged', () => { |
| 53 |
it('should be emitted when the current tab is changed', () => { |
| 54 |
let panel = new TabPanel(); |
| 55 |
panel.addWidget(new Widget()); |
| 56 |
panel.addWidget(new Widget()); |
| 57 |
let called = false; |
| 58 |
let widgets = panel.widgets; |
| 59 |
panel.currentChanged.connect((sender, args) => { |
| 60 |
expect(sender).to.equal(panel); |
| 61 |
expect(args.previousIndex).to.equal(0); |
| 62 |
expect(args.previousWidget).to.equal(widgets[0]); |
| 63 |
expect(args.currentIndex).to.equal(1); |
| 64 |
expect(args.currentWidget).to.equal(widgets[1]); |
| 65 |
called = true; |
| 66 |
}); |
| 67 |
panel.currentIndex = 1; |
| 68 |
expect(called).to.equal(true); |
| 69 |
}); |
| 70 |
|
| 71 |
it('should not be emitted when another tab is inserted', () => { |
| 72 |
let panel = new TabPanel(); |
| 73 |
panel.addWidget(new Widget()); |
| 74 |
panel.addWidget(new Widget()); |
| 75 |
let called = false; |
| 76 |
panel.currentChanged.connect((sender, args) => { |
| 77 |
called = true; |
| 78 |
}); |
| 79 |
panel.insertWidget(0, new Widget()); |
| 80 |
expect(called).to.equal(false); |
| 81 |
}); |
| 82 |
|
| 83 |
it('should not be emitted when another tab is removed', () => { |
| 84 |
let panel = new TabPanel(); |
| 85 |
panel.addWidget(new Widget()); |
| 86 |
panel.addWidget(new Widget()); |
| 87 |
let called = false; |
| 88 |
panel.currentIndex = 1; |
| 89 |
panel.currentChanged.connect((sender, args) => { |
| 90 |
called = true; |
| 91 |
}); |
| 92 |
panel.widgets[0].parent = null; |
| 93 |
expect(called).to.equal(false); |
| 94 |
}); |
| 95 |
|
| 96 |
it('should not be emitted when the current tab is moved', () => { |
| 97 |
let panel = new TabPanel(); |
| 98 |
panel.addWidget(new Widget()); |
| 99 |
panel.addWidget(new Widget()); |
| 100 |
let called = false; |
| 101 |
panel.currentChanged.connect((sender, args) => { |
| 102 |
called = true; |
| 103 |
}); |
| 104 |
panel.insertWidget(2, panel.widgets[0]); |
| 105 |
expect(called).to.equal(false); |
| 106 |
}); |
| 107 |
}); |
| 108 |
|
| 109 |
describe('#currentIndex', () => { |
| 110 |
it('should get the index of the currently selected tab', () => { |
| 111 |
let panel = new TabPanel(); |
| 112 |
panel.addWidget(new Widget()); |
| 113 |
expect(panel.currentIndex).to.equal(0); |
| 114 |
}); |
| 115 |
|
| 116 |
it('should be `-1` if no tab is selected', () => { |
| 117 |
let panel = new TabPanel(); |
| 118 |
expect(panel.currentIndex).to.equal(-1); |
| 119 |
}); |
| 120 |
|
| 121 |
it('should set the index of the currently selected tab', () => { |
| 122 |
let panel = new TabPanel(); |
| 123 |
panel.addWidget(new Widget()); |
| 124 |
panel.addWidget(new Widget()); |
| 125 |
panel.currentIndex = 1; |
| 126 |
expect(panel.currentIndex).to.equal(1); |
| 127 |
}); |
| 128 |
|
| 129 |
it('should set the index to `-1` if out of range', () => { |
| 130 |
let panel = new TabPanel(); |
| 131 |
panel.addWidget(new Widget()); |
| 132 |
panel.addWidget(new Widget()); |
| 133 |
panel.currentIndex = -2; |
| 134 |
expect(panel.currentIndex).to.equal(-1); |
| 135 |
panel.currentIndex = 2; |
| 136 |
expect(panel.currentIndex).to.equal(-1); |
| 137 |
}); |
| 138 |
}); |
| 139 |
|
| 140 |
describe('#currentWidget', () => { |
| 141 |
it('should get the currently selected tab', () => { |
| 142 |
let panel = new TabPanel(); |
| 143 |
let widget = new Widget(); |
| 144 |
panel.addWidget(widget); |
| 145 |
expect(panel.currentWidget).to.equal(widget); |
| 146 |
}); |
| 147 |
|
| 148 |
it('should be `null` if no tab is selected', () => { |
| 149 |
let panel = new TabPanel(); |
| 150 |
expect(panel.currentWidget).to.equal(null); |
| 151 |
}); |
| 152 |
|
| 153 |
it('should set the currently selected tab', () => { |
| 154 |
let panel = new TabPanel(); |
| 155 |
panel.addWidget(new Widget()); |
| 156 |
let widget = new Widget(); |
| 157 |
panel.addWidget(widget); |
| 158 |
panel.currentWidget = widget; |
| 159 |
expect(panel.currentWidget).to.equal(widget); |
| 160 |
}); |
| 161 |
|
| 162 |
it('should set `null` if the widget is not in the panel', () => { |
| 163 |
let panel = new TabPanel(); |
| 164 |
panel.addWidget(new Widget()); |
| 165 |
panel.addWidget(new Widget()); |
| 166 |
panel.currentWidget = new Widget(); |
| 167 |
expect(panel.currentWidget).to.equal(null); |
| 168 |
}); |
| 169 |
}); |
| 170 |
|
| 171 |
describe('#tabsMovable', () => { |
| 172 |
it('should be the tabsMovable property of the tabBar', () => { |
| 173 |
let panel = new TabPanel(); |
| 174 |
expect(panel.tabsMovable).to.equal(false); |
| 175 |
panel.tabsMovable = true; |
| 176 |
expect(panel.tabBar.tabsMovable).to.equal(true); |
| 177 |
}); |
| 178 |
}); |
| 179 |
|
| 180 |
describe('#tabPlacement', () => { |
| 181 |
it('should be the tab placement for the tab panel', () => { |
| 182 |
let panel = new TabPanel(); |
| 183 |
expect(panel.tabPlacement).to.equal('top'); |
| 184 |
expect(panel.tabBar.orientation).to.equal('horizontal'); |
| 185 |
expect(panel.tabBar.node.getAttribute('data-placement')).to.equal( |
| 186 |
'top' |
| 187 |
); |
| 188 |
|
| 189 |
panel.tabPlacement = 'bottom'; |
| 190 |
expect(panel.tabBar.orientation).to.equal('horizontal'); |
| 191 |
expect(panel.tabBar.node.getAttribute('data-placement')).to.equal( |
| 192 |
'bottom' |
| 193 |
); |
| 194 |
|
| 195 |
panel.tabPlacement = 'left'; |
| 196 |
expect(panel.tabBar.orientation).to.equal('vertical'); |
| 197 |
expect(panel.tabBar.node.getAttribute('data-placement')).to.equal( |
| 198 |
'left' |
| 199 |
); |
| 200 |
|
| 201 |
panel.tabPlacement = 'right'; |
| 202 |
expect(panel.tabBar.orientation).to.equal('vertical'); |
| 203 |
expect(panel.tabBar.node.getAttribute('data-placement')).to.equal( |
| 204 |
'right' |
| 205 |
); |
| 206 |
}); |
| 207 |
}); |
| 208 |
|
| 209 |
describe('#tabBar', () => { |
| 210 |
it('should get the tab bar associated with the tab panel', () => { |
| 211 |
let panel = new TabPanel(); |
| 212 |
let bar = panel.tabBar; |
| 213 |
expect(bar).to.be.an.instanceof(TabBar); |
| 214 |
}); |
| 215 |
|
| 216 |
it('should have the "lm-TabPanel-tabBar" class', () => { |
| 217 |
let panel = new TabPanel(); |
| 218 |
let bar = panel.tabBar; |
| 219 |
expect(bar.hasClass('lm-TabPanel-tabBar')).to.equal(true); |
| 220 |
}); |
| 221 |
|
| 222 |
it('should move the widget in the stacked panel when a tab is moved', () => { |
| 223 |
let panel = new TabPanel(); |
| 224 |
let widgets = [new LogWidget(), new LogWidget()]; |
| 225 |
widgets.forEach(w => { |
| 226 |
panel.addWidget(w); |
| 227 |
}); |
| 228 |
Widget.attach(panel, document.body); |
| 229 |
let bar = panel.tabBar; |
| 230 |
let called = false; |
| 231 |
bar.tabMoved.connect(() => { |
| 232 |
let stack = panel.stackedPanel; |
| 233 |
expect(stack.widgets[1]).to.equal(widgets[0]); |
| 234 |
called = true; |
| 235 |
}); |
| 236 |
(bar.tabMoved as any).emit({ |
| 237 |
fromIndex: 0, |
| 238 |
toIndex: 1, |
| 239 |
title: widgets[0].title |
| 240 |
}); |
| 241 |
expect(called).to.equal(true); |
| 242 |
panel.dispose(); |
| 243 |
}); |
| 244 |
|
| 245 |
it('should show the new widget when the current tab changes', () => { |
| 246 |
let panel = new TabPanel(); |
| 247 |
let widgets = [new LogWidget(), new LogWidget(), new LogWidget()]; |
| 248 |
widgets.forEach(w => { |
| 249 |
panel.addWidget(w); |
| 250 |
}); |
| 251 |
widgets.forEach(w => { |
| 252 |
w.node.tabIndex = -1; |
| 253 |
}); |
| 254 |
Widget.attach(panel, document.body); |
| 255 |
panel.tabBar.currentChanged.connect((sender, args) => { |
| 256 |
expect(widgets[args.previousIndex].isVisible).to.equal(false); |
| 257 |
expect(widgets[args.currentIndex].isVisible).to.equal(true); |
| 258 |
}); |
| 259 |
panel.tabBar.currentIndex = 1; |
| 260 |
panel.dispose(); |
| 261 |
}); |
| 262 |
|
| 263 |
it('should close the widget when a tab is closed', () => { |
| 264 |
let panel = new TabPanel(); |
| 265 |
let widget = new LogWidget(); |
| 266 |
panel.addWidget(widget); |
| 267 |
Widget.attach(panel, document.body); |
| 268 |
let bar = panel.tabBar; |
| 269 |
let called = false; |
| 270 |
bar.tabCloseRequested.connect(() => { |
| 271 |
expect(widget.methods.indexOf('onCloseRequest')).to.not.equal(-1); |
| 272 |
called = true; |
| 273 |
}); |
| 274 |
(bar.tabCloseRequested as any).emit({ index: 0, title: widget.title }); |
| 275 |
expect(called).to.equal(true); |
| 276 |
panel.dispose(); |
| 277 |
}); |
| 278 |
}); |
| 279 |
|
| 280 |
describe('#stackedPanel', () => { |
| 281 |
it('should get the stacked panel associated with the tab panel', () => { |
| 282 |
let panel = new TabPanel(); |
| 283 |
let stack = panel.stackedPanel; |
| 284 |
expect(stack).to.be.an.instanceof(StackedPanel); |
| 285 |
}); |
| 286 |
|
| 287 |
it('should have the "lm-TabPanel-stackedPanel" class', () => { |
| 288 |
let panel = new TabPanel(); |
| 289 |
let stack = panel.stackedPanel; |
| 290 |
expect(stack.hasClass('lm-TabPanel-stackedPanel')).to.equal(true); |
| 291 |
}); |
| 292 |
|
| 293 |
it('remove a tab when a widget is removed from the stacked panel', () => { |
| 294 |
let panel = new TabPanel(); |
| 295 |
let widget = new Widget(); |
| 296 |
panel.addWidget(widget); |
| 297 |
let stack = panel.stackedPanel; |
| 298 |
let called = false; |
| 299 |
stack.widgetRemoved.connect(() => { |
| 300 |
let bar = panel.tabBar; |
| 301 |
expect(bar.titles).to.deep.equal([]); |
| 302 |
called = true; |
| 303 |
}); |
| 304 |
widget.parent = null; |
| 305 |
expect(called).to.equal(true); |
| 306 |
}); |
| 307 |
}); |
| 308 |
|
| 309 |
describe('#widgets', () => { |
| 310 |
it('should get a read-only array of the widgets in the panel', () => { |
| 311 |
let panel = new TabPanel(); |
| 312 |
let widgets = [new Widget(), new Widget(), new Widget()]; |
| 313 |
widgets.forEach(w => { |
| 314 |
panel.addWidget(w); |
| 315 |
}); |
| 316 |
expect(panel.widgets).to.deep.equal(widgets); |
| 317 |
}); |
| 318 |
}); |
| 319 |
|
| 320 |
describe('#addWidget()', () => { |
| 321 |
it('should add a widget to the end of the tab panel', () => { |
| 322 |
let panel = new TabPanel(); |
| 323 |
let widgets = [new Widget(), new Widget(), new Widget()]; |
| 324 |
widgets.forEach(w => { |
| 325 |
panel.addWidget(w); |
| 326 |
}); |
| 327 |
let widget = new Widget(); |
| 328 |
panel.addWidget(widget); |
| 329 |
expect(panel.widgets[3]).to.equal(widget); |
| 330 |
expect(panel.tabBar.titles[2]).to.equal(widgets[2].title); |
| 331 |
}); |
| 332 |
|
| 333 |
it('should move an existing widget', () => { |
| 334 |
let panel = new TabPanel(); |
| 335 |
let widgets = [new Widget(), new Widget(), new Widget()]; |
| 336 |
widgets.forEach(w => { |
| 337 |
panel.addWidget(w); |
| 338 |
}); |
| 339 |
panel.addWidget(widgets[0]); |
| 340 |
expect(panel.widgets[2]).to.equal(widgets[0]); |
| 341 |
}); |
| 342 |
}); |
| 343 |
|
| 344 |
describe('#insertWidget()', () => { |
| 345 |
it('should insert a widget into the tab panel at a specified index', () => { |
| 346 |
let panel = new TabPanel(); |
| 347 |
let widgets = [new Widget(), new Widget(), new Widget()]; |
| 348 |
widgets.forEach(w => { |
| 349 |
panel.addWidget(w); |
| 350 |
}); |
| 351 |
let widget = new Widget(); |
| 352 |
panel.insertWidget(1, widget); |
| 353 |
expect(panel.widgets[1]).to.equal(widget); |
| 354 |
expect(panel.tabBar.titles[1]).to.equal(widget.title); |
| 355 |
}); |
| 356 |
|
| 357 |
it('should move an existing widget', () => { |
| 358 |
let panel = new TabPanel(); |
| 359 |
let widgets = [new Widget(), new Widget(), new Widget()]; |
| 360 |
widgets.forEach(w => { |
| 361 |
panel.addWidget(w); |
| 362 |
}); |
| 363 |
panel.insertWidget(0, widgets[2]); |
| 364 |
expect(panel.widgets[0]).to.equal(widgets[2]); |
| 365 |
}); |
| 366 |
}); |
| 367 |
}); |
| 368 |
}); |