Skip to content

Commit

Permalink
only call onResize observer once when mounting
Browse files Browse the repository at this point in the history
  • Loading branch information
souporserious committed Apr 13, 2019
1 parent 8a6abd9 commit dea8f0c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
12 changes: 6 additions & 6 deletions src/__tests__/Measure.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('Measure', () => {
container = document.createElement('div')
})

it('should handel entry', () => {
it('should handle entry', () => {
const ref = createRef()
render(<MeasureWith innerRef={ref} />, container)
expect(container.firstChild).toMatchSnapshot()
Expand All @@ -42,18 +42,18 @@ describe('Measure', () => {
])
jest.runAllTimers()

expect(defaultChildrenFn).toHaveBeenCalledTimes(2)
expect(defaultChildrenFn).toHaveBeenCalledTimes(1)
expect(container.firstChild).toMatchSnapshot()
})

it('should handel bounds', () => {
it('should handle bounds', () => {
render(<MeasureWith bounds />, container)
expect(container.firstChild).toMatchSnapshot()

resizeObserver.callback()
jest.runAllTimers()

expect(defaultChildrenFn).toHaveBeenCalledTimes(2)
expect(defaultChildrenFn).toHaveBeenCalledTimes(1)
expect(container.firstChild).toMatchSnapshot()
})

Expand All @@ -62,7 +62,7 @@ describe('Measure', () => {
render(<MeasureWith />, container)
expect(resizeObserver.observe).toHaveBeenCalledTimes(1)
})
it('should always un observer before observing next one', () => {
it('should always unobserve before observing next one', () => {
const ref = createRef()

render(<MeasureWith innerRef={ref} children={() => null} />, container)
Expand Down Expand Up @@ -101,7 +101,7 @@ describe('Measure', () => {
resizeObserver.callback()
jest.runAllTimers()

expect(onResize).toHaveBeenCalledTimes(2)
expect(onResize).toHaveBeenCalledTimes(1)
})
})
})
32 changes: 16 additions & 16 deletions src/__tests__/__snapshots__/Measure.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Measure should handel bounds 1`] = `
exports[`Measure should handle bounds 1`] = `
<div>
{
"contentRect": {
Expand All @@ -15,24 +15,22 @@ exports[`Measure should handel bounds 1`] = `
</div>
`;

exports[`Measure should handel bounds 2`] = `
exports[`Measure should handle bounds 2`] = `
<div>
{
"contentRect": {
"bounds": {
"top": 0,
"right": 0,
"bottom": 0,
"left": 0,
"width": 0,
"height": 0
}
"entry": {},
"client": {},
"offset": {},
"scroll": {},
"bounds": {},
"margin": {}
}
}
</div>
`;

exports[`Measure should handel entry 1`] = `
exports[`Measure should handle entry 1`] = `
<div>
{
"contentRect": {
Expand All @@ -47,14 +45,16 @@ exports[`Measure should handel entry 1`] = `
</div>
`;

exports[`Measure should handel entry 2`] = `
exports[`Measure should handle entry 2`] = `
<div>
{
"contentRect": {
"entry": {
"width": 0,
"height": 0
}
"entry": {},
"client": {},
"offset": {},
"scroll": {},
"bounds": {},
"margin": {}
}
}
</div>
Expand Down
10 changes: 5 additions & 5 deletions src/with-content-rect.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,16 @@ function withContentRect(types) {

_node = null

_firstResize = true

componentDidMount() {
this._resizeObserver = new ResizeObserver(this.measure)
if (this._node !== null) {
this._resizeObserver.observe(this._node)

if (typeof this.props.onResize === 'function') {
this.props.onResize(
getContentRect(
this._node,
types || getTypes(this.props)
)
getContentRect(this._node, types || getTypes(this.props))
)
}
}
Expand All @@ -70,12 +69,13 @@ function withContentRect(types) {
}

this._animationFrameID = window.requestAnimationFrame(() => {
if (this._resizeObserver !== null) {
if (this._resizeObserver !== null && !this._firstResize) {
this.setState({ contentRect })
if (typeof this.props.onResize === 'function') {
this.props.onResize(contentRect)
}
}
this._firstResize = false
})
}

Expand Down

0 comments on commit dea8f0c

Please sign in to comment.