Skip to content
This repository has been archived by the owner on Jul 29, 2020. It is now read-only.

Commit

Permalink
Adds test for useStyles() hook
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredLunde committed Jul 15, 2019
1 parent ccfdb21 commit b5d2f83
Show file tree
Hide file tree
Showing 10 changed files with 3,316 additions and 61 deletions.
4 changes: 3 additions & 1 deletion .npmignore
Expand Up @@ -2,4 +2,6 @@
node_modules
examples
.babelrc
.idea
.idea
**/*.test.js
**/test.js
1 change: 1 addition & 0 deletions .python-version
@@ -0,0 +1 @@
2.7
38 changes: 30 additions & 8 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "curls",
"version": "3.1.1",
"version": "3.1.4",
"main": "dist/cjs/index.js",
"module": "dist/es/index.js",
"jsnext:main": "dist/es/index.js",
Expand All @@ -11,13 +11,22 @@
"private": false,
"sideEffects": false,
"devDependencies": {
"@babel/register": "^7.4.4",
"@essentials/benchmark": "^1.0.4",
"@stellar-apps/babel-preset-es": "^1.0.4",
"@stellar-apps/babel-preset-react": "^1.0.3",
"@testing-library/react-hooks": "^1.1.0",
"ava": "^2.2.0",
"babel-plugin-emotion": "^10.0.14",
"babel-plugin-polished": "^1.1.0",
"browser-env": "^3.2.6",
"jsdom": "^15.1.1",
"polished": "^3.4.1",
"prettier": "^1.18.2"
"prettier": "^1.18.2",
"prop-types": "^15.7.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-test-renderer": "^16.8.6"
},
"dependencies": {
"@babel/runtime": "^7.5.4",
Expand All @@ -34,19 +43,32 @@
"trie-memoize": "^1.0.7"
},
"scripts": {
"build": "npm run build:es && npm run build:cjs",
"build:es": "rm -rf dist/es && cross-env NODE_ENV=production BABEL_ENV=es babel src --out-dir dist/es && npm run prettier:es",
"build:cjs": "rm -rf dist/cjs && cross-env NODE_ENV=production BABEL_ENV=cjs babel src --out-dir dist/cjs && npm run prettier:cjs",
"build": "yarn build:es && yarn build:cjs",
"build:es": "rm -rf dist/es && cross-env NODE_ENV=production BABEL_ENV=es babel src --out-dir dist/es",
"build:cjs": "rm -rf dist/cjs && cross-env NODE_ENV=production BABEL_ENV=cjs babel src --out-dir dist/cjs",
"watch": "cross-env NODE_ENV=production BABEL_ENV=es babel ./src -w --out-dir dist/es",
"prettier:es": "prettier --single-quote --no-semi --no-bracket-spacing --trailing-comma es5 --write --tab-width 2 \"dist/es/**/*.js\"",
"prettier:cjs": "prettier --single-quote --no-semi --no-bracket-spacing --trailing-comma es5 --write --tab-width 2 \"dist/cjs/**/*.js\"",
"prettier": "yarn prettier:cjs && yarn prettier:es",
"prettier:es": "prettier --single-quote --no-bracket-spacing --trailing-comma es5 --write --tab-width 2 \"dist/es/**/*.js\"",
"prettier:cjs": "prettier --single-quote --no-bracket-spacing --trailing-comma es5 --write --tab-width 2 \"dist/cjs/**/*.js\"",
"publish:dev": "yarn publish --tag dev",
"publish:next": "yarn publish --tag next",
"prepublishOnly": "yarn build"
"prepublishOnly": "yarn test && yarn build && yarn prettier",
"test": "yarn build:cjs && BABEL_ENV=cjs ava -v"
},
"peerDependencies": {
"prop-types": ">= 15.6.0",
"react": ">= 16.8.0",
"react-dom": ">= 16.8.0"
},
"ava": {
"babel": false,
"compileEnhancements": false,
"files": [
"dist/cjs/**/*.test.js",
"dist/cjs/**/test.js"
],
"require": [
"@babel/register"
]
}
}
17 changes: 8 additions & 9 deletions src/ThemeConsumer/ThemeConsumer.js
Expand Up @@ -22,12 +22,10 @@ const mergeGlobals = (name, defaultTheme, {userTheme, theme}) => {
if (name === void 0)
return theme
else {
const componentTheme = getTheme(
defaultTheme,
mergeGlobals_(userTheme, getTheme(defaultTheme, userTheme[name]))
)

theme[name] = getTheme(defaultTheme, userTheme[name])
const
nextTheme = getTheme(defaultTheme, userTheme[name]),
componentTheme = getTheme(defaultTheme, mergeGlobals_(userTheme, nextTheme))
theme[name] = nextTheme
return componentTheme
}
}
Expand All @@ -45,9 +43,10 @@ export default props => {
}

export const useTheme = (name, defaultTheme) => {
if (typeof name === 'object')
name = options.name
defaultTheme = options.defaultTheme
if (typeof name === 'object') {
name = name.name
defaultTheme = name.defaultTheme
}

return mergeGlobals(name, defaultTheme, useContext(CurlsContext))
}
8 changes: 8 additions & 0 deletions src/createElement.test.js
@@ -0,0 +1,8 @@
require('browser-env')()
import test from 'ava'
import createElement from './createElement'


test('createElement', t => {
t.pass()
})
19 changes: 19 additions & 0 deletions src/testUtils.js
@@ -0,0 +1,19 @@
import React from 'react'
import {renderHook} from '@testing-library/react-hooks'
import {CurlsContext, createTheme} from './ThemeProvider'


export const renderHookWithTheme = (children, userTheme = {}) => {
const theme = createTheme(userTheme)
return renderHook(
children,
{
wrapper: ({children}) => (
<CurlsContext.Provider
value={{theme, userTheme: theme}}
children={children}
/>
),
},
)
}
7 changes: 4 additions & 3 deletions src/useStyles.js
Expand Up @@ -86,13 +86,14 @@ export default (props, options = emptyObj) => {

let
nextProps = assignOrdered(theme.defaultProps, kind, props),
derivedStyles = typeof styles === 'object' ? getStyles(styles, theme, nextProps) : void 0
derivedStyles = typeof styles === 'object' ? getStyles(styles, theme, nextProps) : void 0,
styleProps = withoutStyles(styles)

// This seems a little bit fucky having this derivedStyles check twice, but the intent is to
// do the least work possible. That means calling Object.assign as little as possible. Without
// this order, it's possible we could assign new props twice when we really only need to once.
if (derivedStyles !== void 0)
nextProps = objectWithoutProps(nextProps, withoutStyles(styles))
nextProps = objectWithoutProps(nextProps, styleProps)
nextProps = maybeAddCssProp(props, nextProps, defaultStyles)
nextProps = maybeAddCssProp(props, nextProps, kindCss)

Expand All @@ -105,5 +106,5 @@ export default (props, options = emptyObj) => {
nextProps.css = maybeUnshiftCssArray(nextProps.css, derivedStyles)
}

return nextProps === props ? Object.assign({}, props) : nextProps
return nextProps === props ? objectWithoutProps(nextProps, styleProps) : nextProps
}

0 comments on commit b5d2f83

Please sign in to comment.