Debian Codegraph @

packages/widgets/tests/src/layout.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, every } from '@lumino/algorithm';
13
14 import { Message, MessageLoop } from '@lumino/messaging';
15
16 import { Layout, Widget } from '@lumino/widgets';
17
18 import { LogWidget } from './widget.spec';
19
20 class LogLayout extends Layout {
21 methods: string[] = [];
22
23 widgets = [new LogWidget(), new LogWidget()];
24
25 dispose(): void {
26 while (this.widgets.length !== 0) {
27 this.widgets.pop()!.dispose();
28 }
29 super.dispose();
30 }
31
32 *[Symbol.iterator](): IterableIterator<Widget> {
33 yield* this.widgets;
34 }
35
36 removeWidget(widget: Widget): void {
37 this.methods.push('removeWidget');
38 ArrayExt.removeFirstOf(this.widgets, widget);
39 }
40
41 protected init(): void {
42 this.methods.push('init');
43 super.init();
44 }
45
46 protected onResize(msg: Widget.ResizeMessage): void {
47 super.onResize(msg);
48 this.methods.push('onResize');
49 }
50
51 protected onUpdateRequest(msg: Message): void {
52 super.onUpdateRequest(msg);
53 this.methods.push('onUpdateRequest');
54 }
55
56 protected onAfterAttach(msg: Message): void {
57 super.onAfterAttach(msg);
58 this.methods.push('onAfterAttach');
59 }
60
61 protected onBeforeDetach(msg: Message): void {
62 super.onBeforeDetach(msg);
63 this.methods.push('onBeforeDetach');
64 }
65
66 protected onAfterShow(msg: Message): void {
67 super.onAfterShow(msg);
68 this.methods.push('onAfterShow');
69 }
70
71 protected onBeforeHide(msg: Message): void {
72 super.onBeforeHide(msg);
73 this.methods.push('onBeforeHide');
74 }
75
76 protected onFitRequest(msg: Widget.ChildMessage): void {
77 super.onFitRequest(msg);
78 this.methods.push('onFitRequest');
79 }
80
81 protected onChildShown(msg: Widget.ChildMessage): void {
82 super.onChildShown(msg);
83 this.methods.push('onChildShown');
84 }
85
86 protected onChildHidden(msg: Widget.ChildMessage): void {
87 super.onChildHidden(msg);
88 this.methods.push('onChildHidden');
89 }
90 }
91
92 describe('@lumino/widgets', () => {
93 describe('Layout', () => {
94 describe('[Symbol.iterator]()', () => {
95 it('should create an iterator over the widgets in the layout', () => {
96 let layout = new LogLayout();
97 expect(every(layout, child => child instanceof Widget)).to.equal(true);
98 });
99 });
100
101 describe('#removeWidget()', () => {
102 it("should be invoked when a child widget's `parent` property is set to `null`", () => {
103 let parent = new Widget();
104 let layout = new LogLayout();
105 parent.layout = layout;
106 layout.widgets[0].parent = null;
107 expect(layout.methods).to.contain('removeWidget');
108 });
109 });
110
111 describe('#dispose()', () => {
112 it('should dispose of the resource held by the layout', () => {
113 let widget = new Widget();
114 let layout = new LogLayout();
115 widget.layout = layout;
116 layout.dispose();
117 expect(layout.parent).to.equal(null);
118 expect(every(widget.children(), w => w.isDisposed)).to.equal(true);
119 });
120
121 it('should be called automatically when the parent is disposed', () => {
122 let widget = new Widget();
123 let layout = new LogLayout();
124 widget.layout = layout;
125 widget.dispose();
126 expect(layout.parent).to.equal(null);
127 expect(layout.isDisposed).to.equal(true);
128 });
129 });
130
131 describe('#isDisposed', () => {
132 it('should test whether the layout is disposed', () => {
133 let layout = new LogLayout();
134 expect(layout.isDisposed).to.equal(false);
135 layout.dispose();
136 expect(layout.isDisposed).to.equal(true);
137 });
138 });
139
140 describe('#parent', () => {
141 it('should get the parent widget of the layout', () => {
142 let layout = new LogLayout();
143 let parent = new Widget();
144 parent.layout = layout;
145 expect(layout.parent).to.equal(parent);
146 });
147
148 it('should throw an error if set to `null`', () => {
149 let layout = new LogLayout();
150 let parent = new Widget();
151 parent.layout = layout;
152 expect(() => {
153 layout.parent = null;
154 }).to.throw(Error);
155 });
156
157 it('should throw an error if set to a different value', () => {
158 let layout = new LogLayout();
159 let parent = new Widget();
160 parent.layout = layout;
161 expect(() => {
162 layout.parent = new Widget();
163 }).to.throw(Error);
164 });
165
166 it('should be a no-op if the parent is set to the same value', () => {
167 let layout = new LogLayout();
168 let parent = new Widget();
169 parent.layout = layout;
170 layout.parent = parent;
171 expect(layout.parent).to.equal(parent);
172 });
173 });
174
175 describe('#init()', () => {
176 it('should be invoked when the layout is installed on its parent widget', () => {
177 let widget = new Widget();
178 let layout = new LogLayout();
179 widget.layout = layout;
180 expect(layout.methods).to.contain('init');
181 });
182
183 it('should reparent the child widgets', () => {
184 let widget = new Widget();
185 let layout = new LogLayout();
186 widget.layout = layout;
187 expect(every(layout, child => child.parent === widget)).to.equal(true);
188 });
189 });
190
191 describe('#onResize()', () => {
192 it('should be invoked on a `resize` message', () => {
193 let layout = new LogLayout();
194 let parent = new Widget();
195 parent.layout = layout;
196 MessageLoop.sendMessage(parent, Widget.ResizeMessage.UnknownSize);
197 expect(layout.methods).to.contain('onResize');
198 });
199
200 it('should send a `resize` message to each of the widgets in the layout', () => {
201 let layout = new LogLayout();
202 let parent = new Widget();
203 parent.layout = layout;
204 MessageLoop.sendMessage(parent, Widget.ResizeMessage.UnknownSize);
205 expect(layout.methods).to.contain('onResize');
206 expect(layout.widgets[0].methods).to.contain('onResize');
207 expect(layout.widgets[1].methods).to.contain('onResize');
208 });
209 });
210
211 describe('#onUpdateRequest()', () => {
212 it('should be invoked on an `update-request` message', () => {
213 let layout = new LogLayout();
214 let parent = new Widget();
215 parent.layout = layout;
216 MessageLoop.sendMessage(parent, Widget.Msg.UpdateRequest);
217 expect(layout.methods).to.contain('onUpdateRequest');
218 });
219
220 it('should send a `resize` message to each of the widgets in the layout', () => {
221 let layout = new LogLayout();
222 let parent = new Widget();
223 parent.layout = layout;
224 MessageLoop.sendMessage(parent, Widget.Msg.UpdateRequest);
225 expect(layout.methods).to.contain('onUpdateRequest');
226 expect(layout.widgets[0].methods).to.contain('onResize');
227 expect(layout.widgets[1].methods).to.contain('onResize');
228 });
229 });
230
231 describe('#onAfterAttach()', () => {
232 it('should be invoked on an `after-attach` message', () => {
233 let layout = new LogLayout();
234 let parent = new Widget();
235 parent.layout = layout;
236 MessageLoop.sendMessage(parent, Widget.Msg.AfterAttach);
237 expect(layout.methods).to.contain('onAfterAttach');
238 });
239
240 it('should send an `after-attach` message to each of the widgets in the layout', () => {
241 let layout = new LogLayout();
242 let parent = new Widget();
243 parent.layout = layout;
244 MessageLoop.sendMessage(parent, Widget.Msg.AfterAttach);
245 expect(layout.methods).to.contain('onAfterAttach');
246 expect(layout.widgets[0].methods).to.contain('onAfterAttach');
247 expect(layout.widgets[1].methods).to.contain('onAfterAttach');
248 });
249 });
250
251 describe('#onBeforeDetach()', () => {
252 it('should be invoked on an `before-detach` message', () => {
253 let layout = new LogLayout();
254 let parent = new Widget();
255 parent.layout = layout;
256 MessageLoop.sendMessage(parent, Widget.Msg.BeforeDetach);
257 expect(layout.methods).to.contain('onBeforeDetach');
258 });
259
260 it('should send a `before-detach` message to each of the widgets in the layout', () => {
261 let layout = new LogLayout();
262 let parent = new Widget();
263 parent.layout = layout;
264 MessageLoop.sendMessage(parent, Widget.Msg.BeforeDetach);
265 expect(layout.methods).to.contain('onBeforeDetach');
266 expect(layout.widgets[0].methods).to.contain('onBeforeDetach');
267 expect(layout.widgets[1].methods).to.contain('onBeforeDetach');
268 });
269 });
270
271 describe('#onAfterShow()', () => {
272 it('should be invoked on an `after-show` message', () => {
273 let layout = new LogLayout();
274 let parent = new Widget();
275 parent.layout = layout;
276 MessageLoop.sendMessage(parent, Widget.Msg.AfterShow);
277 expect(layout.methods).to.contain('onAfterShow');
278 });
279
280 it('should send an `after-show` message to non hidden of the widgets in the layout', () => {
281 let layout = new LogLayout();
282 let parent = new Widget();
283 parent.layout = layout;
284 layout.widgets[0].hide();
285 MessageLoop.sendMessage(parent, Widget.Msg.AfterShow);
286 expect(layout.methods).to.contain('onAfterShow');
287 expect(layout.widgets[0].methods).to.not.contain('onAfterShow');
288 expect(layout.widgets[1].methods).to.contain('onAfterShow');
289 });
290 });
291
292 describe('#onBeforeHide()', () => {
293 it('should be invoked on a `before-hide` message', () => {
294 let layout = new LogLayout();
295 let parent = new Widget();
296 parent.layout = layout;
297 MessageLoop.sendMessage(parent, Widget.Msg.BeforeHide);
298 expect(layout.methods).to.contain('onBeforeHide');
299 });
300
301 it('should send a `before-hide` message to non hidden of the widgets in the layout', () => {
302 let layout = new LogLayout();
303 let parent = new Widget();
304 parent.layout = layout;
305 layout.widgets[0].hide();
306 MessageLoop.sendMessage(parent, Widget.Msg.BeforeHide);
307 expect(layout.methods).to.contain('onBeforeHide');
308 expect(layout.widgets[0].methods).to.not.contain('onBeforeHide');
309 expect(layout.widgets[1].methods).to.contain('onBeforeHide');
310 });
311 });
312
313 describe('#onFitRequest()', () => {
314 it('should be invoked on an `fit-request` message', () => {
315 let layout = new LogLayout();
316 let parent = new Widget();
317 parent.layout = layout;
318 MessageLoop.sendMessage(parent, Widget.Msg.FitRequest);
319 expect(layout.methods).to.contain('onFitRequest');
320 });
321 });
322
323 describe('#onChildShown()', () => {
324 it('should be invoked on an `child-shown` message', () => {
325 let layout = new LogLayout();
326 let parent = new Widget();
327 parent.layout = layout;
328 let msg = new Widget.ChildMessage('child-shown', new Widget());
329 MessageLoop.sendMessage(parent, msg);
330 expect(layout.methods).to.contain('onChildShown');
331 });
332 });
333
334 describe('#onChildHidden()', () => {
335 it('should be invoked on an `child-hidden` message', () => {
336 let layout = new LogLayout();
337 let parent = new Widget();
338 parent.layout = layout;
339 let msg = new Widget.ChildMessage('child-hidden', new Widget());
340 MessageLoop.sendMessage(parent, msg);
341 expect(layout.methods).to.contain('onChildHidden');
342 });
343 });
344 });
345 });