Skip to content

Commit 279ace8

Browse files
committed
test(draggable): additional ref tests
1 parent 22b0615 commit 279ace8

File tree

1 file changed

+40
-9
lines changed

1 file changed

+40
-9
lines changed

specs/draggable.spec.jsx

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -714,13 +714,14 @@ describe('react-draggable', function () {
714714
});
715715

716716
describe('draggable callbacks', function () {
717-
it('should call back on drag', function () {
717+
it('should call back on drag', function (done) {
718718
function onDrag(event, data) {
719719
assert(data.x === 100);
720720
assert(data.y === 100);
721721
assert(data.deltaX === 100);
722722
assert(data.deltaY === 100);
723723
assert(data.node === ReactDOM.findDOMNode(drag));
724+
done();
724725
}
725726
drag = TestUtils.renderIntoDocument(
726727
<Draggable onDrag={onDrag}>
@@ -732,11 +733,12 @@ describe('react-draggable', function () {
732733
simulateMovementFromTo(drag, 0, 0, 100, 100);
733734
});
734735

735-
it('should call back with correct dom node with nodeRef', function () {
736+
it('should call back with correct dom node with nodeRef', function (done) {
736737
function onDrag(event, data) {
737738
// Being tricky here and installing the ref on the inner child, to ensure it's working
738739
// and not just falling back on ReactDOM.findDOMNode()
739740
assert(data.node === ReactDOM.findDOMNode(drag).firstChild);
741+
done();
740742
}
741743
const nodeRef = React.createRef();
742744
drag = TestUtils.renderIntoDocument(
@@ -751,12 +753,35 @@ describe('react-draggable', function () {
751753
simulateMovementFromTo(drag, 0, 0, 100, 100);
752754
});
753755

754-
it('should call back on drag, with values within the defined bounds', function(){
756+
it('should call back with correct dom node with nodeRef (forwardRef)', function (done) {
757+
758+
const Component1 = React.forwardRef(function (props, ref) {
759+
return <div {...props} ref={ref}>Nested component</div>;
760+
});
761+
762+
function onDrag(event, data) {
763+
assert(data.node === ReactDOM.findDOMNode(drag));
764+
assert(data.node.innerText === 'Nested component');
765+
done();
766+
}
767+
const nodeRef = React.createRef();
768+
drag = TestUtils.renderIntoDocument(
769+
<DraggableCore onDrag={onDrag} nodeRef={nodeRef}>
770+
<Component1 ref={nodeRef} />
771+
</DraggableCore>
772+
);
773+
774+
// (element, fromX, fromY, toX, toY)
775+
simulateMovementFromTo(drag, 0, 0, 100, 100);
776+
});
777+
778+
it('should call back on drag, with values within the defined bounds', function(done){
755779
function onDrag(event, data) {
756780
assert(data.x === 90);
757781
assert(data.y === 90);
758782
assert(data.deltaX === 90);
759783
assert(data.deltaY === 90);
784+
done();
760785
}
761786
drag = TestUtils.renderIntoDocument(
762787
<Draggable onDrag={onDrag} bounds={{left: 0, right: 90, top: 0, bottom: 90}}>
@@ -769,12 +794,13 @@ describe('react-draggable', function () {
769794

770795
});
771796

772-
it('should call back with offset left/top, not client', function () {
797+
it('should call back with offset left/top, not client', function(done) {
773798
function onDrag(event, data) {
774799
assert(data.x === 100);
775800
assert(data.y === 100);
776801
assert(data.deltaX === 100);
777802
assert(data.deltaY === 100);
803+
done();
778804
}
779805
drag = TestUtils.renderIntoDocument(
780806
<Draggable onDrag={onDrag} >
@@ -785,14 +811,15 @@ describe('react-draggable', function () {
785811
simulateMovementFromTo(drag, 200, 200, 300, 300);
786812
});
787813

788-
it('should call back with correct position when parent element is 2x scaled', function() {
814+
it('should call back with correct position when parent element is 2x scaled', function(done) {
789815
function onDrag(event, data) {
790816
// visually it will look like 100, because parent is 2x scaled
791817
assert(data.x === 50);
792818
assert(data.y === 50);
793819
assert(data.deltaX === 50);
794820
assert(data.deltaY === 50);
795821
assert(data.node === ReactDOM.findDOMNode(drag));
822+
done();
796823
}
797824
drag = TestUtils.renderIntoDocument(
798825
<Draggable onDrag={onDrag} scale={2}>
@@ -804,14 +831,15 @@ describe('react-draggable', function () {
804831
simulateMovementFromTo(drag, 0, 0, 100, 100);
805832
});
806833

807-
it('should call back with correct position when parent element is 0.5x scaled', function() {
834+
it('should call back with correct position when parent element is 0.5x scaled', function(done) {
808835
function onDrag(event, data) {
809836
// visually it will look like 100, because parent is 0.5x scaled
810837
assert(data.x === 200);
811838
assert(data.y === 200);
812839
assert(data.deltaX === 200);
813840
assert(data.deltaY === 200);
814841
assert(data.node === ReactDOM.findDOMNode(drag));
842+
done();
815843
}
816844
drag = TestUtils.renderIntoDocument(
817845
<Draggable onDrag={onDrag} scale={0.5}>
@@ -857,13 +885,14 @@ describe('react-draggable', function () {
857885
});
858886

859887
describe('DraggableCore callbacks', function () {
860-
it('should call back with node on drag', function () {
888+
it('should call back with node on drag', function(done) {
861889
function onDrag(event, data) {
862890
assert(data.x === 100);
863891
assert(data.y === 100);
864892
assert(data.deltaX === 100);
865893
assert(data.deltaY === 100);
866894
assert(data.node === ReactDOM.findDOMNode(drag));
895+
done();
867896
}
868897
drag = TestUtils.renderIntoDocument(
869898
<DraggableCore onDrag={onDrag}>
@@ -875,14 +904,15 @@ describe('react-draggable', function () {
875904
simulateMovementFromTo(drag, 0, 0, 100, 100);
876905
});
877906

878-
it('should call back with correct position when parent element is 2x scaled', function() {
907+
it('should call back with correct position when parent element is 2x scaled', function(done) {
879908
function onDrag(event, data) {
880909
// visually it will look like 100, because parent is 2x scaled
881910
assert(data.x === 50);
882911
assert(data.y === 50);
883912
assert(data.deltaX === 50);
884913
assert(data.deltaY === 50);
885914
assert(data.node === ReactDOM.findDOMNode(drag));
915+
done();
886916
}
887917
drag = TestUtils.renderIntoDocument(
888918
<DraggableCore onDrag={onDrag} scale={2}>
@@ -894,14 +924,15 @@ describe('react-draggable', function () {
894924
simulateMovementFromTo(drag, 0, 0, 100, 100);
895925
});
896926

897-
it('should call back with correct position when parent element is 0.5x scaled', function() {
927+
it('should call back with correct position when parent element is 0.5x scaled', function(done) {
898928
function onDrag(event, data) {
899929
// visually it will look like 100, because parent is 0.5x scaled
900930
assert(data.x === 200);
901931
assert(data.y === 200);
902932
assert(data.deltaX === 200);
903933
assert(data.deltaY === 200);
904934
assert(data.node === ReactDOM.findDOMNode(drag));
935+
done();
905936
}
906937
drag = TestUtils.renderIntoDocument(
907938
<DraggableCore onDrag={onDrag} scale={0.5}>

0 commit comments

Comments
 (0)