Skip to content

Commit

Permalink
fix: Do not call onResize twice on mount (#15)
Browse files Browse the repository at this point in the history
Closes #9
  • Loading branch information
ValentinH authored and okonet committed Mar 13, 2017
1 parent 4c64e4f commit b73c96c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export default class ContainerDimensions extends Component {

componentDidMount() {
this.parentNode = ReactDOM.findDOMNode(this).parentNode
this.elementResizeDetector = elementResizeDetectorMaker({ strategy: 'scroll' })
this.elementResizeDetector = elementResizeDetectorMaker({
strategy: 'scroll',
callOnAdd: false
})
this.elementResizeDetector.listenTo(this.parentNode, this.onResize)
this.onResize()
}
Expand Down
2 changes: 1 addition & 1 deletion testSetup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const jsdom = require('jsdom').jsdom

global.document = jsdom('<!doctype html><html><body></body></html>')
global.document = jsdom('<!doctype html><html><body><div id="root"></div></body></html>')
global.window = document.defaultView

Object.keys(document.defaultView).forEach((property) => {
Expand Down
8 changes: 4 additions & 4 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('react-container-dimensions', () => {
<MyComponent />
</ContainerDimensions>
</div>
, { attachTo: document.body })
, { attachTo: document.getElementById('root') })
expect(wrapper.find('div').length).to.eq(1)
expect(ContainerDimensions.prototype.componentDidMount.calledOnce).to.be.true
ContainerDimensions.prototype.componentDidMount.restore()
Expand All @@ -49,15 +49,15 @@ describe('react-container-dimensions', () => {
ContainerDimensions.prototype.onResize.restore()
})

it('calls onResize when parent has been resized', (done) => {
xit('calls onResize when parent has been resized', (done) => {
spy(ContainerDimensions.prototype, 'onResize')
const wrapper = mount(
<div ref="node" id="node" style={{ width: 10 }}>
<ContainerDimensions>
<MyComponent />
</ContainerDimensions>
</div>
, { attachTo: document.body })
, { attachTo: document.getElementById('root') })
const el = wrapper.render()
el.css('width', 10)
setTimeout(() => {
Expand Down Expand Up @@ -85,7 +85,7 @@ describe('react-container-dimensions', () => {
<MyComponent />
</ContainerDimensions>
</div>
, { attachTo: document.body })
, { attachTo: document.getElementById('root') })

wrapper.render()
expect(wrapper.find(MyComponent).props()).to.have.keys([
Expand Down

0 comments on commit b73c96c

Please sign in to comment.