Debian Codegraph @

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

raw
1 // Copyright (c) Jupyter Development Team.
2 // Distributed under the terms of the Modified BSD License.
3
4 import {
5 IMessageHandler,
6 IMessageHook,
7 Message,
8 MessageLoop
9 } from '@lumino/messaging';
10 import { DockLayout, TabBar, Widget } from '@lumino/widgets';
11 import { expect } from 'chai';
12
13 const renderer: DockLayout.IRenderer = {
14 createTabBar: function (
15 document?: Document | ShadowRoot | undefined
16 ): TabBar<Widget> {
17 return new TabBar();
18 },
19 createHandle: function (): HTMLDivElement {
20 return document.createElement('div');
21 }
22 };
23
24 class LogDockLayout extends DockLayout {
25 methods: string[] = [];
26
27 protected init(): void {
28 super.init();
29 this.methods.push('init');
30 }
31
32 iter() {
33 throw new Error('Method not implemented.');
34 }
35
36 protected onFitRequest(msg: Message) {
37 super.onFitRequest(msg);
38 this.methods.push('onFitRequest');
39 }
40
41 resetMethods() {
42 this.methods = [];
43 }
44 }
45
46 class LogHook implements IMessageHook {
47 messages: string[] = [];
48
49 messageHook(target: IMessageHandler, msg: Message): boolean {
50 this.messages.push(msg.type);
51 return true;
52 }
53
54 resetMessages() {
55 this.messages = [];
56 }
57 }
58
59 describe('@lumino/widgets', () => {
60 describe('DockLayout', () => {
61 describe('#constructor()', () => {
62 it('should accept a renderer', () => {
63 const layout = new DockLayout({ renderer });
64 expect(layout).to.be.an.instanceOf(DockLayout);
65 });
66
67 it('should have a default spacing value', () => {
68 const layout = new DockLayout({ renderer });
69 expect(layout.spacing).not.to.be.null;
70 });
71
72 it('should accept a spacing value', () => {
73 const layout = new DockLayout({ renderer, spacing: 6 });
74 expect(layout.spacing).equal(6);
75 });
76
77 it('should normalize a spacing value to a whole number', () => {
78 const layout = new DockLayout({ renderer, spacing: 5.5 });
79 expect(layout.spacing).equal(5);
80 });
81
82 it('should have a default hidden mode of display', () => {
83 const layout = new DockLayout({ renderer });
84 expect(layout.hiddenMode).to.equal(Widget.HiddenMode.Display);
85 });
86
87 it('should have a accept hidden mode option', () => {
88 const layout = new DockLayout({
89 renderer,
90 hiddenMode: Widget.HiddenMode.Scale
91 });
92 expect(layout.hiddenMode).to.equal(Widget.HiddenMode.Scale);
93 });
94 });
95
96 describe('#dispose()', () => {
97 it('should dispose of resources held by widget', () => {
98 const layout = new DockLayout({ renderer });
99 layout.addWidget(new Widget());
100
101 layout.dispose();
102 expect(layout.isDisposed).to.equal(true);
103
104 layout.dispose();
105 expect(layout.isDisposed).to.equal(true);
106 });
107 });
108
109 describe('#handles()', () => {
110 it('should return the handles within the layout', () => {
111 const layout = new DockLayout({ renderer });
112 layout.addWidget(new Widget());
113 layout.addWidget(new Widget(), { mode: 'split-bottom' });
114 expect(Array.from(layout.handles())).to.have.lengthOf(2);
115 layout.dispose();
116 });
117 });
118
119 describe('hiddenMode', () => {
120 let layout: DockLayout;
121 let widgets: Widget[] = [];
122
123 beforeEach(() => {
124 layout = new DockLayout({ renderer });
125
126 // Create two stacked widgets
127 widgets.push(new Widget());
128 layout.addWidget(widgets[0]);
129 widgets.push(new Widget());
130 layout.addWidget(widgets[1], { mode: 'tab-after' });
131 });
132
133 afterEach(() => {
134 layout.dispose();
135 });
136
137 it("should be 'display' mode by default", () => {
138 expect(layout.hiddenMode).to.equal(Widget.HiddenMode.Display);
139 });
140
141 it("should switch to 'scale'", () => {
142 widgets[0].hiddenMode = Widget.HiddenMode.Scale;
143
144 layout.hiddenMode = Widget.HiddenMode.Scale;
145
146 expect(widgets[0].hiddenMode).to.equal(Widget.HiddenMode.Scale);
147 expect(widgets[1].hiddenMode).to.equal(Widget.HiddenMode.Scale);
148 });
149
150 it("should switch to 'display'", () => {
151 widgets[0].hiddenMode = Widget.HiddenMode.Scale;
152
153 layout.hiddenMode = Widget.HiddenMode.Scale;
154 layout.hiddenMode = Widget.HiddenMode.Display;
155
156 expect(widgets[0].hiddenMode).to.equal(Widget.HiddenMode.Display);
157 expect(widgets[1].hiddenMode).to.equal(Widget.HiddenMode.Display);
158 });
159
160 it("should not set 'scale' if only one widget", () => {
161 layout.removeWidget(widgets[1]);
162
163 layout.hiddenMode = Widget.HiddenMode.Scale;
164
165 expect(widgets[0].hiddenMode).to.equal(Widget.HiddenMode.Display);
166 });
167 });
168
169 describe('#spacing', () => {
170 it('should get spacing value', () => {
171 const layout = new DockLayout({ renderer });
172 expect(layout.spacing).to.not.be.an('integer');
173 });
174
175 it('should trigger a fit request on change', done => {
176 const layout = new LogDockLayout({ renderer });
177 const parent = new Widget();
178 parent.layout = layout;
179 const hook = new LogHook();
180 MessageLoop.installMessageHook(parent, hook);
181 requestAnimationFrame(() => {
182 hook.resetMessages();
183 layout.resetMethods();
184 layout.spacing = layout.spacing + 1;
185 requestAnimationFrame(() => {
186 expect(hook.messages).to.contain('fit-request');
187 expect(layout.methods).to.contain('onFitRequest');
188 done();
189 });
190 });
191 });
192
193 // This should not be failing, but it does
194 it('should be a no-op if value does not change', done => {
195 const layout = new LogDockLayout({ renderer });
196 const parent = new Widget();
197 parent.layout = layout;
198 const hook = new LogHook();
199 MessageLoop.installMessageHook(parent, hook);
200 requestAnimationFrame(() => {
201 hook.resetMessages();
202 layout.resetMethods();
203 const spacing = layout.spacing;
204 layout.spacing = spacing;
205 requestAnimationFrame(() => {
206 expect(hook.messages).to.not.contain('fit-request');
207 expect(layout.methods).to.not.contain('onFitRequest');
208 done();
209 });
210 });
211 });
212 });
213
214 describe('#isEmpty()', () => {
215 it('should return true for layout with no widgets', () => {
216 const layout = new DockLayout({ renderer });
217 expect(layout.isEmpty).to.equal(true);
218 });
219
220 it('should return false for layout with widgets', () => {
221 const layout = new DockLayout({ renderer });
222 layout.addWidget(new Widget());
223 expect(layout.isEmpty).to.equal(false);
224 });
225 });
226
227 // TODO: More tests to add
228 // describe('#widgets()', () => {
229 // it.skip('should have some tests');
230 // });
231 // describe('#selectedWidgets()', () => {
232 // it.skip('should have some tests');
233 // });
234 // describe('#tabBars()', () => {
235 // it.skip('should have some tests');
236 // });
237 // describe('#moveHandle()', () => {
238 // it.skip('should have some tests');
239 // });
240 // describe('#saveLayout()', () => {
241 // it.skip('should have some tests');
242 // });
243 // describe('#restoreLayout()', () => {
244 // it.skip('should have some tests');
245 // });
246 describe('#addWidget()', () => {
247 type Mode = DockLayout.InsertMode;
248 it('should add widgets', () => {
249 const layout = new DockLayout({ renderer });
250 const widget = new Widget();
251 layout.addWidget(widget);
252 expect(layout['_root'].tabBar).to.exist;
253 expect(layout['_root'].tabBar.titles[0].owner).to.equal(widget);
254 });
255 it('should add splits', () => {
256 for (let i = 0, d = ['right', 'left', 'bottom', 'top']; i < 4; ++i) {
257 const layout = new DockLayout({ renderer });
258 const w1 = new Widget();
259 const w2 = new Widget();
260 layout.addWidget(w1);
261 layout.addWidget(w2, { ref: w1, mode: <Mode>`split-${d[i]}` });
262 expect(layout['_root'].children).to.exist;
263 expect(layout['_root'].children.length).to.equal(2);
264 expect(
265 layout['_root'].children[0 + (i % 2)].tabBar.titles[0].owner
266 ).to.equal(w1);
267 expect(
268 layout['_root'].children[1 - (i % 2)].tabBar.titles[0].owner
269 ).to.equal(w2);
270 }
271 });
272 it('should merge splits', () => {
273 for (let i = 0, d = ['right', 'left', 'bottom', 'top']; i < 4; ++i) {
274 const layout = new DockLayout({ renderer });
275 const w1 = new Widget();
276 const w2 = new Widget();
277 const w3 = new Widget();
278 layout.addWidget(w1);
279 layout.addWidget(w2, { ref: w1, mode: <Mode>`merge-${d[i]}` });
280 layout.addWidget(w3, { ref: w1, mode: <Mode>`merge-${d[i]}` });
281 expect(layout['_root'].children).to.exist;
282 expect(layout['_root'].children.length).to.equal(2);
283 expect(
284 layout['_root'].children[0 + (i % 2)].tabBar.titles[0].owner
285 ).to.equal(w1);
286 expect(
287 layout['_root'].children[1 - (i % 2)].tabBar.titles[0].owner
288 ).to.equal(w2);
289 expect(
290 layout['_root'].children[1 - (i % 2)].tabBar.titles[1].owner
291 ).to.equal(w3);
292 }
293 });
294 });
295 // describe('#removeWidget()', () => {
296 // it.skip('should have some tests');
297 // });
298 // describe('#hitTestTabAreas()', () => {
299 // it.skip('should have some tests');
300 // });
301 // describe('#attachWidget()', () => {
302 // it.skip('should have some tests');
303 // });
304 // describe('#detachWidget()', () => {
305 // it.skip('should have some tests');
306 // });
307 });
308 });