Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: getOuterSizes returning NaN for virtual elements (#711)
When the element is not attach at the DOM the `getComputedStyle` returns a empty string making the convertion to float `parseFloat(styles.marginTop)` return NaN.
- Loading branch information
Showing
2 changed files
with
49 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import chai from 'chai'; | ||
const { expect } = chai; | ||
import getOuterSizes from '../../src/utils/getOuterSizes'; | ||
|
||
describe('utils/getOuterSizes', () => { | ||
let node; | ||
|
||
beforeEach(() => { | ||
node = document.createElement('div'); | ||
}); | ||
|
||
describe('when the element is not attach on the DOM', () => { | ||
it('should returns 0 width and height for empty elements', () => { | ||
expect(getOuterSizes(node)).to.deep.equal({ | ||
width: 0, | ||
height: 0, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when the element is attach on the DOM', () => { | ||
it('should returns width and height for elements without margins', () => { | ||
node.style.width = '20px'; | ||
node.style.height = '20px'; | ||
document.body.appendChild(node); | ||
node.style.position = 'relative'; | ||
|
||
expect(getOuterSizes(node)).to.deep.equal({ | ||
width: 20, | ||
height: 20, | ||
}); | ||
}); | ||
|
||
it('should returns width and height for elements with margins', () => { | ||
node.style.width = '20px'; | ||
node.style.height = '20px'; | ||
node.style.margin = '10px'; | ||
document.body.appendChild(node); | ||
node.style.position = 'relative'; | ||
|
||
expect(getOuterSizes(node)).to.deep.equal({ | ||
width: 40, | ||
height: 40, | ||
}); | ||
}); | ||
}); | ||
}); |