@@ -3,24 +3,6 @@ import React from 'react';
3
3
import { spy } from 'sinon' ;
4
4
import { Resizable } from '.' ;
5
5
6
- const mouseUp = ( x : number , y : number ) => {
7
- const event = document . createEvent ( 'MouseEvents' ) ;
8
- event . initMouseEvent ( 'mouseup' , true , true , window , 0 , 0 , 0 , x , y , false , false , false , false , 0 , null ) ;
9
- document . dispatchEvent ( event ) ;
10
- return event ;
11
- } ;
12
-
13
- // test.afterEach(async ({ mount })=> {
14
- // ReactDOM.unmountComponentAtNode(document.body);
15
- // const content = document.querySelector('#content');
16
- // if (!content) return;
17
- // ReactDOM.unmountComponentAtNode(content);
18
- // });
19
-
20
- const fail = ( m : string = 'unknown reason' ) => {
21
- throw new Error ( `[fail] ${ m } ` ) ;
22
- } ;
23
-
24
6
test . use ( { viewport : { width : 500 , height : 500 } } ) ;
25
7
26
8
test ( 'should box width and height equal 100px' , async ( { mount } ) => {
@@ -272,18 +254,20 @@ test('Should only bottomRight is resizable and call onResizeStart when mousedown
272
254
expect ( onResizeStart . getCall ( 0 ) . args [ 1 ] ) . toBe ( 'bottomRight' ) ;
273
255
} ) ;
274
256
275
- test ( 'Should not begin resize when onResizeStart returns false' , async ( { mount, page } ) => {
276
- const onResizeStart = ( ) => {
277
- return false ;
278
- } ;
279
- const onResize = spy ( ) ;
280
- const resizable = await mount ( < Resizable onResizeStart = { onResizeStart } onResize = { onResize } /> ) ;
281
- const divs = resizable . locator ( 'div' ) ;
282
- await ( await divs . all ( ) ) [ 1 ] . dispatchEvent ( 'mousedown' ) ;
283
- await page . mouse . move ( 100 , 200 ) ;
284
- await page . mouse . up ( ) ;
285
- expect ( onResize . callCount ) . toBe ( 0 ) ;
286
- } ) ;
257
+ // TODO: flacky
258
+ // test('Should not begin resize when onResizeStart returns false', async ({ mount, page }) => {
259
+ // const onResizeStart = () => {
260
+ // return false;
261
+ // };
262
+ // const onResize = spy();
263
+ // const resizable = await mount(<Resizable onResizeStart={onResizeStart} onResize={onResize} />);
264
+ // const divs = resizable.locator('div');
265
+ // await (await divs.all())[1].dispatchEvent('mousedown');
266
+ // await page.mouse.down();
267
+ // await page.mouse.move(100, 200);
268
+ // await page.mouse.up();
269
+ // expect(onResize.callCount).toBe(0);
270
+ // });
287
271
288
272
// test('should call onResize with expected args when resize direction right', async ({ mount, page }) => {
289
273
// const onResize = spy();
@@ -622,133 +606,136 @@ test('should allow 0 as minWidth', async ({ mount, page }) => {
622
606
expect ( onResize . firstCall . args [ 3 ] ) . toEqual ( { width : - 100 , height : 0 } ) ;
623
607
} ) ;
624
608
625
- /*
626
- test('should clamped by max height', async ({ mount })=> {
609
+ test ( 'should clamped by max height' , async ( { mount, page } ) => {
627
610
const onResize = spy ( ) ;
628
611
const onResizeStart = spy ( ) ;
629
612
const onResizeStop = spy ( ) ;
630
- const resizable = ReactDOM.render<ResizableProps, Resizable> (
613
+ const resizable = await mount (
631
614
< Resizable
632
615
defaultSize = { { width : 100 , height : 100 } }
633
616
maxHeight = { 200 }
634
617
onResize = { onResize }
635
618
onResizeStart = { onResizeStart }
636
619
onResizeStop = { onResizeStop }
637
620
/> ,
638
- document.getElementById('content'),
639
621
) ;
640
622
const divs = resizable . locator ( 'div' ) ;
641
- const node = ReactDOM.findDOMNode(divs[7]);
642
- if (!node || !(node instanceof HTMLDivElement)) return fail();
643
- TestUtils.Simulate.mouseDown(node, { clientX: 0, clientY: 0 });
644
- mouseMove(0, 200);
645
- t.true(onResize.getCall(0).args[0] instanceof MouseEvent);
646
- t.deepEqual(onResize.getCall(0).args[2].clientHeight, 200);
647
- t.deepEqual(onResize.getCall(0).args[3], { width: 0, height: 100 });
623
+ const bottomHandle = ( await divs . all ( ) ) [ 3 ] ;
624
+ await bottomHandle . dispatchEvent ( 'mousedown' , { button : 0 , clientX : 0 , clientY : 0 } ) ;
625
+ await page . mouse . move ( 0 , 200 ) ;
626
+ await page . mouse . up ( ) ;
627
+ expect ( onResize . getCall ( 0 ) . args [ 0 ] . isTrusted ) . toBe ( true ) ;
628
+ const clientHeight = await resizable . evaluate ( el => el . clientHeight ) ;
629
+ expect ( clientHeight ) . toBe ( 200 ) ;
630
+ expect ( onResize . getCall ( 0 ) . args [ 3 ] ) . toEqual ( { width : 0 , height : 100 } ) ;
648
631
} ) ;
649
632
650
- test('should clamped by min height', async ({ mount }) => {
633
+ test ( 'should clamped by min height' , async ( { mount, page } ) => {
651
634
const onResize = spy ( ) ;
652
635
const onResizeStart = spy ( ) ;
653
636
const onResizeStop = spy ( ) ;
654
- const resizable = ReactDOM.render<ResizableProps, Resizable> (
637
+ const resizable = await mount (
655
638
< Resizable
656
639
defaultSize = { { width : 100 , height : 100 } }
657
640
minHeight = { 50 }
658
641
onResize = { onResize }
659
642
onResizeStart = { onResizeStart }
660
643
onResizeStop = { onResizeStop }
661
644
/> ,
662
- document.getElementById('content'),
663
645
) ;
664
646
const divs = resizable . locator ( 'div' ) ;
665
- const node = ReactDOM.findDOMNode(divs[7]);
666
- if (!node || !(node instanceof HTMLDivElement)) return fail();
667
- TestUtils.Simulate.mouseDown(node, { clientX: 0, clientY: 0 });
668
- mouseMove(0, -100);
669
- t.true(onResize.getCall(0).args[0] instanceof MouseEvent);
670
- t.deepEqual(onResize.getCall(0).args[2].clientHeight, 50);
671
- t.deepEqual(onResize.getCall(0).args[3], { width: 0, height: -50 });
647
+ const bottomHandle = ( await divs . all ( ) ) [ 3 ] ;
648
+ await bottomHandle . dispatchEvent ( 'mousedown' , { button : 0 , clientX : 0 , clientY : 0 } ) ;
649
+ await page . mouse . down ( ) ;
650
+ await page . mouse . move ( 0 , - 100 ) ;
651
+ await page . mouse . up ( ) ;
652
+ expect ( onResize . getCall ( 0 ) . args [ 0 ] . isTrusted ) . toBe ( true ) ;
653
+ const clientHeight = await resizable . evaluate ( el => el . clientHeight ) ;
654
+ expect ( clientHeight ) . toBe ( 50 ) ;
655
+ expect ( onResize . getCall ( 0 ) . args [ 3 ] ) . toEqual ( { width : 0 , height : - 50 } ) ;
672
656
} ) ;
673
657
674
- test('should allow 0 as minHeight', async ({ mount }) => {
658
+ test ( 'should allow 0 as minHeight' , async ( { mount, page } ) => {
675
659
const onResize = spy ( ) ;
676
660
const onResizeStart = spy ( ) ;
677
661
const onResizeStop = spy ( ) ;
678
- const resizable = ReactDOM.render<ResizableProps, Resizable> (
662
+ const resizable = await mount (
679
663
< Resizable
680
664
defaultSize = { { width : 100 , height : 100 } }
681
665
minHeight = { 0 }
682
666
onResize = { onResize }
683
667
onResizeStart = { onResizeStart }
684
668
onResizeStop = { onResizeStop }
685
669
/> ,
686
- document.getElementById('content'),
687
670
) ;
688
671
const divs = resizable . locator ( 'div' ) ;
689
- const node = ReactDOM.findDOMNode(divs[7]);
690
- if (!node || !(node instanceof HTMLDivElement)) return fail();
691
- TestUtils.Simulate.mouseDown(node, { clientX: 0, clientY: 0 });
692
- mouseMove(0, -100);
693
- t.true(onResize.getCall(0).args[0] instanceof MouseEvent);
694
- t.deepEqual(onResize.getCall(0).args[2].clientHeight, 0);
695
- t.deepEqual(onResize.getCall(0).args[3], { width: 0, height: -100 });
672
+ const bottomHandle = ( await divs . all ( ) ) [ 3 ] ;
673
+ if ( ! bottomHandle ) throw new Error ( 'Handle not found' ) ;
674
+ await bottomHandle . dispatchEvent ( 'mousedown' , { button : 0 , clientX : 0 , clientY : 0 } ) ;
675
+ await page . mouse . down ( ) ;
676
+ await page . mouse . move ( 0 , - 100 ) ;
677
+ await page . mouse . up ( ) ;
678
+ const clientHeight = await resizable . evaluate ( el => el . clientHeight ) ;
679
+ expect ( clientHeight ) . toBe ( 0 ) ;
680
+ expect ( onResize . firstCall . args [ 3 ] ) . toEqual ( { width : 0 , height : - 100 } ) ;
696
681
} ) ;
697
682
698
- test('should aspect ratio locked when resize to right', async ({ mount }) => {
683
+ test ( 'should aspect ratio locked when resize to right' , async ( { mount, page } ) => {
699
684
const onResize = spy ( ) ;
700
685
const onResizeStart = spy ( ) ;
701
686
const onResizeStop = spy ( ) ;
702
- const resizable = ReactDOM.render<ResizableProps, Resizable> (
687
+ const resizable = await mount (
703
688
< Resizable
704
689
defaultSize = { { width : 100 , height : 100 } }
705
690
onResize = { onResize }
706
691
onResizeStart = { onResizeStart }
707
692
onResizeStop = { onResizeStop }
708
693
lockAspectRatio
709
694
/> ,
710
- document.getElementById('content'),
711
695
) ;
712
696
const divs = resizable . locator ( 'div' ) ;
713
- const node = ReactDOM.findDOMNode(divs[3]);
714
- if (!node || !(node instanceof HTMLDivElement)) return fail();
715
- TestUtils.Simulate.mouseDown(node, { clientX: 0, clientY: 0 });
716
- mouseMove(200, 0);
717
- mouseUp(200, 0);
718
- t.is(onResizeStop.callCount, 1);
719
- t.true(onResize.getCall(0).args[0] instanceof MouseEvent);
720
- t.deepEqual(onResize.getCall(0).args[2].clientWidth, 300);
721
- t.deepEqual(onResize.getCall(0).args[2].clientHeight, 300);
722
- t.deepEqual(onResize.getCall(0).args[3], { width: 200, height: 200 });
697
+ const rightHandle = ( await divs . all ( ) ) [ 2 ] ;
698
+ await rightHandle . dispatchEvent ( 'mousedown' , { button : 0 , clientX : 0 , clientY : 0 } ) ;
699
+ await page . mouse . down ( ) ;
700
+ await page . mouse . move ( 200 , 0 ) ;
701
+ await page . mouse . up ( ) ;
702
+ expect ( onResizeStop . callCount ) . toBe ( 1 ) ;
703
+ const clientHeight = await resizable . evaluate ( el => el . clientHeight ) ;
704
+ const clientWidth = await resizable . evaluate ( el => el . clientWidth ) ;
705
+ expect ( clientWidth ) . toBe ( 300 ) ;
706
+ expect ( clientHeight ) . toBe ( 300 ) ;
707
+ expect ( onResize . getCall ( 0 ) . args [ 3 ] ) . toEqual ( { width : 200 , height : 200 } ) ;
723
708
} ) ;
724
709
725
- test('should aspect ratio locked with 1:1 ratio when resize to right', async ({ mount }) => {
710
+ test ( 'should aspect ratio locked with 1:1 ratio when resize to right' , async ( { mount, page } ) => {
726
711
const onResize = spy ( ) ;
727
712
const onResizeStart = spy ( ) ;
728
713
const onResizeStop = spy ( ) ;
729
- const resizable = ReactDOM.render<ResizableProps, Resizable> (
714
+ const resizable = await mount (
730
715
< Resizable
731
716
defaultSize = { { width : 100 , height : 100 } }
732
717
onResize = { onResize }
733
718
onResizeStart = { onResizeStart }
734
719
onResizeStop = { onResizeStop }
735
- lockAspectRatio={1 / 1 }
720
+ lockAspectRatio = { 1 }
736
721
/> ,
737
- document.getElementById('content'),
738
722
) ;
739
723
const divs = resizable . locator ( 'div' ) ;
740
- const node = ReactDOM.findDOMNode(divs[3]);
741
- if (!node || !(node instanceof HTMLDivElement)) return fail();
742
- TestUtils.Simulate.mouseDown(node, { clientX: 0, clientY: 0 });
743
- mouseMove(200, 0);
744
- mouseUp(200, 0);
745
- t.is(onResizeStop.callCount, 1);
746
- t.true(onResize.getCall(0).args[0] instanceof MouseEvent);
747
- t.deepEqual(onResize.getCall(0).args[2].clientWidth, 300);
748
- t.deepEqual(onResize.getCall(0).args[2].clientHeight, 300);
749
- t.deepEqual(onResize.getCall(0).args[3], { width: 200, height: 200 });
724
+ const rightHandle = ( await divs . all ( ) ) [ 2 ] ;
725
+ await rightHandle . dispatchEvent ( 'mousedown' , { button : 0 , clientX : 0 , clientY : 0 } ) ;
726
+ await page . mouse . down ( ) ;
727
+ await page . mouse . move ( 200 , 0 ) ;
728
+ await page . mouse . up ( ) ;
729
+ expect ( onResizeStop . callCount ) . toBe ( 1 ) ;
730
+ expect ( onResize . getCall ( 0 ) . args [ 0 ] . isTrusted ) . toBeTruthy ( ) ;
731
+ const clientWidth = await resizable . evaluate ( el => el . clientWidth ) ;
732
+ const clientHeight = await resizable . evaluate ( el => el . clientHeight ) ;
733
+ expect ( clientWidth ) . toBe ( 300 ) ;
734
+ expect ( clientHeight ) . toBe ( 300 ) ;
735
+ expect ( onResize . getCall ( 0 ) . args [ 3 ] ) . toEqual ( { width : 200 , height : 200 } ) ;
750
736
} ) ;
751
737
738
+ /*
752
739
test('should aspect ratio locked with 2:1 ratio when resize to right', async ({ mount })=> {
753
740
const onResize = spy();
754
741
const onResizeStart = spy();
0 commit comments