Skip to content

Commit

Permalink
Merge pull request #18 from lionng429/fix-issue-17
Browse files Browse the repository at this point in the history
Fixed wrapper existence check in Receiver constructor
  • Loading branch information
lionng429 authored Mar 27, 2018
2 parents 825e1dc + 751adde commit 4c7cbf2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-file-uploader",
"version": "0.4.0",
"version": "0.4.1",
"description": "A set of file-upload-components with React.js.",
"main": "lib/index.js",
"scripts": {
Expand Down
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 4c7cbf2

Please sign in to comment.