Debian Codegraph @

packages/widgets/tests/src/boxlayout.spec.ts

raw
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 { every } from '@lumino/algorithm';
13
14 import { Message, MessageLoop } from '@lumino/messaging';
15
16 import { BoxLayout, Widget } from '@lumino/widgets';
17
18 class LogBoxLayout extends BoxLayout {
19 methods: string[] = [];
20
21 protected init(): void {
22 super.init();
23 this.methods.push('init');
24 }
25
26 protected attachWidget(index: number, widget: Widget): void {
27 super.attachWidget(index, widget);
28 this.methods.push('attachWidget');
29 }
30
31 protected moveWidget(
32 fromIndex: number,
33 toIndex: number,
34 widget: Widget
35 ): void {
36 super.moveWidget(fromIndex, toIndex, widget);
37 this.methods.push('moveWidget');
38 }
39
40 protected detachWidget(index: number, widget: Widget): void {
41 super.detachWidget(index, widget);
42 this.methods.push('detachWidget');
43 }
44
45 protected onAfterShow(msg: Message): void {
46 super.onAfterShow(msg);
47 this.methods.push('onAfterShow');
48 }
49
50 protected onAfterAttach(msg: Message): void {
51 super.onAfterAttach(msg);
52 this.methods.push('onAfterAttach');
53 }
54
55 protected onChildShown(msg: Widget.ChildMessage): void {
56 super.onChildShown(msg);
57 this.methods.push('onChildShown');
58 }
59
60 protected onChildHidden(msg: Widget.ChildMessage): void {
61 super.onChildHidden(msg);
62 this.methods.push('onChildHidden');
63 }
64
65 protected onResize(msg: Widget.ResizeMessage): void {
66 super.onResize(msg);
67 this.methods.push('onResize');
68 }
69
70 protected onUpdateRequest(msg: Message): void {
71 super.onUpdateRequest(msg);
72 this.methods.push('onUpdateRequest');
73 }
74
75 protected onFitRequest(msg: Message): void {
76 super.onFitRequest(msg);
77 this.methods.push('onFitRequest');
78 }
79 }
80
81 class LogWidget extends Widget {
82 methods: string[] = [];
83
84 protected onAfterAttach(msg: Message): void {
85 super.onAfterAttach(msg);
86 this.methods.push('onAfterAttach');
87 }
88
89 protected onBeforeDetach(msg: Message): void {
90 super.onBeforeDetach(msg);
91 this.methods.push('onBeforeDetach');
92 }
93
94 protected onAfterShow(msg: Message): void {
95 super.onAfterShow(msg);
96 this.methods.push('onAfterShow');
97 }
98
99 protected onUpdateRequest(msg: Message): void {
100 super.onUpdateRequest(msg);
101 this.methods.push('onUpdateRequest');
102 }
103 }
104
105 describe('@lumino/widgets', () => {
106 describe('BoxLayout', () => {
107 describe('constructor()', () => {
108 it('should take no arguments', () => {
109 let layout = new BoxLayout();
110 expect(layout).to.be.an.instanceof(BoxLayout);
111 });
112
113 it('should accept options', () => {
114 let layout = new BoxLayout({ direction: 'bottom-to-top', spacing: 10 });
115 expect(layout.direction).to.equal('bottom-to-top');
116 expect(layout.spacing).to.equal(10);
117 });
118 });
119
120 describe('#direction', () => {
121 it('should default to `"top-to-bottom"`', () => {
122 let layout = new BoxLayout();
123 expect(layout.direction).to.equal('top-to-bottom');
124 });
125
126 it('should set the layout direction for the box layout', () => {
127 let layout = new BoxLayout();
128 layout.direction = 'left-to-right';
129 expect(layout.direction).to.equal('left-to-right');
130 });
131
132 it('should set the direction attribute of the parent widget', () => {
133 let parent = new Widget();
134 let layout = new BoxLayout();
135 parent.layout = layout;
136 layout.direction = 'top-to-bottom';
137 expect(parent.node.getAttribute('data-direction')).to.equal(
138 'top-to-bottom'
139 );
140 layout.direction = 'bottom-to-top';
141 expect(parent.node.getAttribute('data-direction')).to.equal(
142 'bottom-to-top'
143 );
144 layout.direction = 'left-to-right';
145 expect(parent.node.getAttribute('data-direction')).to.equal(
146 'left-to-right'
147 );
148 layout.direction = 'right-to-left';
149 expect(parent.node.getAttribute('data-direction')).to.equal(
150 'right-to-left'
151 );
152 });
153
154 it('should post a fit request to the parent widget', done => {
155 let parent = new Widget();
156 let layout = new LogBoxLayout();
157 parent.layout = layout;
158 layout.direction = 'right-to-left';
159 requestAnimationFrame(() => {
160 expect(layout.methods).to.contain('onFitRequest');
161 done();
162 });
163 });
164
165 it('should be a no-op if the value does not change', done => {
166 let parent = new Widget();
167 let layout = new LogBoxLayout();
168 parent.layout = layout;
169 layout.direction = 'top-to-bottom';
170 requestAnimationFrame(() => {
171 expect(layout.methods).to.not.contain('onFitRequest');
172 done();
173 });
174 });
175 });
176
177 describe('#spacing', () => {
178 it('should default to `4`', () => {
179 let layout = new BoxLayout();
180 expect(layout.spacing).to.equal(4);
181 });
182
183 it('should set the inter-element spacing for the box panel', () => {
184 let layout = new BoxLayout();
185 layout.spacing = 8;
186 expect(layout.spacing).to.equal(8);
187 });
188
189 it('should post a fit request to the parent widget', done => {
190 let parent = new Widget();
191 let layout = new LogBoxLayout();
192 parent.layout = layout;
193 layout.spacing = 8;
194 requestAnimationFrame(() => {
195 expect(layout.methods).to.contain('onFitRequest');
196 done();
197 });
198 });
199
200 it('should be a no-op if the value does not change', done => {
201 let parent = new Widget();
202 let layout = new LogBoxLayout();
203 parent.layout = layout;
204 layout.spacing = 4;
205 requestAnimationFrame(() => {
206 expect(layout.methods).to.not.contain('onFitRequest');
207 done();
208 });
209 });
210 });
211
212 describe('#init()', () => {
213 it('should set the direction attribute on the parent widget', () => {
214 let parent = new Widget();
215 let layout = new LogBoxLayout();
216 parent.layout = layout;
217 expect(parent.node.getAttribute('data-direction')).to.equal(
218 'top-to-bottom'
219 );
220 expect(layout.methods).to.contain('init');
221 parent.dispose();
222 });
223
224 it('should attach the child widgets', () => {
225 let parent = new Widget();
226 let layout = new LogBoxLayout();
227 let widgets = [new Widget(), new Widget(), new Widget()];
228 widgets.forEach(w => {
229 layout.addWidget(w);
230 });
231 parent.layout = layout;
232 expect(every(widgets, w => w.parent === parent));
233 expect(layout.methods).to.contain('attachWidget');
234 parent.dispose();
235 });
236 });
237
238 describe('#attachWidget()', () => {
239 it("should attach a widget to the parent's DOM node", () => {
240 let panel = new Widget();
241 let layout = new LogBoxLayout();
242 let widget = new Widget();
243 panel.layout = layout;
244 layout.addWidget(widget);
245 layout.addWidget(widget);
246 expect(layout.methods).to.contain('attachWidget');
247 expect(panel.node.contains(widget.node)).to.equal(true);
248 panel.dispose();
249 });
250
251 it("should send an `'after-attach'` message if the parent is attached", () => {
252 let panel = new Widget();
253 let layout = new LogBoxLayout();
254 let widget = new LogWidget();
255 panel.layout = layout;
256 Widget.attach(panel, document.body);
257 layout.addWidget(widget);
258 expect(layout.methods).to.contain('attachWidget');
259 expect(widget.methods).to.contain('onAfterAttach');
260 panel.dispose();
261 });
262
263 it('should post a layout request for the parent widget', done => {
264 let panel = new Widget();
265 let layout = new LogBoxLayout();
266 panel.layout = layout;
267 layout.addWidget(new Widget());
268 requestAnimationFrame(() => {
269 expect(layout.methods).to.contain('onFitRequest');
270 panel.dispose();
271 done();
272 });
273 });
274 });
275
276 describe('#moveWidget()', () => {
277 it('should post an update request for the parent widget', done => {
278 let panel = new Widget();
279 let layout = new LogBoxLayout();
280 panel.layout = layout;
281 layout.addWidget(new Widget());
282 let widget = new Widget();
283 layout.addWidget(widget);
284 layout.insertWidget(0, widget);
285 expect(layout.methods).to.contain('moveWidget');
286 requestAnimationFrame(() => {
287 expect(layout.methods).to.contain('onUpdateRequest');
288 panel.dispose();
289 done();
290 });
291 });
292 });
293
294 describe('#detachWidget()', () => {
295 it("should detach a widget from the parent's DOM node", () => {
296 let panel = new Widget();
297 let layout = new LogBoxLayout();
298 let widget = new Widget();
299 panel.layout = layout;
300 layout.addWidget(widget);
301 layout.removeWidget(widget);
302 expect(layout.methods).to.contain('detachWidget');
303 expect(panel.node.contains(widget.node)).to.equal(false);
304 panel.dispose();
305 });
306
307 it("should send a `'before-detach'` message if the parent is attached", () => {
308 let panel = new Widget();
309 let layout = new LogBoxLayout();
310 panel.layout = layout;
311 let widget = new LogWidget();
312 Widget.attach(panel, document.body);
313 layout.addWidget(widget);
314 layout.removeWidget(widget);
315 expect(widget.methods).to.contain('onBeforeDetach');
316 panel.dispose();
317 });
318
319 it('should post a layout request for the parent widget', done => {
320 let panel = new Widget();
321 let layout = new LogBoxLayout();
322 let widget = new Widget();
323 panel.layout = layout;
324 layout.addWidget(widget);
325 requestAnimationFrame(() => {
326 expect(layout.methods).to.contain('onFitRequest');
327 layout.removeWidget(widget);
328 layout.methods = [];
329 requestAnimationFrame(() => {
330 expect(layout.methods).to.contain('onFitRequest');
331 panel.dispose();
332 done();
333 });
334 });
335 });
336 });
337
338 describe('#onAfterShow()', () => {
339 it('should post an update request to the parent', done => {
340 let parent = new LogWidget();
341 let layout = new LogBoxLayout();
342 parent.layout = layout;
343 Widget.attach(parent, document.body);
344 parent.hide();
345 parent.show();
346 expect(parent.methods).to.contain('onAfterShow');
347 expect(layout.methods).to.contain('onAfterShow');
348 requestAnimationFrame(() => {
349 expect(parent.methods).to.contain('onUpdateRequest');
350 parent.dispose();
351 done();
352 });
353 });
354
355 it('should send an `after-show` message to non-hidden child widgets', () => {
356 let parent = new LogWidget();
357 let layout = new LogBoxLayout();
358 parent.layout = layout;
359 let widgets = [new LogWidget(), new LogWidget(), new LogWidget()];
360 let hiddenWidgets = [new LogWidget(), new LogWidget()];
361 widgets.forEach(w => {
362 layout.addWidget(w);
363 });
364 hiddenWidgets.forEach(w => {
365 layout.addWidget(w);
366 });
367 hiddenWidgets.forEach(w => {
368 w.hide();
369 });
370 Widget.attach(parent, document.body);
371 parent.layout = layout;
372 parent.hide();
373 parent.show();
374 expect(every(widgets, w => w.methods.indexOf('after-show') !== -1));
375 expect(
376 every(hiddenWidgets, w => w.methods.indexOf('after-show') === -1)
377 );
378 expect(parent.methods).to.contain('onAfterShow');
379 expect(layout.methods).to.contain('onAfterShow');
380 parent.dispose();
381 });
382 });
383
384 describe('#onAfterAttach()', () => {
385 it('should post a fit request to the parent', done => {
386 let parent = new LogWidget();
387 let layout = new LogBoxLayout();
388 parent.layout = layout;
389 Widget.attach(parent, document.body);
390 expect(parent.methods).to.contain('onAfterAttach');
391 expect(layout.methods).to.contain('onAfterAttach');
392 requestAnimationFrame(() => {
393 expect(layout.methods).to.contain('onFitRequest');
394 parent.dispose();
395 done();
396 });
397 });
398
399 it('should send `after-attach` to all child widgets', () => {
400 let parent = new LogWidget();
401 let layout = new LogBoxLayout();
402 parent.layout = layout;
403 let widgets = [new LogWidget(), new LogWidget(), new LogWidget()];
404 widgets.forEach(w => {
405 layout.addWidget(w);
406 });
407 Widget.attach(parent, document.body);
408 expect(parent.methods).to.contain('onAfterAttach');
409 expect(layout.methods).to.contain('onAfterAttach');
410 expect(every(widgets, w => w.methods.indexOf('onAfterAttach') !== -1));
411 parent.dispose();
412 });
413 });
414
415 describe('#onChildShown()', () => {
416 it('should post or send a fit request to the parent', done => {
417 let parent = new LogWidget();
418 let layout = new LogBoxLayout();
419 parent.layout = layout;
420 let widgets = [new LogWidget(), new LogWidget(), new LogWidget()];
421 widgets[0].hide();
422 widgets.forEach(w => {
423 layout.addWidget(w);
424 });
425 Widget.attach(parent, document.body);
426 widgets[0].show();
427 expect(layout.methods).to.contain('onChildShown');
428 requestAnimationFrame(() => {
429 expect(layout.methods).to.contain('onFitRequest');
430 parent.dispose();
431 done();
432 });
433 });
434 });
435
436 describe('#onChildHidden()', () => {
437 it('should post a fit request to the parent', done => {
438 let parent = new LogWidget();
439 let layout = new LogBoxLayout();
440 parent.layout = layout;
441 let widgets = [new LogWidget(), new LogWidget(), new LogWidget()];
442 widgets.forEach(w => {
443 layout.addWidget(w);
444 });
445 Widget.attach(parent, document.body);
446 widgets[0].hide();
447 expect(layout.methods).to.contain('onChildHidden');
448 requestAnimationFrame(() => {
449 expect(layout.methods).to.contain('onFitRequest');
450 parent.dispose();
451 done();
452 });
453 });
454 });
455
456 describe('#onResize()', () => {
457 it('should be called when a resize event is sent to the parent', () => {
458 let parent = new LogWidget();
459 let layout = new LogBoxLayout();
460 parent.layout = layout;
461 let widgets = [new LogWidget(), new LogWidget(), new LogWidget()];
462 widgets.forEach(w => {
463 layout.addWidget(w);
464 });
465 Widget.attach(parent, document.body);
466 MessageLoop.sendMessage(parent, Widget.ResizeMessage.UnknownSize);
467 expect(layout.methods).to.contain('onResize');
468 parent.dispose();
469 });
470
471 it('should be a no-op if the parent is hidden', () => {
472 let parent = new LogWidget();
473 let layout = new LogBoxLayout();
474 parent.layout = layout;
475 Widget.attach(parent, document.body);
476 parent.hide();
477 MessageLoop.sendMessage(parent, Widget.ResizeMessage.UnknownSize);
478 expect(layout.methods).to.contain('onResize');
479 parent.dispose();
480 });
481 });
482
483 describe('#onUpdateRequest()', () => {
484 it('should be called when the parent is updated', () => {
485 let parent = new Widget();
486 let layout = new LogBoxLayout();
487 parent.layout = layout;
488 MessageLoop.sendMessage(parent, Widget.Msg.UpdateRequest);
489 expect(layout.methods).to.contain('onUpdateRequest');
490 });
491 });
492
493 describe('#onFitRequest()', () => {
494 it('should be called when the parent fit is requested', () => {
495 let parent = new Widget();
496 let layout = new LogBoxLayout();
497 parent.layout = layout;
498 MessageLoop.sendMessage(parent, Widget.Msg.FitRequest);
499 expect(layout.methods).to.contain('onFitRequest');
500 });
501 });
502
503 describe('.getStretch()', () => {
504 it('should get the box panel stretch factor for the given widget', () => {
505 let widget = new Widget();
506 expect(BoxLayout.getStretch(widget)).to.equal(0);
507 });
508 });
509
510 describe('.setStretch()', () => {
511 it('should set the box panel stretch factor for the given widget', () => {
512 let widget = new Widget();
513 BoxLayout.setStretch(widget, 8);
514 expect(BoxLayout.getStretch(widget)).to.equal(8);
515 });
516
517 it("should post a fit request to the widget's parent", done => {
518 let parent = new Widget();
519 let widget = new Widget();
520 let layout = new LogBoxLayout();
521 parent.layout = layout;
522 layout.addWidget(widget);
523 BoxLayout.setStretch(widget, 8);
524 requestAnimationFrame(() => {
525 expect(layout.methods).to.contain('onFitRequest');
526 done();
527 });
528 });
529 });
530
531 describe('.getSizeBasis()', () => {
532 it('should get the box panel size basis for the given widget', () => {
533 let widget = new Widget();
534 expect(BoxLayout.getSizeBasis(widget)).to.equal(0);
535 });
536 });
537
538 describe('.setSizeBasis()', () => {
539 it('should set the box panel size basis for the given widget', () => {
540 let widget = new Widget();
541 BoxLayout.setSizeBasis(widget, 8);
542 expect(BoxLayout.getSizeBasis(widget)).to.equal(8);
543 });
544
545 it("should post a fit request to the widget's parent", done => {
546 let parent = new Widget();
547 let widget = new Widget();
548 let layout = new LogBoxLayout();
549 parent.layout = layout;
550 layout.addWidget(widget);
551 BoxLayout.setSizeBasis(widget, 8);
552 requestAnimationFrame(() => {
553 expect(layout.methods).to.contain('onFitRequest');
554 done();
555 });
556 });
557 });
558 });
559 });