Debian Codegraph @

packages/widgets/tests/src/widget.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 { ArrayExt } from '@lumino/algorithm';
13
14 import { Message, MessageLoop } from '@lumino/messaging';
15
16 import { Layout, Title, Widget } from '@lumino/widgets';
17
18 export class LogWidget extends Widget {
19 messages: string[] = [];
20
21 methods: string[] = [];
22
23 raw: Message[] = [];
24
25 processMessage(msg: Message): void {
26 super.processMessage(msg);
27 this.messages.push(msg.type);
28 }
29
30 protected notifyLayout(msg: Message): void {
31 super.notifyLayout(msg);
32 this.methods.push('notifyLayout');
33 }
34
35 protected onActivateRequest(msg: Message): void {
36 super.onActivateRequest(msg);
37 this.methods.push('onActivateRequest');
38 }
39
40 protected onCloseRequest(msg: Message): void {
41 super.onCloseRequest(msg);
42 this.methods.push('onCloseRequest');
43 }
44
45 protected onResize(msg: Widget.ResizeMessage): void {
46 super.onResize(msg);
47 this.methods.push('onResize');
48 }
49
50 protected onUpdateRequest(msg: Message): void {
51 super.onUpdateRequest(msg);
52 this.methods.push('onUpdateRequest');
53 }
54
55 protected onAfterShow(msg: Message): void {
56 super.onAfterShow(msg);
57 this.methods.push('onAfterShow');
58 }
59
60 protected onBeforeHide(msg: Message): void {
61 super.onBeforeHide(msg);
62 this.methods.push('onBeforeHide');
63 }
64
65 protected onAfterAttach(msg: Message): void {
66 super.onAfterAttach(msg);
67 this.methods.push('onAfterAttach');
68 }
69
70 protected onBeforeDetach(msg: Message): void {
71 super.onBeforeDetach(msg);
72 this.methods.push('onBeforeDetach');
73 }
74
75 protected onChildAdded(msg: Widget.ChildMessage): void {
76 super.onChildAdded(msg);
77 this.methods.push('onChildAdded');
78 this.raw.push(msg);
79 }
80
81 protected onChildRemoved(msg: Widget.ChildMessage): void {
82 super.onChildRemoved(msg);
83 this.methods.push('onChildRemoved');
84 this.raw.push(msg);
85 }
86 }
87
88 class TestLayout extends Layout {
89 dispose(): void {
90 while (this._widgets.length !== 0) {
91 this._widgets.pop()!.dispose();
92 }
93 super.dispose();
94 }
95
96 *[Symbol.iterator](): IterableIterator<Widget> {
97 yield* this._widgets;
98 }
99
100 removeWidget(widget: Widget): void {
101 ArrayExt.removeFirstOf(this._widgets, widget);
102 }
103
104 private _widgets = [new Widget(), new Widget()];
105 }
106
107 describe('@lumino/widgets', () => {
108 describe('Widget', () => {
109 describe('#constructor()', () => {
110 it('should accept no arguments', () => {
111 let widget = new Widget();
112 expect(widget).to.be.an.instanceof(Widget);
113 });
114
115 it('should accept options', () => {
116 let span = document.createElement('span');
117 let widget = new Widget({ node: span });
118 expect(widget.node).to.equal(span);
119 });
120
121 it('should add the `lm-Widget` class', () => {
122 let widget = new Widget();
123 expect(widget.hasClass('lm-Widget')).to.equal(true);
124 });
125 });
126
127 describe('#dispose()', () => {
128 it('should dispose of the widget', () => {
129 let widget = new Widget();
130 widget.dispose();
131 expect(widget.isDisposed).to.equal(true);
132 });
133
134 it('should be a no-op if the widget already disposed', () => {
135 let called = false;
136 let widget = new Widget();
137 widget.dispose();
138 widget.disposed.connect(() => {
139 called = true;
140 });
141 widget.dispose();
142 expect(called).to.equal(false);
143 expect(widget.isDisposed).to.equal(true);
144 });
145
146 it('should remove the widget from its parent', () => {
147 let parent = new Widget();
148 let child = new Widget();
149 child.parent = parent;
150 child.dispose();
151 expect(parent.isDisposed).to.equal(false);
152 expect(child.isDisposed).to.equal(true);
153 expect(child.parent).to.equal(null);
154 });
155
156 it('should automatically detach the widget', () => {
157 let widget = new Widget();
158 Widget.attach(widget, document.body);
159 expect(widget.isAttached).to.equal(true);
160 widget.dispose();
161 expect(widget.isAttached).to.equal(false);
162 });
163
164 it('should dispose of the widget layout', () => {
165 let widget = new Widget();
166 let layout = new TestLayout();
167 widget.layout = layout;
168 widget.dispose();
169 expect(layout.isDisposed).to.equal(true);
170 });
171
172 it('should dispose of the widget title', () => {
173 const widget = new Widget();
174 const title = widget.title;
175 widget.dispose();
176 expect(title.isDisposed).to.equal(true);
177 });
178 });
179
180 describe('#disposed', () => {
181 it('should be emitted when the widget is disposed', () => {
182 let called = false;
183 let widget = new Widget();
184 widget.disposed.connect(() => {
185 called = true;
186 });
187 widget.dispose();
188 expect(called).to.equal(true);
189 });
190 });
191
192 describe('#isDisposed', () => {
193 it('should be `true` if the widget is disposed', () => {
194 let widget = new Widget();
195 widget.dispose();
196 expect(widget.isDisposed).to.equal(true);
197 });
198
199 it('should be `false` if the widget is not disposed', () => {
200 let widget = new Widget();
201 expect(widget.isDisposed).to.equal(false);
202 });
203 });
204
205 describe('#isAttached', () => {
206 it('should be `true` if the widget is attached', () => {
207 let widget = new Widget();
208 Widget.attach(widget, document.body);
209 expect(widget.isAttached).to.equal(true);
210 widget.dispose();
211 });
212
213 it('should be `false` if the widget is not attached', () => {
214 let widget = new Widget();
215 expect(widget.isAttached).to.equal(false);
216 });
217 });
218
219 describe('#isHidden', () => {
220 it('should be `true` if the widget is hidden', () => {
221 let widget = new Widget();
222 Widget.attach(widget, document.body);
223 widget.hide();
224 expect(widget.isHidden).to.equal(true);
225 widget.dispose();
226 });
227
228 it('should be `false` if the widget is not hidden', () => {
229 let widget = new Widget();
230 Widget.attach(widget, document.body);
231 expect(widget.isHidden).to.equal(false);
232 widget.dispose();
233 });
234 });
235
236 describe('#isVisible', () => {
237 it('should be `true` if the widget is visible', () => {
238 let widget = new Widget();
239 Widget.attach(widget, document.body);
240 expect(widget.isVisible).to.equal(true);
241 widget.dispose();
242 });
243
244 it('should be `false` if the widget is not visible', () => {
245 let widget = new Widget();
246 Widget.attach(widget, document.body);
247 widget.hide();
248 expect(widget.isVisible).to.equal(false);
249 widget.dispose();
250 });
251
252 it('should be `false` if the widget is not attached', () => {
253 let widget = new Widget();
254 expect(widget.isVisible).to.equal(false);
255 });
256
257 it('should be false if the widget is not hidden but its parent is hidden', () => {
258 let parent = new Widget();
259 parent.hide();
260 let widget = new Widget();
261 widget.parent = parent;
262 expect(widget.isHidden).to.equal(false);
263 expect(widget.isVisible).to.equal(false);
264 });
265 });
266
267 describe('#node', () => {
268 it('should get the DOM node owned by the widget', () => {
269 let widget = new Widget();
270 let node = widget.node;
271 expect(node.tagName.toLowerCase()).to.equal('div');
272 });
273 });
274
275 describe('#id', () => {
276 it('should get the id of the widget node', () => {
277 let widget = new Widget();
278 widget.node.id = 'foo';
279 expect(widget.id).to.equal('foo');
280 });
281
282 it('should set the id of the widget node', () => {
283 let widget = new Widget();
284 widget.id = 'bar';
285 expect(widget.node.id).to.equal('bar');
286 });
287 });
288
289 describe('#dataset', () => {
290 it('should get the dataset of the widget node', () => {
291 let widget = new Widget();
292 expect(widget.dataset).to.equal(widget.node.dataset);
293 });
294 });
295
296 describe('#title', () => {
297 it('should get the title data object for the widget', () => {
298 let widget = new Widget();
299 expect(widget.title).to.be.an.instanceof(Title);
300 });
301 });
302
303 describe('#parent', () => {
304 it('should default to `null`', () => {
305 let widget = new Widget();
306 expect(widget.parent).to.equal(null);
307 });
308
309 it('should set the parent and send a `child-added` messagee', () => {
310 let child = new Widget();
311 let parent = new LogWidget();
312 child.parent = parent;
313 expect(child.parent).to.equal(parent);
314 expect(parent.messages).to.contain('child-added');
315 });
316
317 it('should remove itself from the current parent', () => {
318 let parent0 = new LogWidget();
319 let parent1 = new LogWidget();
320 let child = new Widget();
321 child.parent = parent0;
322 child.parent = parent1;
323 expect(parent0.messages).to.contain('child-removed');
324 expect(parent1.messages).to.contain('child-added');
325 });
326
327 it('should throw an error if the widget contains the parent', () => {
328 let widget0 = new Widget();
329 let widget1 = new Widget();
330 widget0.parent = widget1;
331 expect(() => {
332 widget1.parent = widget0;
333 }).to.throw(Error);
334 });
335
336 it('should be a no-op if there is no parent change', () => {
337 let parent = new LogWidget();
338 let child = new Widget();
339 child.parent = parent;
340 child.parent = parent;
341 expect(parent.messages).to.not.contain('child-removed');
342 });
343 });
344
345 describe('#layout', () => {
346 it('should default to `null`', () => {
347 let widget = new Widget();
348 expect(widget.layout).to.equal(null);
349 });
350
351 it('should set the layout for the widget', () => {
352 let widget = new Widget();
353 let layout = new TestLayout();
354 widget.layout = layout;
355 expect(widget.layout).to.equal(layout);
356 });
357
358 it('should be single-use only', () => {
359 let widget = new Widget();
360 widget.layout = new TestLayout();
361 expect(() => {
362 widget.layout = new TestLayout();
363 }).to.throw(Error);
364 });
365
366 it('should be disposed when the widget is disposed', () => {
367 let widget = new Widget();
368 let layout = new TestLayout();
369 widget.layout = layout;
370 widget.dispose();
371 expect(layout.isDisposed).to.equal(true);
372 });
373
374 it('should be a no-op if the layout is the same', () => {
375 let widget = new Widget();
376 let layout = new TestLayout();
377 widget.layout = layout;
378 expect(widget.layout).to.equal(layout);
379 });
380
381 it('should throw an error if the layout already has a parent', () => {
382 let widget0 = new Widget();
383 let widget1 = new Widget();
384 let layout = new TestLayout();
385 widget0.layout = layout;
386 expect(() => {
387 widget1.layout = layout;
388 }).to.throw(Error);
389 });
390
391 it('should throw an error if the `DisallowLayout` flag is set', () => {
392 let widget = new Widget();
393 widget.setFlag(Widget.Flag.DisallowLayout);
394 let layout = new TestLayout();
395 expect(() => {
396 widget.layout = layout;
397 }).to.throw(Error);
398 });
399 });
400
401 describe('#children()', () => {
402 it('should return an iterator over the widget children', () => {
403 let widget = new Widget();
404 widget.layout = new TestLayout();
405 for (const child of widget.children()) {
406 expect(child).to.be.an.instanceof(Widget);
407 }
408 });
409
410 it('should return an empty iterator if there is no layout', () => {
411 let widget = new Widget();
412 expect(widget.children().next().done).to.equal(true);
413 });
414 });
415
416 describe('#contains()', () => {
417 it('should return `true` if the widget is a descendant', () => {
418 let p1 = new Widget();
419 let p2 = new Widget();
420 let p3 = new Widget();
421 let w1 = new Widget();
422 let w2 = new Widget();
423 p2.parent = p1;
424 p3.parent = p2;
425 w1.parent = p2;
426 w2.parent = p3;
427 expect(p1.contains(p1)).to.equal(true);
428 expect(p1.contains(p2)).to.equal(true);
429 expect(p1.contains(p3)).to.equal(true);
430 expect(p1.contains(w1)).to.equal(true);
431 expect(p1.contains(w2)).to.equal(true);
432 expect(p2.contains(p2)).to.equal(true);
433 expect(p2.contains(p3)).to.equal(true);
434 expect(p2.contains(w1)).to.equal(true);
435 expect(p2.contains(w2)).to.equal(true);
436 expect(p3.contains(p3)).to.equal(true);
437 expect(p3.contains(w2)).to.equal(true);
438 });
439
440 it('should return `false` if the widget is not a descendant', () => {
441 let p1 = new Widget();
442 let p2 = new Widget();
443 let p3 = new Widget();
444 let w1 = new Widget();
445 let w2 = new Widget();
446 p2.parent = p1;
447 p3.parent = p2;
448 w1.parent = p2;
449 w2.parent = p3;
450 expect(p2.contains(p1)).to.equal(false);
451 expect(p3.contains(p1)).to.equal(false);
452 expect(p3.contains(p2)).to.equal(false);
453 expect(p3.contains(w1)).to.equal(false);
454 expect(w1.contains(p1)).to.equal(false);
455 expect(w1.contains(p2)).to.equal(false);
456 expect(w1.contains(p3)).to.equal(false);
457 expect(w1.contains(w2)).to.equal(false);
458 expect(w2.contains(p1)).to.equal(false);
459 expect(w2.contains(p2)).to.equal(false);
460 expect(w2.contains(p3)).to.equal(false);
461 expect(w2.contains(w1)).to.equal(false);
462 });
463 });
464
465 describe('#hasClass()', () => {
466 it('should return `true` if a node has a class', () => {
467 let widget = new Widget();
468 widget.node.classList.add('foo');
469 widget.node.classList.add('bar');
470 widget.node.classList.add('baz');
471 expect(widget.hasClass('foo')).to.equal(true);
472 expect(widget.hasClass('bar')).to.equal(true);
473 expect(widget.hasClass('baz')).to.equal(true);
474 });
475
476 it('should return `false` if a node does not have a class', () => {
477 let widget = new Widget();
478 widget.node.classList.add('foo');
479 widget.node.classList.add('bar');
480 widget.node.classList.add('baz');
481 expect(widget.hasClass('one')).to.equal(false);
482 expect(widget.hasClass('two')).to.equal(false);
483 expect(widget.hasClass('three')).to.equal(false);
484 });
485 });
486
487 describe('#addClass()', () => {
488 it('should add a class to the DOM node', () => {
489 let widget = new Widget();
490 expect(widget.node.classList.contains('foo')).to.equal(false);
491 widget.addClass('foo');
492 expect(widget.node.classList.contains('foo')).to.equal(true);
493 expect(widget.node.classList.contains('bar')).to.equal(false);
494 widget.addClass('bar');
495 expect(widget.node.classList.contains('bar')).to.equal(true);
496 });
497
498 it('should be a no-op if the class is already present', () => {
499 let widget = new Widget();
500 widget.addClass('foo');
501 expect(widget.node.classList.contains('foo')).to.equal(true);
502 widget.addClass('foo');
503 expect(widget.node.classList.contains('foo')).to.equal(true);
504 });
505 });
506
507 describe('#removeClass()', () => {
508 it('should remove the class from the DOM node', () => {
509 let widget = new Widget();
510 widget.node.classList.add('foo');
511 widget.node.classList.add('bar');
512 widget.removeClass('foo');
513 expect(widget.node.classList.contains('foo')).to.equal(false);
514 expect(widget.node.classList.contains('bar')).to.equal(true);
515 widget.removeClass('bar');
516 expect(widget.node.classList.contains('bar')).to.equal(false);
517 });
518
519 it('should be a no-op if the class is not present', () => {
520 let widget = new Widget();
521 expect(widget.node.classList.contains('foo')).to.equal(false);
522 widget.removeClass('foo');
523 expect(widget.node.classList.contains('foo')).to.equal(false);
524 });
525 });
526
527 describe('#toggleClass()', () => {
528 it('should toggle the presence of a class', () => {
529 let widget = new Widget();
530 widget.toggleClass('foo');
531 expect(widget.node.classList.contains('foo')).to.equal(true);
532 widget.toggleClass('foo');
533 expect(widget.node.classList.contains('foo')).to.equal(false);
534 });
535
536 it('should force-add a class', () => {
537 let widget = new Widget();
538 expect(widget.node.classList.contains('foo')).to.equal(false);
539 widget.toggleClass('foo', true);
540 expect(widget.node.classList.contains('foo')).to.equal(true);
541 widget.toggleClass('foo', true);
542 expect(widget.node.classList.contains('foo')).to.equal(true);
543 });
544
545 it('should force-remove a class', () => {
546 let widget = new Widget();
547 widget.node.classList.add('foo');
548 expect(widget.node.classList.contains('foo')).to.equal(true);
549 widget.toggleClass('foo', false);
550 expect(widget.node.classList.contains('foo')).to.equal(false);
551 widget.toggleClass('foo', false);
552 expect(widget.node.classList.contains('foo')).to.equal(false);
553 });
554
555 it('should return `true` if the class is present', () => {
556 let widget = new Widget();
557 expect(widget.toggleClass('foo')).to.equal(true);
558 expect(widget.toggleClass('foo', true)).to.equal(true);
559 });
560
561 it('should return `false` if the class is not present', () => {
562 let widget = new Widget();
563 widget.node.classList.add('foo');
564 expect(widget.toggleClass('foo')).to.equal(false);
565 expect(widget.toggleClass('foo', false)).to.equal(false);
566 });
567 });
568
569 describe('#update()', () => {
570 it('should post an `update-request` message', done => {
571 let widget = new LogWidget();
572 widget.update();
573 expect(widget.messages).to.deep.equal([]);
574 requestAnimationFrame(() => {
575 expect(widget.messages).to.deep.equal(['update-request']);
576 done();
577 });
578 });
579 });
580
581 describe('#fit()', () => {
582 it('should post a `fit-request` message to the widget', done => {
583 let widget = new LogWidget();
584 widget.fit();
585 expect(widget.messages).to.deep.equal([]);
586 requestAnimationFrame(() => {
587 expect(widget.messages).to.deep.equal(['fit-request']);
588 done();
589 });
590 });
591 });
592
593 describe('#activate()', () => {
594 it('should post an `activate-request` message', done => {
595 let widget = new LogWidget();
596 widget.activate();
597 expect(widget.messages).to.deep.equal([]);
598 requestAnimationFrame(() => {
599 expect(widget.messages).to.deep.equal(['activate-request']);
600 done();
601 });
602 });
603 });
604
605 describe('#close()', () => {
606 it('should send a `close-request` message', () => {
607 let widget = new LogWidget();
608 expect(widget.messages).to.deep.equal([]);
609 widget.close();
610 expect(widget.messages).to.deep.equal(['close-request']);
611 });
612 });
613
614 describe('#show()', () => {
615 it('should set `isHidden` to `false`', () => {
616 let widget = new Widget();
617 widget.hide();
618 expect(widget.isHidden).to.equal(true);
619 widget.show();
620 expect(widget.isHidden).to.equal(false);
621 });
622
623 it('should remove the "lm-mod-hidden" class', () => {
624 let widget = new Widget();
625 widget.hide();
626 expect(widget.hasClass('lm-mod-hidden')).to.equal(true);
627 widget.show();
628 expect(widget.hasClass('lm-mod-hidden')).to.equal(false);
629 });
630
631 it('should send an `after-show` message if applicable', () => {
632 let widget = new LogWidget();
633 widget.hide();
634 Widget.attach(widget, document.body);
635 widget.show();
636 expect(widget.messages).to.contains('after-show');
637 widget.dispose();
638 });
639
640 it('should send a `child-shown` message to the parent', () => {
641 let parent = new LogWidget();
642 let child = new Widget();
643 child.parent = parent;
644 child.hide();
645 child.show();
646 expect(parent.messages).to.contains('child-shown');
647 });
648
649 it('should be a no-op if not hidden', () => {
650 let widget = new LogWidget();
651 Widget.attach(widget, document.body);
652 widget.show();
653 expect(widget.messages).to.not.contains('after-show');
654 widget.dispose();
655 });
656 });
657
658 describe('#hide()', () => {
659 it('should hide the widget', () => {
660 let widget = new Widget();
661 widget.hide();
662 expect(widget.isHidden).to.equal(true);
663 });
664
665 it('should add the `lm-mod-hidden` class', () => {
666 let widget = new Widget();
667 widget.hide();
668 expect(widget.hasClass('lm-mod-hidden')).to.equal(true);
669 });
670
671 it('should send a `before-hide` message if applicable', () => {
672 let widget = new LogWidget();
673 Widget.attach(widget, document.body);
674 widget.hide();
675 expect(widget.messages).to.contain('before-hide');
676 widget.dispose();
677 });
678
679 it('should send a `child-hidden` message to the parent', () => {
680 let parent = new LogWidget();
681 let child = new Widget();
682 child.parent = parent;
683 child.hide();
684 expect(parent.messages).to.contain('child-hidden');
685 });
686
687 it('should be a no-op if already hidden', () => {
688 let widget = new LogWidget();
689 widget.hide();
690 Widget.attach(widget, document.body);
691 widget.hide();
692 expect(widget.messages).to.not.contain('before-hide');
693 widget.dispose();
694 });
695 });
696
697 describe('#setHidden()', () => {
698 it('should call hide if `hidden = true`', () => {
699 let widget = new LogWidget();
700 Widget.attach(widget, document.body);
701 widget.setHidden(true);
702 expect(widget.isHidden).to.equal(true);
703 expect(widget.messages).to.contain('before-hide');
704 widget.dispose();
705 });
706
707 it('should call show if `hidden = false`', () => {
708 let widget = new LogWidget();
709 widget.hide();
710 Widget.attach(widget, document.body);
711 widget.setHidden(false);
712 expect(widget.isHidden).to.equal(false);
713 expect(widget.messages).to.contain('after-show');
714 widget.dispose();
715 });
716 });
717
718 describe('#hiddenMode', () => {
719 it('should use class to hide the widget by default', () => {
720 let widget = new Widget();
721 Widget.attach(widget, document.body);
722 widget.hide();
723 expect(widget.hasClass('lm-mod-hidden')).to.equal(true);
724 expect(widget.node.style.transform).to.be.equal('');
725 expect(widget.node.style.willChange).to.not.equal('transform');
726 widget.dispose();
727 });
728
729 it('should use transformation if in "scale" mode', () => {
730 let widget = new Widget();
731 Widget.attach(widget, document.body);
732 widget.hiddenMode = Widget.HiddenMode.Scale;
733 widget.hide();
734 expect(widget.hasClass('lm-mod-hidden')).to.equal(false);
735 expect(widget.node.style.transform).to.equal('scale(0)');
736 expect(widget.node.style.willChange).to.equal('transform');
737 widget.dispose();
738 });
739
740 it('should remove class when switching from display to scale', () => {
741 let widget = new Widget();
742 Widget.attach(widget, document.body);
743 widget.hide();
744 widget.hiddenMode = Widget.HiddenMode.Scale;
745 expect(widget.hasClass('lm-mod-hidden')).to.equal(false);
746 expect(widget.node.style.transform).to.equal('scale(0)');
747 expect(widget.node.style.willChange).to.equal('transform');
748 widget.dispose();
749 });
750
751 for (const fromMode of [
752 Widget.HiddenMode.Scale,
753 Widget.HiddenMode.ContentVisibility
754 ]) {
755 it(`should add class when switching from ${fromMode} to display`, () => {
756 let widget = new Widget();
757 Widget.attach(widget, document.body);
758 widget.hiddenMode = fromMode;
759 widget.hide();
760 widget.hiddenMode = Widget.HiddenMode.Display;
761 expect(widget.hasClass('lm-mod-hidden')).to.equal(true);
762 expect(widget.node.style.transform).to.equal('');
763 expect(widget.node.style.willChange).to.equal('auto');
764 widget.dispose();
765 });
766 }
767
768 it('should use content-visibility in relevant mode', () => {
769 let widget = new Widget();
770 Widget.attach(widget, document.body);
771 widget.hiddenMode = Widget.HiddenMode.ContentVisibility;
772 widget.hide();
773 expect(widget.hasClass('lm-mod-hidden')).to.equal(false);
774 // @ts-expect-error content-visibility unknown by DOM lib types
775 expect(widget.node.style.contentVisibility).to.equal('hidden');
776 expect(widget.node.style.willChange).to.equal('auto');
777 widget.dispose();
778 });
779
780 it('should add class when switching from content-visibility to display', () => {
781 let widget = new Widget();
782 Widget.attach(widget, document.body);
783 widget.hiddenMode = Widget.HiddenMode.ContentVisibility;
784 widget.hide();
785 widget.hiddenMode = Widget.HiddenMode.Display;
786 expect(widget.hasClass('lm-mod-hidden')).to.equal(true);
787 expect(widget.node.style.transform).to.equal('');
788 expect(widget.node.style.willChange).to.equal('auto');
789 widget.dispose();
790 });
791 });
792
793 describe('#testFlag()', () => {
794 it('should test whether the given widget flag is set', () => {
795 let widget = new Widget();
796 expect(widget.testFlag(Widget.Flag.IsHidden)).to.equal(false);
797 });
798 });
799
800 describe('#setFlag()', () => {
801 it('should set the given widget flag', () => {
802 let widget = new Widget();
803 widget.setFlag(Widget.Flag.IsHidden);
804 expect(widget.testFlag(Widget.Flag.IsHidden)).to.equal(true);
805 });
806 });
807
808 describe('#clearFlag()', () => {
809 it('should clear the given widget flag', () => {
810 let widget = new Widget();
811 widget.setFlag(Widget.Flag.IsHidden);
812 widget.clearFlag(Widget.Flag.IsHidden);
813 expect(widget.testFlag(Widget.Flag.IsHidden)).to.equal(false);
814 });
815 });
816
817 describe('#notifyLayout()', () => {
818 it("should send a message to the widget's layout", () => {
819 let child = new LogWidget();
820 let parent = new LogWidget();
821 let layout = new TestLayout();
822 parent.layout = layout;
823 child.parent = parent;
824 expect(parent.methods).to.contain('notifyLayout');
825 });
826 });
827
828 describe('#onActivateRequest()', () => {
829 it('should be invoked on an `activate-request', () => {
830 let widget = new LogWidget();
831 MessageLoop.sendMessage(widget, Widget.Msg.ActivateRequest);
832 expect(widget.methods).to.contain('onActivateRequest');
833 });
834
835 it('should notify the layout', () => {
836 let widget = new LogWidget();
837 MessageLoop.sendMessage(widget, Widget.Msg.ActivateRequest);
838 expect(widget.methods).to.contain('notifyLayout');
839 });
840 });
841
842 describe('#onCloseRequest()', () => {
843 it('should be invoked on a `close-request`', () => {
844 let widget = new LogWidget();
845 MessageLoop.sendMessage(widget, Widget.Msg.CloseRequest);
846 expect(widget.messages).to.contain('close-request');
847 expect(widget.methods).to.contain('onCloseRequest');
848 });
849
850 it('should unparent a child widget by default', () => {
851 let parent = new Widget();
852 let child = new Widget();
853 child.parent = parent;
854 MessageLoop.sendMessage(child, Widget.Msg.CloseRequest);
855 expect(child.parent).to.equal(null);
856 });
857
858 it('should detach a root widget by default', () => {
859 let widget = new Widget();
860 Widget.attach(widget, document.body);
861 MessageLoop.sendMessage(widget, Widget.Msg.CloseRequest);
862 expect(widget.isAttached).to.equal(false);
863 });
864
865 it('should notify the layout', () => {
866 let widget = new LogWidget();
867 MessageLoop.sendMessage(widget, Widget.Msg.CloseRequest);
868 expect(widget.methods).to.contain('notifyLayout');
869 });
870 });
871
872 describe('#onResize()', () => {
873 it('should be invoked when the widget is resized', () => {
874 let widget = new LogWidget();
875 Widget.attach(widget, document.body);
876 MessageLoop.sendMessage(widget, Widget.ResizeMessage.UnknownSize);
877 expect(widget.messages).to.contain('resize');
878 expect(widget.methods).to.contain('onResize');
879 widget.dispose();
880 });
881
882 it('should notify the layout', () => {
883 let widget = new LogWidget();
884 Widget.attach(widget, document.body);
885 MessageLoop.sendMessage(widget, Widget.ResizeMessage.UnknownSize);
886 expect(widget.methods).to.contain('notifyLayout');
887 widget.dispose();
888 });
889 });
890
891 describe('#onUpdateRequest()', () => {
892 it('should be invoked when an update is requested', () => {
893 let widget = new LogWidget();
894 MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
895 expect(widget.messages).to.contain('update-request');
896 expect(widget.methods).to.contain('onUpdateRequest');
897 });
898
899 it('should notify the layout', () => {
900 let widget = new LogWidget();
901 MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
902 expect(widget.methods).to.contain('notifyLayout');
903 });
904 });
905
906 describe('#onAfterShow()', () => {
907 it('should be invoked just after the widget is made visible', () => {
908 let widget = new LogWidget();
909 Widget.attach(widget, document.body);
910 widget.hide();
911 widget.show();
912 expect(widget.messages).to.contain('after-show');
913 expect(widget.methods).to.contain('onAfterShow');
914 widget.dispose();
915 });
916
917 it('should set the `isVisible` flag', () => {
918 let widget = new LogWidget();
919 Widget.attach(widget, document.body);
920 widget.hide();
921 expect(widget.testFlag(Widget.Flag.IsVisible)).to.equal(false);
922 widget.show();
923 expect(widget.testFlag(Widget.Flag.IsVisible)).to.equal(true);
924 widget.dispose();
925 });
926
927 it('should notify the layout', () => {
928 let widget = new LogWidget();
929 Widget.attach(widget, document.body);
930 widget.hide();
931 widget.show();
932 expect(widget.methods).to.contain('notifyLayout');
933 widget.dispose();
934 });
935 });
936
937 describe('#onBeforeHide()', () => {
938 it('should be invoked just before the widget is made not-visible', () => {
939 let widget = new LogWidget();
940 Widget.attach(widget, document.body);
941 widget.hide();
942 expect(widget.messages).to.contain('before-hide');
943 expect(widget.methods).to.contain('onBeforeHide');
944 widget.dispose();
945 });
946
947 it('should clear the `isVisible` flag', () => {
948 let widget = new LogWidget();
949 Widget.attach(widget, document.body);
950 expect(widget.testFlag(Widget.Flag.IsVisible)).to.equal(true);
951 widget.hide();
952 expect(widget.testFlag(Widget.Flag.IsVisible)).to.equal(false);
953 widget.dispose();
954 });
955
956 it('should notify the layout', () => {
957 let widget = new LogWidget();
958 Widget.attach(widget, document.body);
959 widget.hide();
960 expect(widget.methods).to.contain('notifyLayout');
961 widget.dispose();
962 });
963 });
964
965 describe('#onAfterAttach()', () => {
966 it('should be invoked just after the widget is attached', () => {
967 let widget = new LogWidget();
968 Widget.attach(widget, document.body);
969 expect(widget.messages).to.contain('after-attach');
970 expect(widget.methods).to.contain('onAfterAttach');
971 widget.dispose();
972 });
973
974 it('should set the visible flag if warranted', () => {
975 let widget = new LogWidget();
976 Widget.attach(widget, document.body);
977 expect(widget.testFlag(Widget.Flag.IsVisible)).to.equal(true);
978 Widget.detach(widget);
979
980 widget.hide();
981 Widget.attach(widget, document.body);
982 expect(widget.testFlag(Widget.Flag.IsVisible)).to.equal(false);
983 Widget.detach(widget);
984
985 let child = new LogWidget();
986 child.parent = widget;
987 widget.show();
988 Widget.attach(widget, document.body);
989 expect(widget.testFlag(Widget.Flag.IsVisible)).to.equal(true);
990 Widget.detach(widget);
991 });
992
993 it('should notify the layout', () => {
994 let widget = new LogWidget();
995 Widget.attach(widget, document.body);
996 expect(widget.methods).to.contain('notifyLayout');
997 widget.dispose();
998 });
999 });
1000
1001 describe('#onBeforeDetach()', () => {
1002 it('should be invoked just before the widget is detached', () => {
1003 let widget = new LogWidget();
1004 Widget.attach(widget, document.body);
1005 Widget.detach(widget);
1006 expect(widget.messages).to.contain('before-detach');
1007 expect(widget.methods).to.contain('onBeforeDetach');
1008 });
1009
1010 it('should clear the `isVisible` and `isAttached` flags', () => {
1011 let widget = new LogWidget();
1012 Widget.attach(widget, document.body);
1013 expect(widget.testFlag(Widget.Flag.IsVisible)).to.equal(true);
1014 expect(widget.testFlag(Widget.Flag.IsAttached)).to.equal(true);
1015 Widget.detach(widget);
1016 expect(widget.testFlag(Widget.Flag.IsVisible)).to.equal(false);
1017 expect(widget.testFlag(Widget.Flag.IsAttached)).to.equal(false);
1018 });
1019
1020 it('should notify the layout', () => {
1021 let widget = new LogWidget();
1022 Widget.attach(widget, document.body);
1023 Widget.detach(widget);
1024 expect(widget.methods).to.contain('notifyLayout');
1025 });
1026 });
1027
1028 describe('#onChildAdded()', () => {
1029 it('should be invoked when a child is added', () => {
1030 let child = new Widget();
1031 let parent = new LogWidget();
1032 child.parent = parent;
1033 expect(parent.methods).to.contain('onChildAdded');
1034 });
1035
1036 it('should notify the layout', () => {
1037 let child = new Widget();
1038 let parent = new LogWidget();
1039 child.parent = parent;
1040 expect(parent.methods).to.contain('notifyLayout');
1041 });
1042
1043 context('`msg` parameter', () => {
1044 it('should be a `ChildMessage`', () => {
1045 let child = new Widget();
1046 let parent = new LogWidget();
1047 child.parent = parent;
1048 expect(parent.raw[0]).to.be.an.instanceof(Widget.ChildMessage);
1049 });
1050
1051 it('should have a `type` of `child-added`', () => {
1052 let child = new Widget();
1053 let parent = new LogWidget();
1054 child.parent = parent;
1055 expect(parent.raw[0].type).to.equal('child-added');
1056 });
1057
1058 it('should have the correct `child`', () => {
1059 let child = new Widget();
1060 let parent = new LogWidget();
1061 child.parent = parent;
1062 expect((parent.raw[0] as Widget.ChildMessage).child).to.equal(child);
1063 });
1064 });
1065 });
1066
1067 describe('#onChildRemoved()', () => {
1068 it('should be invoked when a child is removed', () => {
1069 let child = new Widget();
1070 let parent = new LogWidget();
1071 child.parent = parent;
1072 child.parent = null;
1073 expect(parent.methods).to.contain('onChildRemoved');
1074 });
1075
1076 it('should notify the layout', () => {
1077 let child = new Widget();
1078 let parent = new LogWidget();
1079 child.parent = parent;
1080 parent.methods = [];
1081 child.parent = null;
1082 expect(parent.methods).to.contain('notifyLayout');
1083 });
1084
1085 context('`msg` parameter', () => {
1086 it('should be a `ChildMessage`', () => {
1087 let child = new Widget();
1088 let parent = new LogWidget();
1089 child.parent = parent;
1090 parent.raw = [];
1091 child.parent = null;
1092 expect(parent.raw[0]).to.be.an.instanceof(Widget.ChildMessage);
1093 });
1094
1095 it('should have a `type` of `child-removed`', () => {
1096 let child = new Widget();
1097 let parent = new LogWidget();
1098 child.parent = parent;
1099 parent.raw = [];
1100 child.parent = null;
1101 expect((parent.raw[0] as Widget.ChildMessage).type).to.equal(
1102 'child-removed'
1103 );
1104 });
1105
1106 it('should have the correct `child`', () => {
1107 let child = new Widget();
1108 let parent = new LogWidget();
1109 child.parent = parent;
1110 parent.raw = [];
1111 child.parent = null;
1112 expect((parent.raw[0] as Widget.ChildMessage).child).to.equal(child);
1113 });
1114 });
1115 });
1116
1117 describe('.ChildMessage', () => {
1118 describe('#constructor()', () => {
1119 it('should accept the message type and child widget', () => {
1120 let msg = new Widget.ChildMessage('test', new Widget());
1121 expect(msg).to.be.an.instanceof(Widget.ChildMessage);
1122 });
1123 });
1124
1125 describe('#child', () => {
1126 it('should be the child passed to the constructor', () => {
1127 let widget = new Widget();
1128 let msg = new Widget.ChildMessage('test', widget);
1129 expect(msg.child).to.equal(widget);
1130 });
1131 });
1132 });
1133
1134 describe('.ResizeMessage', () => {
1135 describe('#constructor()', () => {
1136 it('should accept a width and height', () => {
1137 let msg = new Widget.ResizeMessage(100, 100);
1138 expect(msg).to.be.an.instanceof(Widget.ResizeMessage);
1139 });
1140 });
1141
1142 describe('#width', () => {
1143 it('should be the width passed to the constructor', () => {
1144 let msg = new Widget.ResizeMessage(100, 200);
1145 expect(msg.width).to.equal(100);
1146 });
1147 });
1148
1149 describe('#height', () => {
1150 it('should be the height passed to the constructor', () => {
1151 let msg = new Widget.ResizeMessage(100, 200);
1152 expect(msg.height).to.equal(200);
1153 });
1154 });
1155
1156 describe('.UnknownSize', () => {
1157 it('should be a `ResizeMessage`', () => {
1158 let msg = Widget.ResizeMessage.UnknownSize;
1159 expect(msg).to.be.an.instanceof(Widget.ResizeMessage);
1160 });
1161
1162 it('should have a `width` of `-1`', () => {
1163 let msg = Widget.ResizeMessage.UnknownSize;
1164 expect(msg.width).to.equal(-1);
1165 });
1166
1167 it('should have a `height` of `-1`', () => {
1168 let msg = Widget.ResizeMessage.UnknownSize;
1169 expect(msg.height).to.equal(-1);
1170 });
1171 });
1172 });
1173
1174 describe('.attach()', () => {
1175 it('should attach a root widget to a host', () => {
1176 let widget = new Widget();
1177 expect(widget.isAttached).to.equal(false);
1178 Widget.attach(widget, document.body);
1179 expect(widget.isAttached).to.equal(true);
1180 widget.dispose();
1181 });
1182
1183 it('should throw if the widget is not a root', () => {
1184 let parent = new Widget();
1185 let child = new Widget();
1186 child.parent = parent;
1187 expect(() => {
1188 Widget.attach(child, document.body);
1189 }).to.throw(Error);
1190 });
1191
1192 it('should throw if the widget is already attached', () => {
1193 let widget = new Widget();
1194 Widget.attach(widget, document.body);
1195 expect(() => {
1196 Widget.attach(widget, document.body);
1197 }).to.throw(Error);
1198 widget.dispose();
1199 });
1200
1201 it('should throw if the host is not attached to the DOM', () => {
1202 let widget = new Widget();
1203 let host = document.createElement('div');
1204 expect(() => {
1205 Widget.attach(widget, host);
1206 }).to.throw(Error);
1207 });
1208
1209 it('should dispatch an `after-attach` message', () => {
1210 let widget = new LogWidget();
1211 expect(widget.isAttached).to.equal(false);
1212 expect(widget.messages).to.not.contain('after-attach');
1213 Widget.attach(widget, document.body);
1214 expect(widget.isAttached).to.equal(true);
1215 expect(widget.messages).to.contain('after-attach');
1216 widget.dispose();
1217 });
1218 });
1219
1220 describe('.detach()', () => {
1221 it('should detach a root widget from its host', () => {
1222 let widget = new Widget();
1223 Widget.attach(widget, document.body);
1224 expect(widget.isAttached).to.equal(true);
1225 Widget.detach(widget);
1226 expect(widget.isAttached).to.equal(false);
1227 widget.dispose();
1228 });
1229
1230 it('should throw if the widget is not a root', () => {
1231 let parent = new Widget();
1232 let child = new Widget();
1233 child.parent = parent;
1234 Widget.attach(parent, document.body);
1235 expect(() => {
1236 Widget.detach(child);
1237 }).to.throw(Error);
1238 parent.dispose();
1239 });
1240
1241 it('should throw if the widget is not attached', () => {
1242 let widget = new Widget();
1243 expect(() => {
1244 Widget.detach(widget);
1245 }).to.throw(Error);
1246 });
1247
1248 it('should dispatch a `before-detach` message', () => {
1249 let widget = new LogWidget();
1250 Widget.attach(widget, document.body);
1251 widget.messages = [];
1252 Widget.detach(widget);
1253 expect(widget.messages).to.contain('before-detach');
1254 widget.dispose();
1255 });
1256 });
1257 });
1258 });