Skip to content

Commit cc280c7

Browse files
committed
Move init invocation to componentDidMount
1 parent 9e858d0 commit cc280c7

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

src/ReactCursorPosition.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ export default class extends React.Component {
8181
};
8282

8383
onTouchStart(e) {
84-
this.init();
8584
this.onTouchDetected();
8685
this.setShouldGuardAgainstMouseEmulationByDevices();
8786

@@ -129,7 +128,6 @@ export default class extends React.Component {
129128
return;
130129
}
131130

132-
this.init();
133131
this.onMouseDetected();
134132
this.setPositionState(this.core.getCursorPosition(e));
135133
this.clearActivationTimers();
@@ -172,6 +170,8 @@ export default class extends React.Component {
172170
}
173171

174172
componentDidMount() {
173+
this.init();
174+
175175
if (this.props.isEnabled) {
176176
this.enable();
177177
}

test/ReactCursorPosition.spec.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@ describe('ReactCursorPosition', () => {
6060

6161
it('decorates child components with props in the touch environment', () => {
6262
const mountedTree = getMountedComponentTree({ isActivatedOnTouch: true });
63+
const instance = mountedTree.instance();
6364

64-
mountedTree.instance().onTouchStart(getTouchEvent({ pageX: 1, pageY: 2 }));
65-
mountedTree.instance().onTouchMove(getTouchEvent({ pageX: 3, pageY: 2 }));
65+
instance.componentDidMount();
66+
instance.onTouchStart(getTouchEvent({ pageX: 1, pageY: 2 }));
67+
instance.onTouchMove(getTouchEvent({ pageX: 3, pageY: 2 }));
6668
mountedTree.update();
6769

6870
const childComponent = mountedTree.find(GenericSpanComponent);
@@ -86,7 +88,9 @@ describe('ReactCursorPosition', () => {
8688

8789
it('decorates child components with props in the mouse environment', (done) => {
8890
const mountedTree = getMountedComponentTree();
89-
mountedTree.instance().onMouseEnter(getMouseEvent());
91+
const instance = mountedTree.instance();
92+
instance.componentDidMount();
93+
instance.onMouseEnter(getMouseEvent());
9094

9195
defer(() => {
9296
mountedTree.update();
@@ -138,6 +142,7 @@ describe('ReactCursorPosition', () => {
138142
const positionObserver = getMountedComponentTree();
139143
const instance = positionObserver.instance();
140144
sinon.spy(instance, 'init');
145+
instance.componentDidMount();
141146
instance.onTouchStart(touchEvent);
142147

143148
instance.onMouseEnter(mouseEvent);
@@ -233,6 +238,7 @@ describe('ReactCursorPosition', () => {
233238
const renderedTree = getMountedComponentTree({ isActivatedOnTouch: true });
234239
const instance = renderedTree.instance();
235240

241+
instance.componentDidMount();
236242
instance.onTouchStart(touchEvent);
237243
renderedTree.update();
238244

@@ -247,8 +253,10 @@ describe('ReactCursorPosition', () => {
247253
describe('Mouse Environment', () => {
248254
it('decorates child components with element dimensions', () => {
249255
const renderedTree = getMountedComponentTree();
256+
const instance = renderedTree.instance();
250257

251-
renderedTree.instance().onMouseEnter(getTouchEvent());
258+
instance.componentDidMount();
259+
instance.onMouseEnter(getTouchEvent());
252260
renderedTree.update();
253261

254262
const childComponent = renderedTree.find(GenericSpanComponent);
@@ -366,6 +374,7 @@ describe('ReactCursorPosition', () => {
366374

367375
expect(childComponent.props().isPositionOutside).to.be.true;
368376

377+
instance.componentDidMount();
369378
instance.onTouchStart(touchEvent);
370379
renderedTree.update();
371380

@@ -400,6 +409,7 @@ describe('ReactCursorPosition', () => {
400409
});
401410
const instance = renderedTree.instance();
402411

412+
instance.componentDidMount();
403413
instance.onMouseEnter(mouseEvent);
404414
instance.onMouseMove(getMouseEvent({
405415
pageX: 1,
@@ -439,6 +449,7 @@ describe('ReactCursorPosition', () => {
439449
});
440450
const instance = renderedTree.instance();
441451

452+
instance.componentDidMount();
442453
instance.onMouseEnter(mouseEvent);
443454
instance.onMouseMove(getMouseEvent({
444455
pageX: 5,
@@ -582,9 +593,11 @@ describe('ReactCursorPosition', () => {
582593
mapChildProps,
583594
isActivatedOnTouch: true
584595
});
596+
const instance = tree.instance();
585597

586-
tree.instance().onTouchStart(touchEvent);
587-
tree.instance().onTouchMove(touchEvent);
598+
instance.componentDidMount();
599+
instance.onTouchStart(touchEvent);
600+
instance.onTouchMove(touchEvent);
588601
tree.update();
589602

590603
const childComponent = tree.find(GenericSpanComponent);
@@ -608,9 +621,11 @@ describe('ReactCursorPosition', () => {
608621
isActivatedOnTouch: true,
609622
onPositionChanged: spy
610623
});
611-
tree.instance().onTouchStart(touchEvent);
624+
const instance = tree.instance();
612625

613-
tree.instance().onTouchMove(getTouchEvent({ pageX: 2, pageY: 3}));
626+
instance.componentDidMount();
627+
instance.onTouchStart(touchEvent);
628+
instance.onTouchMove(getTouchEvent({ pageX: 2, pageY: 3}));
614629

615630
expect(spy.args[1][0]).to.deep.equal({
616631
detectedEnvironment: {

0 commit comments

Comments
 (0)