-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: unit test fixes w/ ugly manual mock
- Loading branch information
David Atchley
committed
Mar 15, 2017
1 parent
0af727c
commit 987a857
Showing
8 changed files
with
136 additions
and
71 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
{ "presets": ["es2015", "react"] } | ||
{ | ||
"presets": ["es2015", "react"], | ||
"plugins": ["transform-object-rest-spread"] | ||
} |
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
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,94 @@ | ||
// __mocks__/utils.js | ||
import values from 'lodash/values'; | ||
|
||
const utils = require.requireActual('../utils'); | ||
|
||
const elementMap = {}; | ||
|
||
// Takes an object with the following properties | ||
// { 'parent': { width, height, top, left }, ... } | ||
const __setMockElements = (mapping) => { | ||
/* eslint-disable no-param-reassign */ | ||
const elements = Object.keys(mapping).reduce((acc, key) => { | ||
const styles = mapping[key].styles || {}; | ||
acc[key] = Object.assign({}, mapping[key], { | ||
clientWidth: mapping[key].width || 0, | ||
clientHeight: mapping[key].height || 0, | ||
offsetWidth: mapping[key].width || 0, | ||
offsetHeight: mapping[key].height || 0, | ||
scrollWidth: mapping[key].width || 0, | ||
scrollHeight: mapping[key].height || 0, | ||
_styles: {}, | ||
style: { | ||
lineHeight: '1', | ||
fontSize: '16px', | ||
...styles | ||
}, | ||
getBoundingClientRect() { | ||
return { | ||
top: mapping[key].top || 0, | ||
bottom: this.offsetHeight, | ||
left: mapping[key].left || 0, | ||
right: this.offsetWidth | ||
}; | ||
} | ||
}); | ||
|
||
// Treat fontSize special, inc/dec the width for testing purposes | ||
acc[key]._styles.fontSize = styles.fontSize || '16px'; | ||
/* eslint-disable object-shorthand */ | ||
Object.defineProperty(acc[key].style, 'fontSize', { | ||
get: function() { return elementMap[key]._styles.fontSize; }, | ||
set: function(val) { | ||
const curSize = parseFloat(elementMap[key]._styles.fontSize); | ||
const newSize = parseFloat(val); | ||
const adj = (newSize - curSize) / 10; | ||
if (curSize < newSize) { | ||
elementMap[key].offsetWidth += adj; | ||
elementMap[key].clientWidth += adj; | ||
elementMap[key].scrollWidth += adj; | ||
} | ||
if (curSize > newSize) { | ||
elementMap[key].offsetWidth += adj; | ||
elementMap[key].clientWidth += adj; | ||
elementMap[key].scrollWidth += adj; | ||
} | ||
elementMap[key]._styles.fontSize = val; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
return acc; | ||
}, {}); | ||
|
||
Object.assign(elementMap, elements); | ||
}; | ||
utils.__setMockElements = __setMockElements; | ||
|
||
utils.__getMockElement = (key) => elementMap[key]; | ||
|
||
utils.setRef = (name, context) => () => { | ||
context[name] = elementMap[name]; | ||
}; | ||
utils.setRef._isMock = true; | ||
|
||
utils.getStyle = (el, prop) => elementMap[el.className][utils.camelize(prop)]; | ||
|
||
const origGetOverflow = utils.getOverflow; | ||
utils.getOverflow = (/* parent, child */) => { | ||
const parent = elementMap.wrapper; | ||
const child = elementMap.content; | ||
return origGetOverflow(parent, child); | ||
}; | ||
|
||
// const origHasOverflow = utils.hasOverflow; | ||
utils.hasOverflow = (/* parent, child */) => { | ||
const parent = elementMap.wrapper; | ||
const child = elementMap.content; | ||
if (child.style.position && child.style.position === 'absolute') { | ||
return values(utils.getOverflow(parent, child)).some(v => v); | ||
} | ||
return (parent.clientWidth <= parent.scrollWidth || parent.clientHeight <= parent.scrollHeight); | ||
}; | ||
|
||
module.exports = utils; |
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
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