1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
| import { DEFAULT_PANEL_HEIGHT, DEFAULT_PANEL_WIDTH } from "../../constants";
| import { JoinOrder, type PanelBBox, Side } from "../types";
| import {
| checkCollapsedPanelsHaveData,
| determineDroppableArea,
| determineLeftOrRight,
| findPanelViewByName,
| findZIndices,
| getSnappedHeights,
| joinPanelColumns,
| redistributeHeights,
| setActive,
| setActiveDefaults,
| splitPanelColumns,
| stateAddedTab,
| stateRemovedTab,
| stateRemovePanelEmptyViews,
| } from "../utils";
|
| const dummyPanels: Record<string, PanelBBox> = {
| p1: {
| top: 0,
| left: 0,
| order: 0,
| relativeLeft: 0,
| relativeTop: 0,
| zIndex: 1,
| width: 500,
| height: 500,
| visible: true,
| detached: false,
| alignment: Side.left,
| maxHeight: 800,
| panelViews: [
| { title: "Tab 1", name: "Tab1", component: () => null, active: true },
| { title: "Tab 2", name: "Tab2", component: () => null, active: false },
| { title: "Tab 3", name: "Tab3", component: () => null, active: false },
| ],
| },
| p2: {
| top: 0,
| left: 0,
| order: 0,
| relativeLeft: 0,
| relativeTop: 0,
| zIndex: 1,
| width: 500,
| height: 500,
| visible: true,
| detached: true,
| alignment: Side.right,
| maxHeight: 800,
| panelViews: [{ title: "Tab 4", name: "Tab4", component: () => null, active: true }],
| },
| };
|
| describe("determineLeftOrRight", () => {
| it("returns DroppableSide.right when the event x position is greater than half the droppable element width", () => {
| const event = {
| pageX: 500,
| target: {
| clientWidth: 800,
| getBoundingClientRect: () => ({ left: 0 }),
| },
| };
|
| expect(determineLeftOrRight(event)).toBe(Side.right);
| });
|
| it("returns DroppableSide.left when the event x position is less than half the droppable element width", () => {
| const event = {
| pageX: 200,
| target: {
| clientWidth: 800,
| getBoundingClientRect: () => ({ left: 0 }),
| },
| };
|
| expect(determineLeftOrRight(event)).toBe(Side.left);
| });
| });
|
| describe("determineDroppableArea", () => {
| it('returns true when the droppingElement id includes "droppable"', () => {
| const droppingElement = document.createElement("div");
|
| droppingElement.id = "droppable-123";
|
| expect(determineDroppableArea(droppingElement)).toBe(true);
| });
|
| it('returns false when the droppingElement id does not include "droppable"', () => {
| const droppingElement = document.createElement("div");
|
| droppingElement.id = "no";
|
| expect(determineDroppableArea(droppingElement)).toBe(false);
| });
| });
|
| describe("stateRemovedTab", () => {
| const state = { ...dummyPanels };
|
| it("should remove the specified tab from the specified panel", () => {
| const panelName = "p1";
| const tabToRemove = 1;
|
| const expectedPanelViews = [
| { title: "Tab 1", name: "Tab1", component: () => null, active: true },
| { title: "Tab 3", name: "Tab3", component: () => null, active: false },
| ];
|
| const expectedState = {
| ...state,
| [panelName]: {
| ...state[panelName],
| panelViews: expectedPanelViews,
| },
| };
|
| const newState = stateRemovedTab(state, panelName, tabToRemove);
|
| expect(JSON.stringify(newState)).toEqual(JSON.stringify(expectedState));
| });
|
| it("should return the same state if the specified panel or tab does not exist", () => {
| const nonExistentPanelName = "nonexistent";
| const nonExistentTab = 5;
| const newState = stateRemovedTab(state, nonExistentPanelName, nonExistentTab);
|
| expect(newState).toEqual(state);
| });
| });
|
| describe("setActive", () => {
| const panelName = "p1";
| const tabIndex = 1;
|
| it("should set the correct tab as active", () => {
| const expectedState = [
| {
| title: "Tab 1",
| name: "Tab1",
| component: () => null,
| active: false,
| },
| {
| title: "Tab 2",
| name: "Tab2",
| component: () => null,
| active: true,
| },
| {
| title: "Tab 3",
| name: "Tab3",
| component: () => null,
| active: false,
| },
| ];
|
| const stateBefore = { ...dummyPanels };
| const newState = setActive(stateBefore, panelName, tabIndex);
|
| expect(JSON.stringify(newState[panelName].panelViews)).toEqual(JSON.stringify(expectedState));
| });
|
| it("should not modify the original state object", () => {
| const stateBefore = { ...dummyPanels };
|
| setActive(dummyPanels, panelName, tabIndex);
| expect(dummyPanels).toEqual(stateBefore);
| });
|
| it("should return a new state object", () => {
| const newState = setActive(dummyPanels, panelName, tabIndex);
|
| expect(newState).not.toBe(dummyPanels);
| });
| });
|
| describe("setActiveDefaults", () => {
| it("sets the first tab as active if no tabs are currently active", () => {
| const state = {
| panel1: {
| panelViews: [
| { title: "Tab 1", name: "Tab1", component: () => null, active: false },
| { title: "Tab 2", name: "Tab2", component: () => null, active: false },
| { title: "Tab 3", name: "Tab3", component: () => null, active: false },
| ],
| },
| panel2: {
| panelViews: [
| { title: "Tab 4", name: "Tab4", component: () => null, active: false },
| { title: "Tab 5", name: "Tab5", component: () => null, active: false },
| ],
| },
| };
| const newState = setActiveDefaults(state);
|
| expect(newState.panel1.panelViews[0].active).toBe(true);
| expect(newState.panel2.panelViews[0].active).toBe(true);
| });
|
| it("does not change active tabs if there are already active tabs", () => {
| const state = {
| panel1: {
| panelViews: [
| { title: "Tab 1", name: "Tab1", component: () => null, active: false },
| { title: "Tab 2", name: "Tab2", component: () => null, active: true },
| { title: "Tab 3", name: "Tab3", component: () => null, active: false },
| ],
| },
| panel2: {
| panelViews: [
| { title: "Tab 4", name: "Tab4", component: () => null, active: true },
| { title: "Tab 5", name: "Tab5", component: () => null, active: false },
| ],
| },
| };
|
| const newState = setActiveDefaults(state);
| const p1t2Active = newState.panel1.panelViews[1].active;
| const p2t1Active = newState.panel2.panelViews[0].active;
|
| expect(p1t2Active).toBe(true);
| expect(p2t1Active).toBe(true);
| });
| });
|
| describe("stateAddedTab", () => {
| const panelName = "panel1";
| const initialPanelViews = [
| { title: "Tab 1", name: "Tab1", component: () => null, active: true },
| { title: "Tab 2", name: "Tab2", component: () => null, active: false },
| ];
|
| const state: Record<string, PanelBBox> = {
| [panelName]: { panelViews: initialPanelViews } as unknown as PanelBBox,
| };
|
| it("adds a new tab to the receiving panel on the right", () => {
| const movingTabData = { title: "Tab 3", name: "Tab3", component: () => null, active: false };
| const receivingTab = 1;
| const receivingPanel = panelName;
| const dropSide = Side.right;
|
| const expectedPanelViews = [
| { title: "Tab 1", name: "Tab1", component: () => null, active: false },
| { title: "Tab 3", name: "Tab3", component: () => null, active: false },
| { title: "Tab 2", name: "Tab2", component: () => null, active: false },
| ];
|
| const expectedState = {
| ...state,
| [panelName]: {
| panelViews: expectedPanelViews,
| },
| };
|
| const newState = stateAddedTab(state, panelName, receivingPanel, movingTabData, receivingTab, dropSide);
|
| expect(JSON.stringify(newState)).toEqual(JSON.stringify(expectedState));
| });
|
| it("adds a new tab to the receiving panel on the left", () => {
| const movingTabData = { title: "Tab 3", name: "Tab3", component: () => null, active: false };
| const receivingTab = 1;
| const receivingPanel = panelName;
| const dropSide = Side.left;
|
| const expectedPanelViews = [
| { title: "Tab 3", name: "Tab3", component: () => null, active: false },
| { title: "Tab 1", name: "Tab1", component: () => null, active: false },
| { title: "Tab 3", name: "Tab3", component: () => null, active: false },
| { title: "Tab 2", name: "Tab2", component: () => null, active: false },
| ];
|
| const expectedState = {
| ...state,
| [panelName]: {
| panelViews: expectedPanelViews,
| },
| };
|
| const newState = stateAddedTab(state, panelName, receivingPanel, movingTabData, receivingTab, dropSide);
|
| expect(JSON.stringify(newState)).toEqual(JSON.stringify(expectedState));
| });
| });
|
| describe("stateRemovePanelEmptyViews", () => {
| const panelName = "panel1";
| const initialPanelViews = [{ title: "Tab 1", name: "Tab1", component: () => null, active: true }];
|
| const state: Record<string, PanelBBox> = {
| [panelName]: { panelViews: initialPanelViews } as unknown as PanelBBox,
| panel2: { panelViews: [] } as unknown as PanelBBox,
| };
|
| it("removes empty panel views from the state", () => {
| const expectedState = {
| [panelName]: { panelViews: initialPanelViews } as unknown as PanelBBox,
| };
|
| const newState = stateRemovePanelEmptyViews(state);
|
| expect(newState).toEqual(expectedState);
| });
| });
|
| describe("splitPanelColumns", () => {
| const panel1 = "panel1";
| const panel2 = "panel2";
| const panel3 = "panel3";
| const totalHeight = 1000;
| const panelAttributes = {
| panelViews: [{ name: "view1", component: () => null }],
| order: 0,
| width: DEFAULT_PANEL_WIDTH,
| height: DEFAULT_PANEL_HEIGHT,
| alignment: Side.left,
| };
| const initialPanels = {
| panel1: { ...panelAttributes },
| panel2: { ...panelAttributes, alignment: Side.right },
| panel3: { ...panelAttributes, alignment: Side.left },
| };
|
| it("should split the columns when removing a panel from a column", () => {
| const removingKey = panel1;
| const expectedPanel1Attributes = {
| ...panelAttributes,
| order: 0,
| alignment: Side.left,
| detached: true,
| panelViews: [{ name: "view1", component: () => null }],
| };
| const expectedPanel2Attributes = {
| ...panelAttributes,
| order: 0,
| alignment: Side.right,
| };
| const expectedPanel3Attributes = {
| ...panelAttributes,
| order: 1,
| alignment: Side.left,
| };
| const expectedState = {
| [panel1]: { ...expectedPanel1Attributes },
| [panel2]: { ...expectedPanel2Attributes },
| [panel3]: { ...expectedPanel3Attributes },
| };
|
| const newState = splitPanelColumns(initialPanels, removingKey, totalHeight);
|
| expect(JSON.stringify(newState[removingKey])).toEqual(JSON.stringify(expectedState[removingKey]));
| });
|
| it("should split the columns when removing a panel from a column with multiple panels", () => {
| const panel1Attributes = {
| ...panelAttributes,
| panelViews: [{ name: "view1", component: () => null }],
| };
| const panel2Attributes = {
| ...panelAttributes,
| alignment: Side.left,
| panelViews: [{ name: "view2", component: () => null }],
| };
| const panel3Attributes = {
| ...panelAttributes,
| alignment: Side.right,
| panelViews: [{ name: "view3", component: () => null }],
| };
| const removingKey = panel1;
| const expectedPanel1Attributes = {
| ...panelAttributes,
| order: 0,
| alignment: Side.left,
| detached: true,
| };
| const expectedPanel2Attributes = {
| ...panelAttributes,
| order: 1,
| alignment: Side.left,
| panelViews: [{ name: "view2", component: () => null }],
| };
| const expectedPanel3Attributes = {
| ...panelAttributes,
| order: 0,
| alignment: Side.right,
| panelViews: [{ name: "view3", component: () => null }],
| };
| const expectedState = {
| [panel1]: { ...expectedPanel1Attributes },
| [panel2]: { ...expectedPanel2Attributes },
| [panel3]: { ...expectedPanel3Attributes },
| };
|
| const newState = splitPanelColumns(
| { panel1: panel1Attributes, panel2: panel2Attributes, panel3: panel3Attributes },
| removingKey,
| totalHeight,
| );
|
| expect(JSON.stringify(newState)).toEqual(JSON.stringify(expectedState));
| });
| });
|
| describe("joinPanelColumns", () => {
| const panel1: PanelBBox = {
| width: 200,
| height: 300,
| top: 0,
| left: 0,
| order: 0,
| detached: false,
| alignment: Side.left,
| panelViews: [],
| };
| const panel2: PanelBBox = {
| width: 300,
| height: 200,
| top: 0,
| left: 200,
| order: 1,
| detached: false,
| alignment: Side.left,
| panelViews: [],
| };
| const panel3: PanelBBox = {
| width: 250,
| height: 400,
| top: 0,
| left: 500,
| order: 2,
| detached: false,
| alignment: Side.right,
| panelViews: [],
| };
| const panel4: PanelBBox = {
| width: 250,
| height: 400,
| top: 0,
| left: 500,
| order: 2,
| detached: false,
| alignment: Side.right,
| panelViews: [],
| };
| const state = {
| panel1,
| panel2,
| panel3,
| panel4,
| };
|
| it("should join two columns on the left", () => {
| const result = joinPanelColumns(state, "panel4", Side.left, 300, 600);
|
| expect(result).toEqual({
| panel1,
| panel2,
| panel3,
| panel4: {
| width: 300,
| zIndex: 10,
| height: 400,
| top: 0,
| left: 500,
| order: 3,
| detached: false,
| alignment: "left",
| panelViews: [],
| },
| });
| });
|
| it("should join two columns on the right", () => {
| const result = joinPanelColumns(state, "panel4", Side.right, 400, 600);
|
| expect(result).toEqual({
| panel1,
| panel2,
| panel3,
| panel4: {
| width: 250,
| zIndex: 10,
| height: 400,
| top: 0,
| left: 500,
| order: 2,
| detached: false,
| alignment: "right",
| panelViews: [],
| },
| });
| });
|
| it("should adjust the order when joining two columns", () => {
| const result = joinPanelColumns(state, "panel4", Side.left, 300, 600, JoinOrder.top);
|
| expect(result.panel1.order).toBe(1);
| expect(result.panel2.order).toBe(2);
| expect(result.panel3.order).toBe(0);
| expect(result.panel4.order).toBe(0);
| });
| });
|
| describe("redistributeHeights", () => {
| const state = {
| panel1: {
| detached: false,
| alignment: "left",
| height: 100,
| visible: true,
| },
| panel2: {
| detached: false,
| alignment: "left",
| height: 100,
| visible: true,
| },
| panel3: {
| detached: false,
| alignment: "left",
| height: 100,
| visible: true,
| },
| panel4: {
| detached: false,
| alignment: "left",
| height: 100,
| visible: true,
| },
| panel5: {
| detached: false,
| alignment: "left",
| height: 100,
| visible: false,
| },
| };
| const totalHeight = 500;
| const alignment = "left";
| const result = redistributeHeights(state, totalHeight, alignment);
|
| it("should return a new state object", () => {
| expect(result).not.toBe(state);
| });
|
| it("should distribute the total height among the visible panels", () => {
| expect(result.panel1.height).toBeCloseTo(119);
| expect(result.panel2.height).toBeCloseTo(119);
| expect(result.panel3.height).toBeCloseTo(119);
| expect(result.panel4.height).toBeCloseTo(119);
| });
|
| it("should not change the height of collapsed panels", () => {
| expect(result.panel5.height).toBe(100);
| });
| });
|
| describe("getSnappedHeights", () => {
| const state = {
| panel1: { height: 100, top: 0, visible: true },
| panel2: { height: 200, top: 100, visible: true },
| panel3: { height: 50, top: 300, visible: true },
| panel4: { height: 30, top: 350, visible: false },
| };
|
| it("should return a new state object with updated panel heights and top positions", () => {
| const totalHeight = 500;
| const expectedState = {
| panel1: { height: 100, top: 0, visible: true },
| panel2: { height: 200, top: 100, visible: true },
| panel3: { height: 50, top: 300, visible: true },
| panel4: { height: 30, top: 350, visible: false },
| };
|
| expect(getSnappedHeights(state, totalHeight)).toEqual(expectedState);
| });
|
| it("should handle collapsed panels", () => {
| const totalHeight = 300;
| const expectedState = {
| panel1: { height: 100, top: 0, visible: true },
| panel2: { height: 200, top: 100, visible: true },
| panel3: { height: 50, top: 300, visible: true },
| panel4: { height: 30, top: 350, visible: false },
| };
|
| expect(getSnappedHeights(state, totalHeight)).toEqual(expectedState);
| });
|
| it("should handle an empty state object", () => {
| const totalHeight = 500;
| const expectedState = {};
|
| expect(getSnappedHeights({}, totalHeight)).toEqual(expectedState);
| });
| });
|
| describe("findZIndices", () => {
| const panel1 = {
| panelViews: [{ title: "Tab 1", name: "Tab1", component: () => null, active: true }],
| detached: true,
| zIndex: 5,
| };
| const panel2 = {
| panelViews: [{ title: "Tab 2", name: "Tab2", component: () => null, active: false }],
| detached: false,
| zIndex: 8,
| };
| const panel3 = {
| panelViews: [{ title: "Tab 3", name: "Tab3", component: () => null, active: false }],
| detached: true,
| zIndex: 7,
| };
| const state = {
| panel1,
| panel2,
| panel3,
| };
|
| it("should correctly update z-indices for attached and detached panels", () => {
| const expectedState = {
| panel1: { ...panel1, zIndex: 14 },
| panel2: { ...panel2, zIndex: 10 },
| panel3: { ...panel3, zIndex: 13 },
| };
| const focusedKey = "panel1";
| const newState = findZIndices(state, focusedKey);
|
| expect(newState).toEqual(expectedState);
| });
|
| it("should correctly update z-index for focused detached panel", () => {
| const panel4 = {
| panelViews: [{ title: "Tab 4", name: "Tab4", component: () => null, active: true }],
| detached: true,
| zIndex: 6,
| };
| const newState = {
| ...state,
| panel4,
| };
| const expectedState = {
| ...newState,
| panel4: { ...panel4, zIndex: 15 },
| };
| const focusedKey = "panel4";
| const result = findZIndices(newState, focusedKey);
|
| expect(result).toEqual(expectedState);
| });
| });
|
| describe("findPanelViewByName", () => {
| const state = {
| "view1-view2-view3": {
| panelViews: [{ name: "view1" }, { name: "view2" }, { name: "view3" }],
| },
| "view4-view5": {
| panelViews: [{ name: "View 4" }, { name: "View 5" }],
| },
| };
|
| it("should return the correct panel view when it exists", () => {
| const name = "view2";
| const expected = {
| panelName: "view1-view2-view3",
| tab: { name: "view2" },
| panelViewIndex: 1,
| };
|
| const result = findPanelViewByName(state, name);
|
| expect(result).toEqual(expected);
| });
|
| it("should return undefined when the panel view does not exist", () => {
| const name = "Non-existent View";
|
| const result = findPanelViewByName(state, name);
|
| expect(result).toBeUndefined();
| });
|
| it("should handle an empty state object", () => {
| const name = "View";
|
| const result = findPanelViewByName({}, name);
|
| expect(result).toBeUndefined();
| });
|
| it("should handle an empty panelViews array", () => {
| const stateWithEmptyPanel = {
| panel1: {
| panelViews: [],
| },
| };
| const name = "View";
|
| const result = findPanelViewByName(stateWithEmptyPanel, name);
|
| expect(result).toBeUndefined();
| });
|
| it("should handle a state object with no matching panel views", () => {
| const name = "Non-existent View";
|
| const result = findPanelViewByName(state, name);
|
| expect(result).toBeUndefined();
| });
| });
|
| describe("checkCollapsedPanelsHaveData", () => {
| const collapsedSide = {
| left: true,
| right: false,
| };
|
| const panelData = {
| panel1: { alignment: "left", detached: false },
| panel2: { alignment: "right", detached: false },
| panel3: { alignment: "top", detached: true },
| panel4: { alignment: "bottom", detached: false },
| };
|
| it("should update collapsedSide correctly when there is data in collapsed panels", () => {
| const expected = {
| left: true,
| right: false,
| };
|
| const result = checkCollapsedPanelsHaveData(collapsedSide, panelData);
|
| expect(result).toEqual(expected);
| });
|
| it("should not update collapsedSide when there is no data in collapsed panels", () => {
| const collapsedSideWithNoData = {
| left: true,
| right: true,
| };
| const expected = { ...collapsedSideWithNoData };
|
| const result = checkCollapsedPanelsHaveData(collapsedSideWithNoData, panelData);
|
| expect(result).toEqual(expected);
| });
| });
|
|