Skip to content

Commit

Permalink
Moved wrapper existence check to componentDidMount
Browse files Browse the repository at this point in the history
- as mentioned in #17, wrapper existence check does not work for wrapperId pointing to Receiver's parent wrapper. Moving the logic back to componentDidMount solves this issue.
  • Loading branch information
lionng429 committed Mar 27, 2018
1 parent 825e1dc commit bb6209b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
20 changes: 11 additions & 9 deletions src/Receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ class Receiver extends Component {
super(props);

this.wrapper = window;

if (props.wrapperId) {
this.wrapper = document.getElementById(props.wrapperId);
}

if (!this.wrapper) {
throw new Error(`wrapper element with Id ${props.wrapperId} not found.`);
}

this.onDragEnter = this.onDragEnter.bind(this);
this.onDragOver = this.onDragOver.bind(this);
this.onDragLeave = this.onDragLeave.bind(this);
Expand All @@ -37,6 +28,17 @@ class Receiver extends Component {
'Browser does not support DnD events or File API.'
);

const { wrapperId } = this.props;

if (wrapperId) {
invariant(
!!document.getElementById(wrapperId),
`wrapper element with Id ${wrapperId} not found.`
);

this.wrapper = document.getElementById(wrapperId);
}

this.wrapper.addEventListener('dragenter', this.onDragEnter);
this.wrapper.addEventListener('dragleave', this.onDragLeave);
this.wrapper.addEventListener('dragover', this.onDragOver);
Expand Down
16 changes: 15 additions & 1 deletion src/__tests__/Receiver-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jest.dontMock('../index');
jest.dontMock('classnames');

import React from 'react';
import { shallow, configure } from 'enzyme';
import { mount, shallow, configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import { jsdom } from 'jsdom';

Expand Down Expand Up @@ -92,6 +92,20 @@ describe('Receiver', () => {
/>
)).toThrow();
});

it('should not throw an error if wrapperId is given and the element exists', () => {
expect(() => mount((
<div id="wrapper">
<Receiver
wrapperId="wrapper"
onDragEnter={emptyFn}
onDragOver={emptyFn}
onDragLeave={emptyFn}
onFileDrop={emptyFn}
/>
</div>
), { attachTo: document.body })).not.toThrow();
});
});

describe('state of dragLevel', () => {
Expand Down

0 comments on commit bb6209b

Please sign in to comment.