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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
// @flow
/* eslint-env jest */

import React from "react";
import _ from "lodash";
import TestUtils from "react-dom/test-utils";
import ReactGridLayout from "../../lib/ReactGridLayout";
import { calcGridItemPosition } from "../../lib/calculateUtils";
import GridItem from "../../lib/GridItem";
import ResponsiveReactGridLayout from "../../lib/ResponsiveReactGridLayout";
import BasicLayout from "../examples/01-basic";
import ShowcaseLayout from "../examples/00-showcase";
import DroppableLayout from "../examples/15-drag-from-outside";
import ResizableLayout from "../examples/20-resizable-handles";
import deepFreeze from "../util/deepFreeze";
import { mount } from "enzyme";
import { IsBounded } from "../examples/test_demo.jsx";

function mouseMove(x, y, node) {
  const doc = node ? node.ownerDocument : document;
  const evt = doc.createEvent("MouseEvents");
  // $FlowIgnore
  evt.initMouseEvent(
    "mousemove",
    true,
    true,
    window,
    0,
    0,
    0,
    x,
    y,
    false,
    false,
    false,
    false,
    0,
    null
  );
  doc.dispatchEvent(evt);
  return evt;
}
function simulateMovementFromTo(drag, fromX, fromY, toX, toY) {
  TestUtils.Simulate.mouseDown(drag, {
    clientX: fromX,
    clientY: fromY
  });
  mouseMove(toX, toY, drag);
  TestUtils.Simulate.mouseUp(drag);
}

describe("Lifecycle tests", function () {
  // Example layouts use randomness
  let randIdx = 0;
  beforeAll(() => {
    const randArr = [0.001, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.999];
    jest.spyOn(global.Math, "random").mockImplementation(() => {
      randIdx = (randIdx + 1) % randArr.length;
      return randArr[randIdx];
    });
  });

  beforeEach(() => {
    randIdx = 0;
  });

  afterAll(() => {
    global.Math.random.mockRestore();
  });

  describe("<GridItem >", () => {
    const mockProps = {
      children: <div>test child</div>,
      cols: 12,
      containerWidth: 1200,
      rowHeight: 300,
      margin: [0, 0],
      maxRows: 4,
      containerPadding: [0, 0],
      i: "0",
      // These are all in grid units
      x: 0,
      y: 0,
      w: 100,
      h: 100,
      isDraggable: false,
      isResizable: false,
      isBounded: false,
      useCSSTransforms: false
    };
    it("Basic Render", () => {
      const wrapper = mount(<GridItem {...mockProps} />);
      expect(wrapper).toMatchSnapshot();
    });

    describe("optional min/max dimension props log err", () => {
      describe("minW", () => {
        const mockError = jest
          .spyOn(console, "error")
          .mockImplementation(() => {});
        afterEach(() => {
          jest.clearAllMocks();
        });

        it("2x when string, not number", () => {
          // $FlowIgnore
          mount(<GridItem {...mockProps} minW={"apple"} />);
          expect(mockError).toHaveBeenCalledTimes(2);
        });
        it("1 err when larger than w prop", () => {
          mount(<GridItem {...mockProps} minW={400} />);
          expect(mockError).toHaveBeenCalledTimes(1);
        });
      });

      describe("maxW", () => {
        const mockError = jest
          .spyOn(console, "error")
          .mockImplementation(() => {});
        afterEach(() => {
          jest.clearAllMocks();
        });

        it("1x when string, not number", () => {
          // $FlowIgnore
          mount(<GridItem {...mockProps} maxW={"apple"} />);
          expect(mockError).toHaveBeenCalledTimes(1);
        });
        it("1x err when smaller than w prop", () => {
          mount(<GridItem {...mockProps} w={4} maxW={2} />);
          expect(mockError).toHaveBeenCalledTimes(1);
        });
      });

      describe("minH", () => {
        const mockError = jest
          .spyOn(console, "error")
          .mockImplementation(() => {});
        afterEach(() => {
          jest.clearAllMocks();
        });

        it("2x when string, not number", () => {
          // $FlowIgnore
          mount(<GridItem {...mockProps} minH={"apple"} />);
          expect(mockError).toHaveBeenCalledTimes(2);
        });
        it("1x when larger than h prop", () => {
          mount(<GridItem {...mockProps} minH={200} />);
          expect(mockError).toHaveBeenCalledTimes(1);
        });
      });

      describe("maxH", () => {
        const mockError = jest
          .spyOn(console, "error")
          .mockImplementation(() => {});
        afterEach(() => {
          jest.clearAllMocks();
        });

        it("1x when string, not number", () => {
          // $FlowIgnore
          mount(<GridItem {...mockProps} maxH={"apple"} />);
          expect(mockError).toHaveBeenCalledTimes(1);
        });
        it("1x when smaller than h prop", () => {
          mount(<GridItem {...mockProps} h={3} maxH={2} />);
          expect(mockError).toHaveBeenCalledTimes(1);
        });
      });
    });

    describe("onDrag", () => {
      it("calls onDragStart prop when droppingPosition prop has expected content", () => {
        const mockFn = jest.fn();

        mount(
          <GridItem
            {...mockProps}
            // $FlowIgnore
            droppingPosition={{ left: 1, top: 1, e: {} }}
            onDragStart={mockFn}
          />
        );
        expect(mockFn).toHaveBeenCalledTimes(1);
      });

      it("throws err when calling onDrag without state set to dragging ", () => {
        const componentInstance = mount(
          <GridItem {...mockProps} onDrag={() => {}} />
        ).instance();

        expect(() => {
          // $FlowIgnore
          componentInstance.onDrag({}, {});
        }).toThrow("onDrag called before onDragStart.");
      });

      it("calls onDragStart prop callback fn", () => {
        const mockFn = jest.fn();

        const componentInstance = mount(
          <GridItem
            {...mockProps}
            // $FlowIgnore
            droppingPosition={{ left: 1, top: 1, e: {} }}
            onDragStart={mockFn}
          />
        ).instance();
        // $FlowIgnore
        componentInstance.onDrag({}, () => {});
        expect(mockFn).toHaveBeenCalledTimes(1);
      });

      it("calls onDrag prop callback fn", () => {
        const mockOnDragStartCallback = jest.fn();
        const mockOnDrag = jest.fn();
        const renderedItem = mount(
          <GridItem
            {...mockProps}
            // $FlowIgnore
            isDraggable={true}
            isBounded={true}
            onDragStart={mockOnDragStartCallback}
            onDrag={mockOnDrag}
          />
        );
        TestUtils.act(() => {
          renderedItem.setState({ dragging: true });
          renderedItem.setProps({
            droppingPosition: { left: 700, top: 300, e: {} }
          });
        });
        expect(mockOnDrag).toHaveBeenCalledTimes(1);
      });
    });
  });

  describe("<ReactGridLayout>", function () {
    it("Basic Render", async function () {
      const wrapper = mount(<BasicLayout />);
      expect(wrapper).toMatchSnapshot();
    });

    describe("data-grid", () => {
      it("Creates layout based on properties", async function () {
        const wrapper = mount(
          <ReactGridLayout
            className="layout"
            cols={12}
            rowHeight={30}
            width={1200}
          >
            <div key="a" data-grid={{ x: 0, y: 0, w: 1, h: 2, static: true }}>
              a
            </div>
            <div
              key="b"
              data-grid={{ x: 1, y: 0, w: 3, h: 2, minW: 2, maxW: 4 }}
            >
              b
            </div>
            <div key="c" data-grid={{ x: 4, y: 0, w: 1, h: 2 }}>
              c
            </div>
          </ReactGridLayout>
        );
        expect(wrapper).toMatchSnapshot();
        expect(wrapper.state().layout).toMatchObject([
          {
            h: 2,
            i: "a",
            static: true,
            w: 1,
            x: 0,
            y: 0
          },
          {
            h: 2,
            i: "b",
            static: false,
            w: 3,
            x: 1,
            y: 0
          },
          {
            h: 2,
            i: "c",
            static: false,
            w: 1,
            x: 4,
            y: 0
          }
        ]);
      });

      it("Null items in list", async function () {
        const wrapper = mount(
          <ReactGridLayout
            className="layout"
            cols={12}
            rowHeight={30}
            width={1200}
            // $FlowIgnore
          >
            <div key="a" data-grid={{ x: 0, y: 0, w: 1, h: 2, static: true }}>
              a
            </div>
            {false}
            {null}
            <div key="c" data-grid={{ x: 4, y: 0, w: 1, h: 2 }}>
              c
            </div>
          </ReactGridLayout>
        );

        expect(wrapper).toMatchSnapshot();
        expect(wrapper.state().layout).toHaveLength(2); // Only two truthy items
      });
    });

    describe("WidthProvider", () => {
      it("Renders with WidthProvider", async function () {
        const wrapper = mount(<BasicLayout measureBeforeMount={false} />);
        expect(wrapper).toMatchSnapshot();

        const widthProviderWrapper = wrapper.childAt(0);
        expect(widthProviderWrapper.name()).toEqual("WidthProvider");
        expect(widthProviderWrapper.childAt(0).name()).toEqual(
          "ReactGridLayout"
        );

        expect(widthProviderWrapper.state().width).toEqual(1280); // default
      });

      // FIXME: This doesn't work since we rewrote to use <ResizeObserver>
      // See https://github.com/react-grid-layout/react-grid-layout/pull/1839
      // eslint-disable-next-line no-undef
      xit("Renders with WidthProvider measureBeforeMount", async function () {
        const wrapper = mount(<BasicLayout measureBeforeMount={true} />);
        expect(wrapper).toMatchSnapshot();

        const widthProviderWrapper = wrapper.childAt(0);
        expect(widthProviderWrapper.name()).toEqual("WidthProvider");
        // Renders a div first
        expect(widthProviderWrapper.childAt(0).name()).toEqual("div");

        // Mock offsetWidth to return 500 and fire a resize
        const node = wrapper.getDOMNode();
        Object.defineProperty(node, "offsetWidth", {
          get: jest.fn(() => 500)
        });
        global.dispatchEvent(new Event("resize"));

        wrapper.setProps({}); // force a rerender synchronously

        // Should have removed the div, now has the RGL
        expect(wrapper.childAt(0).childAt(0).name()).toEqual("ReactGridLayout");
        expect(wrapper.childAt(0).state().width).toEqual(500);
      });

      // FIXME: This doesn't work since we rewrote to use <ResizeObserver>
      // See https://github.com/react-grid-layout/react-grid-layout/pull/1839
      // eslint-disable-next-line no-undef
      xit("WidthProvider responds to window resize events", async function () {
        const wrapper = mount(<BasicLayout />);
        const widthProviderWrapper = wrapper.childAt(0);

        // Original width
        expect(widthProviderWrapper.state().width).toEqual(1280);

        // Mock offsetWidth to return 500
        const node = wrapper.getDOMNode();
        Object.defineProperty(node, "offsetWidth", {
          get: jest.fn(() => 500)
        });

        // Trigger the window resize event.
        global.dispatchEvent(new Event("resize"));

        // State should now be 500
        expect(widthProviderWrapper.state().width).toEqual(500);
      });
    });

    describe("Droppability", function () {
      function dragDroppableTo(wrapper, x, y) {
        const gridLayout = wrapper.find("ReactGridLayout");
        const droppable = wrapper.find(".droppable-element");

        TestUtils.Simulate.dragOver(gridLayout.getDOMNode(), {
          currentTarget: {
            getBoundingClientRect: () => ({ left: 0, top: 0 })
          },
          clientX: x,
          clientY: y,
          nativeEvent: {
            target: droppable.getDOMNode()
          }
        });
      }
      it("Updates when an item is dropped in", function () {
        const wrapper = mount(<DroppableLayout containerPadding={[0, 0]} />);
        const gridLayout = wrapper.find("ReactGridLayout");
        expect(gridLayout).toHaveLength(1);

        // Start: no dropping node.
        expect(gridLayout.state("droppingDOMNode")).toEqual(null);

        // Drag the droppable over the grid layout.
        dragDroppableTo(wrapper, 200, 140);

        // We should have the position in our state.
        expect(gridLayout.state("droppingPosition")).toHaveProperty(
          "left",
          200
        );
        expect(gridLayout.state("droppingPosition")).toHaveProperty("top", 140);
        // We should now have the placeholder element in our state.
        expect(gridLayout.state("droppingDOMNode")).toHaveProperty(
          "type",
          "div"
        );
        expect(gridLayout.state("droppingDOMNode")).toHaveProperty(
          "key",
          "__dropping-elem__"
        );

        // It should also have a layout item assigned to it.
        let layoutItem = gridLayout
          .state("layout")
          .find(item => item.i === "__dropping-elem__");
        expect(layoutItem).toEqual({
          i: "__dropping-elem__",
          h: 1,
          w: 1,
          x: 2,
          y: 4,
          static: false,
          isDraggable: true
        });

        // Let's move it some more.
        dragDroppableTo(wrapper, 0, 300);

        // State should change.
        expect(gridLayout.state("droppingPosition")).toHaveProperty("left", 0);
        expect(gridLayout.state("droppingPosition")).toHaveProperty("top", 300);

        layoutItem = gridLayout
          .state("layout")
          .find(item => item.i === "__dropping-elem__");
        // Using toMatchObject() here as this will inherit some undefined properties from the cloning
        expect(layoutItem).toMatchObject({
          i: "__dropping-elem__",
          h: 1,
          w: 1,
          x: 0,
          y: 10,
          static: false,
          isDraggable: true
        });
      });

      it("should transform output correctly when isBounded is true", () => {
        let transform = "transform";

        const wrapper = mount(
          <IsBounded
            onDragStop={(_, __, ___, ____, _____, element) => {
              transform = element.style.transform;
            }}
          />
        );
        const Item = wrapper.find("GridItem").at(1);

        simulateMovementFromTo(Item.getDOMNode(), 0, 0, 10, 0);
        expect(transform).toBe("translate(10px,0px)");
      });

      it("Allows customizing the droppable placeholder", function () {
        const wrapper = mount(
          <DroppableLayout onDropDragOver={() => ({ w: 2, h: 2 })} />
        );
        const gridLayout = wrapper.find("ReactGridLayout");

        // Find the droppable element and drag it over the grid layout.
        dragDroppableTo(wrapper, 200, 150);

        // It should also have a layout item assigned to it.
        const layoutItem = gridLayout
          .state("layout")
          .find(item => item.i === "__dropping-elem__");
        expect(layoutItem).toEqual({
          i: "__dropping-elem__",
          h: 2,
          w: 2,
          x: 2,
          y: 4,
          static: false,
          isDraggable: true
        });
      });

      it("Allows short-circuiting the drag", function () {
        const wrapper = mount(<DroppableLayout onDropDragOver={() => false} />);
        const gridLayout = wrapper.find("ReactGridLayout");

        // Find the droppable element and drag it over the grid layout.
        dragDroppableTo(wrapper, 200, 150);

        // It should also have a layout item assigned to it.
        const layoutItem = gridLayout
          .state("layout")
          .find(item => item.i === "__dropping-elem__");
        expect(layoutItem).toBeUndefined();
      });
    });

    describe("Resizing", () => {
      const rowHeight = 30;
      const colWidth = 101;
      const gridPadding = 10;
      let gridLayout;

      /**
       * Simulate a movement; can't use TestUtils here because it uses react's event system only,
       * but <DraggableCore> attaches event listeners directly to the document.
       * Would love to new MouseEvent() here but it doesn't work with PhantomJS / old browsers.
       * var e = new MouseEvent('mousemove', {clientX: 100, clientY: 100});
       *
       * Taken from: https://github.com/react-grid-layout/react-draggable/blob/master/specs/draggable.spec.jsx#L1000C1-L1003C70
       */
      const mouseMove = (node, x, y) => {
        const doc = node ? node.ownerDocument : document;
        const mouseEvent = new MouseEvent("mousemove", {
          button: 0,
          clientX: x,
          clientY: y,
          screenX: 0,
          screenY: 0
        });
        doc.dispatchEvent(mouseEvent);
      };

      const getCurrentPosition = wrapper => {
        const { x, y, w, h } = wrapper.props();
        const positionParams = wrapper.instance()?.getPositionParams();

        if (!positionParams) {
          return {};
        }

        return calcGridItemPosition(
          positionParams,
          x,
          y,
          w,
          h,
          wrapper.state()
        );
      };

      const resizeTo = (wrapper, preventMouseUp, currentPosition, x, y) => {
        const node = wrapper.getDOMNode();
        TestUtils.Simulate.mouseDown(node, {
          clientX: currentPosition.left,
          clientY: currentPosition.top
        });
        mouseMove(node, x, y);

        // In some test cases we want to take measurements before mouseUp occurs
        if (!preventMouseUp) {
          TestUtils.Simulate.mouseUp(node);
        }
      };

      const findGridItemByText = (wrapper, id) =>
        wrapper.findWhere(node => {
          const isGridItem = node.instance() instanceof GridItem;
          const hasSpecifiedText = node.text() === `${id}`;

          return isGridItem && hasSpecifiedText;
        });

      const findHandleForGridItem = (wrapper, handle) =>
        wrapper.find(`.react-resizable-handle-${handle}`);

      const getGridItemData = (wrapper, id) =>
        wrapper.state("layout").find(item => item.i == id);

      beforeEach(() => {
        const wrapper = mount(<ResizableLayout />);
        gridLayout = wrapper.find("ReactGridLayout");
      });

      it("sets up resizable handles", () => {
        const gridItem0 = findGridItemByText(gridLayout, 0);
        // Ensure every handle is present on the target element
        ["n", "ne", "e", "se", "s", "sw", "w", "nw"].forEach(handle => {
          expect(findHandleForGridItem(gridItem0, handle).exists()).toEqual(
            true
          );
        });
      });

      it("resizes from n handle", () => {
        const itemId = 6;
        const gridItem6 = findGridItemByText(gridLayout, itemId);
        const handleElement = findHandleForGridItem(gridItem6, "n");
        const pos = getCurrentPosition(gridItem6);

        const positionBeforeResize = getGridItemData(gridLayout, itemId);
        // Resize up two rows
        resizeTo(handleElement, false, pos, pos.left, pos.top - rowHeight * 2);
        const positionAfterResize = getGridItemData(gridLayout, itemId);
        expect(positionAfterResize).toEqual({
          ...positionBeforeResize,
          h: positionBeforeResize.h + 2,
          y: positionBeforeResize.y - 2
        });
      });

      it("resizes from ne handle", () => {
        const itemId = 6;
        const gridItem6 = findGridItemByText(gridLayout, itemId);
        const handleElement = findHandleForGridItem(gridItem6, "ne");
        const pos = getCurrentPosition(gridItem6);

        const positionBeforeResize = getGridItemData(gridLayout, itemId);
        // Resize up two rows and right two columns
        resizeTo(
          handleElement,
          false,
          pos,
          pos.left + colWidth * 2,
          pos.top - rowHeight * 2
        );
        const positionAfterResize = getGridItemData(gridLayout, itemId);
        expect(positionAfterResize).toEqual({
          ...positionBeforeResize,
          w: positionBeforeResize.w + 2,
          h: positionBeforeResize.h + 2,
          y: positionBeforeResize.y - 2
        });
      });

      it("resizes from e handle", () => {
        const itemId = 6;
        const gridItem6 = findGridItemByText(gridLayout, itemId);
        const handleElement = findHandleForGridItem(gridItem6, "e");
        const pos = getCurrentPosition(gridItem6);

        const positionBeforeResize = getGridItemData(gridLayout, itemId);
        // Resize right two columns
        resizeTo(handleElement, false, pos, pos.left + colWidth * 2, pos.top);
        const positionAfterResize = getGridItemData(gridLayout, itemId);
        expect(positionAfterResize).toEqual({
          ...positionBeforeResize,
          w: positionBeforeResize.w + 2
        });
      });

      it("resizes from se handle", () => {
        const itemId = 6;
        const gridItem6 = findGridItemByText(gridLayout, itemId);
        const handleElement = findHandleForGridItem(gridItem6, "se");
        const pos = getCurrentPosition(gridItem6);

        const positionBeforeResize = getGridItemData(gridLayout, itemId);
        // Resize down two rows and right two columns
        resizeTo(
          handleElement,
          false,
          pos,
          pos.left + colWidth * 2,
          pos.top + rowHeight * 2
        );
        const positionAfterResize = getGridItemData(gridLayout, itemId);
        expect(positionAfterResize).toEqual({
          ...positionBeforeResize,
          w: positionBeforeResize.w + 2,
          h: positionBeforeResize.h + 2
        });
      });

      it("resizes from s handle", () => {
        const itemId = 6;
        const gridItem6 = findGridItemByText(gridLayout, itemId);
        const handleElement = findHandleForGridItem(gridItem6, "s");
        const pos = getCurrentPosition(gridItem6);

        const positionBeforeResize = getGridItemData(gridLayout, itemId);
        // Resize right two columns
        resizeTo(handleElement, false, pos, pos.left, pos.top + rowHeight * 2);
        const positionAfterResize = getGridItemData(gridLayout, itemId);
        expect(positionAfterResize).toEqual({
          ...positionBeforeResize,
          h: positionBeforeResize.h + 2
        });
      });

      it("resizes from sw handle", () => {
        const itemId = 1;
        const gridItem1 = findGridItemByText(gridLayout, itemId);
        const handleElement = findHandleForGridItem(gridItem1, "sw");
        const pos = getCurrentPosition(gridItem1);

        const positionBeforeResize = getGridItemData(gridLayout, itemId);
        // Resize down two rows and left two columns
        resizeTo(
          handleElement,
          false,
          pos,
          pos.left - colWidth * 2,
          pos.top + rowHeight * 2
        );
        const positionAfterResize = getGridItemData(gridLayout, itemId);
        expect(positionAfterResize).toEqual({
          ...positionBeforeResize,
          x: positionBeforeResize.x - 2,
          w: positionBeforeResize.w + 2,
          h: positionBeforeResize.h + 2
        });
      });

      it("resizes from w handle", () => {
        const itemId = 1;
        const gridItem1 = findGridItemByText(gridLayout, itemId);
        const handleElement = findHandleForGridItem(gridItem1, "w");
        const pos = getCurrentPosition(gridItem1);

        const positionBeforeResize = getGridItemData(gridLayout, itemId);
        // Resize left two columns
        resizeTo(handleElement, false, pos, pos.left - colWidth * 2, pos.top);
        const positionAfterResize = getGridItemData(gridLayout, itemId);
        expect(positionAfterResize).toEqual({
          ...positionBeforeResize,
          x: positionBeforeResize.x - 2,
          w: positionBeforeResize.w + 2
        });
      });

      it("resizes from nw handle", () => {
        const itemId = 7;
        const gridItem7 = findGridItemByText(gridLayout, itemId);
        const handleElement = findHandleForGridItem(gridItem7, "nw");
        const pos = getCurrentPosition(gridItem7);

        const positionBeforeResize = getGridItemData(gridLayout, itemId);
        // Resize up two rows and left two columns
        resizeTo(
          handleElement,
          false,
          pos,
          pos.left - colWidth * 2,
          pos.top - rowHeight * 2
        );
        const positionAfterResize = getGridItemData(gridLayout, itemId);
        expect(positionAfterResize).toEqual({
          ...positionBeforeResize,
          x: positionBeforeResize.x - 2,
          w: positionBeforeResize.w + 2,
          h: positionBeforeResize.h + 2,
          y: positionBeforeResize.y - 2
        });
      });

      describe("Out of bounds prevention", () => {
        it("prevents OOB from n handle", () => {
          const itemId = 0;
          const gridItem0 = findGridItemByText(gridLayout, itemId);
          const handleElement = findHandleForGridItem(gridItem0, "n");
          const pos = getCurrentPosition(gridItem0);

          // Attempt to resize up two rows outside of the container
          resizeTo(handleElement, true, pos, pos.left, pos.top - rowHeight * 2);
          const posAfter = getCurrentPosition(gridItem0);
          // We should prevent resizing outside container bounds
          expect(posAfter).toEqual({
            ...pos,
            top: 0
          });
        });

        it("prevents OOB from ne handle", () => {
          const itemId = 5;
          const gridItem5 = findGridItemByText(gridLayout, itemId);
          const handleElement = findHandleForGridItem(gridItem5, "ne");
          const pos = getCurrentPosition(gridItem5);

          // Attempt to resize up two rows and right two rows outside of the container
          resizeTo(
            handleElement,
            true,
            pos,
            pos.left + colWidth * 2,
            pos.top - rowHeight * 2
          );
          const posAfter = getCurrentPosition(gridItem5);
          // We should prevent resizing outside container bounds
          expect(posAfter).toEqual({
            ...pos,
            top: 0,
            left: gridLayout.props().width - gridPadding - pos.width
          });
        });

        it("prevents OOB from e handle", () => {
          const itemId = 5;
          const gridItem5 = findGridItemByText(gridLayout, itemId);
          const handleElement = findHandleForGridItem(gridItem5, "e");
          const pos = getCurrentPosition(gridItem5);

          // Attempt to resize right two rows outside of the container
          resizeTo(handleElement, true, pos, pos.left + colWidth * 2, pos.top);
          const posAfter = getCurrentPosition(gridItem5);
          // We should prevent resizing outside container bounds
          expect(posAfter).toEqual({
            ...pos,
            left: gridLayout.props().width - gridPadding - pos.width
          });
        });

        it("prevents OOB from se handle", () => {
          const itemId = 5;
          const gridItem5 = findGridItemByText(gridLayout, itemId);
          const handleElement = findHandleForGridItem(gridItem5, "se");
          const pos = getCurrentPosition(gridItem5);

          // Attempt to resize down two rows and right two rows outside of the container
          resizeTo(
            handleElement,
            true,
            pos,
            pos.left + colWidth * 2,
            pos.top + rowHeight * 2
          );
          const posAfter = getCurrentPosition(gridItem5);
          // We should prevent resizing outside container bounds
          expect(posAfter).toEqual({
            ...pos,
            // There is always room to grow south, so height should adjust as normal
            height: pos.height + rowHeight * 2,
            left: gridLayout.props().width - gridPadding - pos.width
          });
        });

        it("prevents OOB from sw handle", () => {
          const itemId = 0;
          const gridItem0 = findGridItemByText(gridLayout, itemId);
          const handleElement = findHandleForGridItem(gridItem0, "sw");
          const pos = getCurrentPosition(gridItem0);

          // Attempt to resize down two rows and left two rows outside of the container
          resizeTo(
            handleElement,
            true,
            pos,
            pos.left - colWidth * 2,
            pos.top + rowHeight * 2
          );
          const posAfter = getCurrentPosition(gridItem0);
          // We should prevent resizing outside container bounds
          expect(posAfter).toEqual({
            ...pos,
            // There is always room to grow south, so height should adjust as normal
            height: pos.height + rowHeight * 2,
            left: 0
          });
        });

        it("prevents OOB from w handle", () => {
          const itemId = 0;
          const gridItem0 = findGridItemByText(gridLayout, itemId);
          const handleElement = findHandleForGridItem(gridItem0, "w");
          const pos = getCurrentPosition(gridItem0);

          // Attempt to resize left two rows outside of the container
          resizeTo(handleElement, true, pos, pos.left - colWidth * 2, pos.top);
          const posAfter = getCurrentPosition(gridItem0);
          // We should prevent resizing outside container bounds
          expect(posAfter).toEqual({
            ...pos,
            left: 0
          });
        });

        it("prevents OOB from nw handle", () => {
          const itemId = 0;
          const gridItem0 = findGridItemByText(gridLayout, itemId);
          const handleElement = findHandleForGridItem(gridItem0, "nw");
          const pos = getCurrentPosition(gridItem0);

          // Attempt to resize up two rows and left two rows outside of the container
          resizeTo(
            handleElement,
            true,
            pos,
            pos.left - colWidth * 2,
            pos.top - rowHeight * 2
          );
          const posAfter = getCurrentPosition(gridItem0);
          // We should prevent resizing outside container bounds
          expect(posAfter).toEqual({
            ...pos,
            top: 0,
            left: 0
          });
        });
      });

      describe("Resizing first row when containerPadding is disabled (#1929)", () => {
        const rowHeight = 150;

        it("resizes from s handle when containerPadding=[0, 0]", () => {
          const wrapper = mount(
            <ResizableLayout rowHeight={rowHeight} containerPadding={[0, 0]} />
          );
          const gridLayout = wrapper.find("ReactGridLayout");
          const itemId = 0;
          const gridItem0 = findGridItemByText(gridLayout, itemId);
          const handleElement = findHandleForGridItem(gridItem0, "s");
          const pos = getCurrentPosition(gridItem0);
          const positionBeforeResize = getGridItemData(gridLayout, itemId);

          // Resize down two columns
          resizeTo(
            handleElement,
            false,
            pos,
            pos.left,
            pos.top + rowHeight * 2
          );
          const positionAfterResize = getGridItemData(gridLayout, itemId);
          expect(positionAfterResize).toEqual({
            ...positionBeforeResize,
            h: positionBeforeResize.h + 2
          });
        });
      });

      describe("preventCollision=true and no compaction (#1933)", () => {
        const resizeHandles = ["n", "e", "s", "w"];
        // eslint-disable-next-line react/prop-types
        const PreventCollisionContainer = ({ layoutA, layoutB }) => (
          <ReactGridLayout
            className="layout"
            cols={12}
            rowHeight={30}
            width={1200}
            preventCollision={true}
            compactType={null}
            resizeHandles={resizeHandles}
          >
            <div key="0" data-grid={layoutA}>
              0
            </div>
            <div key="1" data-grid={layoutB}>
              1
            </div>
          </ReactGridLayout>
        );

        it("Does not allow elements to move when resizing with no free space", () => {
          const wrapper = mount(
            <PreventCollisionContainer
              layoutA={{ x: 0, y: 0, w: 1, h: 2, i: "0" }}
              layoutB={{ x: 1, y: 0, w: 7, h: 2, i: "1" }}
            />
          );
          const gridLayout = wrapper.find("ReactGridLayout");
          const itemId = 0;
          const gridItem0 = findGridItemByText(gridLayout, itemId);
          const handleElement = findHandleForGridItem(gridItem0, "e");
          const pos = getCurrentPosition(gridItem0);
          const positionBeforeResize = getGridItemData(gridLayout, itemId);

          // Resize right two columns
          resizeTo(handleElement, true, pos, pos.left + colWidth * 2, pos.top);
          const positionAfterResize = getGridItemData(gridLayout, itemId);
          // Position shouldn't change because the system should preventCollisions
          expect(positionAfterResize).toEqual(positionBeforeResize);
        });

        it("Allows elements to resize only within available free space", () => {
          const wrapper = mount(
            <PreventCollisionContainer
              layoutA={{ x: 0, y: 0, w: 1, h: 2, i: "0" }}
              layoutB={{ x: 2, y: 0, w: 7, h: 2, i: "1" }}
            />
          );
          const gridLayout = wrapper.find("ReactGridLayout");
          const itemId = 0;
          const gridItem0 = findGridItemByText(gridLayout, itemId);
          const handleElement = findHandleForGridItem(gridItem0, "e");
          const pos = getCurrentPosition(gridItem0);
          const positionBeforeResize = getGridItemData(gridLayout, itemId);

          // Resize right two columns
          resizeTo(handleElement, true, pos, pos.left + colWidth, pos.top);
          resizeTo(handleElement, false, pos, pos.left + colWidth * 2, pos.top);
          const positionAfterResize = getGridItemData(gridLayout, itemId);
          /**
           * Width should only increase by 1 column, not 2, because there is a
           * collision that occurs at x=2 when attempting to resize over it.
           **/
          expect(positionAfterResize).toEqual({
            ...positionBeforeResize,
            w: 2
          });
        });

        it("Allows elements to resize within free space", () => {
          const wrapper = mount(
            <PreventCollisionContainer
              layoutA={{ x: 0, y: 0, w: 1, h: 2, i: "0" }}
              layoutB={{ x: 10, y: 0, w: 2, h: 2, i: "1" }}
            />
          );
          const gridLayout = wrapper.find("ReactGridLayout");
          const itemId = 0;
          const gridItem0 = findGridItemByText(gridLayout, itemId);
          const handleElement = findHandleForGridItem(gridItem0, "e");
          const pos = getCurrentPosition(gridItem0);
          const positionBeforeResize = getGridItemData(gridLayout, itemId);

          // Resize right nine columns
          resizeTo(handleElement, true, pos, pos.left + colWidth * 9, pos.top);
          const positionAfterResize = getGridItemData(gridLayout, itemId);
          expect(positionAfterResize).toEqual({
            ...positionBeforeResize,
            w: 10
          });
        });
      });
    });
  });

  describe("<ResponsiveReactGridLayout>", function () {
    it("Basic Render", async function () {
      const wrapper = mount(<ShowcaseLayout />);
      expect(wrapper).toMatchSnapshot();
    });

    it("Does not modify layout on movement", async function () {
      const layouts = {
        lg: [
          ..._.times(3, i => ({
            i: String(i),
            x: i,
            y: 0,
            w: 1,
            h: 1
          }))
        ]
      };
      const frozenLayouts = deepFreeze(layouts, {
        set: true,
        get: false /* don't crash on unknown gets */
      });
      // Render the basic Responsive layout.
      const wrapper = mount(
        <ResponsiveReactGridLayout
          layouts={frozenLayouts}
          width={1280}
          breakpoint="lg"
        >
          {_.times(3, i => (
            <div key={i} />
          ))}
        </ResponsiveReactGridLayout>
      );

      // Set that layout as state and ensure it doesn't change.
      wrapper.setState({ layouts: frozenLayouts });
      wrapper.setProps({ width: 800, breakpoint: "md" }); // will generate new layout
      wrapper.render();

      expect(frozenLayouts).not.toContain("md");
    });
  });
});