diff options
author | Francesco <firefelix@gmail.com> | 2015-06-19 13:23:52 +0200 |
---|---|---|
committer | Francesco <firefelix@gmail.com> | 2015-06-19 13:30:56 +0200 |
commit | dc8075314bdf86502663fe2dbf916910d0d2b6c5 (patch) | |
tree | 14f885457558ff9a3d49040ed119a4a4d8d46e74 | |
parent | d4867b2d0bc99f50781c8d577b054d034b567623 (diff) | |
download | react-autosize-textarea-dc8075314bdf86502663fe2dbf916910d0d2b6c5.zip react-autosize-textarea-dc8075314bdf86502663fe2dbf916910d0d2b6c5.tar.gz react-autosize-textarea-dc8075314bdf86502663fe2dbf916910d0d2b6c5.tar.bz2 |
Fix: was removing autosize listener with unmounted component
due to setTimeout() used needed by update event
-rw-r--r-- | karma.conf.js | 2 | ||||
-rw-r--r-- | src/TextareaAutosize.js | 13 | ||||
-rw-r--r-- | test/tests/TextareaAutosize-test.js | 51 |
3 files changed, 61 insertions, 5 deletions
diff --git a/karma.conf.js b/karma.conf.js index b716d94..d6ea1eb 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -15,7 +15,7 @@ module.exports = function (config) { browsers: [ 'Chrome' ], - singleRun: false, + singleRun: true, frameworks: [ 'mocha' ], diff --git a/src/TextareaAutosize.js b/src/TextareaAutosize.js index 09f67ff..01b24b4 100644 --- a/src/TextareaAutosize.js +++ b/src/TextareaAutosize.js @@ -31,10 +31,15 @@ const TextareaAutosize = React.createClass({ this.dispatchEvent(DESTROY); }, - dispatchEvent(EVENT_TYPE) { + dispatchEvent(EVENT_TYPE, defer) { const event = document.createEvent('Event'); event.initEvent(EVENT_TYPE, true, false); - setTimeout(() => this.refs.textarea.getDOMNode().dispatchEvent(event)); + const dispatch = () => this.refs.textarea.getDOMNode().dispatchEvent(event); + if (defer) { + setTimeout(dispatch); + } else { + dispatch(); + } }, getValue(props) { @@ -53,10 +58,10 @@ const TextareaAutosize = React.createClass({ componentWillReceiveProps(nextProps) { if (this.getValue(nextProps) !== this.getValue(this.props)) { - this.dispatchEvent(UPDATE); + this.dispatchEvent(UPDATE, true); } }, }); -module.exports = TextareaAutosize; +export default TextareaAutosize;
\ No newline at end of file diff --git a/test/tests/TextareaAutosize-test.js b/test/tests/TextareaAutosize-test.js new file mode 100644 index 0000000..4159a8e --- /dev/null +++ b/test/tests/TextareaAutosize-test.js @@ -0,0 +1,51 @@ +import React from 'react/addons'; +const TestUtils = React.addons.TestUtils; +import expect from 'expect'; +import TextareaAutosize from '../../src/TextareaAutosize'; + + +const renderTextarea = () => { + const component = + <div> + <TextareaAutosize className='textarea-autosize' /> + </div>; + const textareaWrapper = TestUtils.renderIntoDocument(component); + return textareaWrapper; +}; + +describe('TextareaAutosize', function() { + + it('should be displayed', function() { + const textareaWrapper = renderTextarea(); + const textarea = TestUtils.scryRenderedDOMComponentsWithClass(textareaWrapper, 'textarea-autosize'); + expect(textarea.length).toBe(1, 'textarea is not displayed'); + }); + + it('should display initial value', function() { + const initialValue = 'Initial Value' + const component = + <div> + <TextareaAutosize className='textarea-autosize' defaultValue={initialValue} /> + </div>; + const textareaWrapper = TestUtils.renderIntoDocument(component); + const textarea = TestUtils.findRenderedDOMComponentWithClass(textareaWrapper, 'textarea-autosize'); + const value = textarea.getDOMNode().innerHTML; + expect(value).toBe(initialValue, 'intial value is not displayed correctly'); + }); + + // it('should resize correctly based on initial value', function() { + // const initialValue = '\n\n\n\n\n\n\n\n\n\nInitial Value' + // const component = + // <div> + // <TextareaAutosize className='textarea-autosize' defaultValue={initialValue} /> + // </div>; + // const textareaWrapper = TestUtils.renderIntoDocument(component); + // const textarea = TestUtils.findRenderedDOMComponentWithClass(textareaWrapper, 'textarea-autosize').getDOMNode(); + // React.addons.TestUtils.Simulate.click(textarea); + // console.log(textarea.style.height); + // console.log(textarea.clientHeight); + // // const value = textarea.getDOMNode().innerHTML; + // // expect(value).toBe(initialValue, 'intial value is not displayed correctly'); + // }); + +});
\ No newline at end of file |