Skip to content

Commit

Permalink
Merge pull request #1 from dimapaloskin/add-tests
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
dimapaloskin authored Nov 11, 2018
2 parents 6d0b844 + 26e3c8f commit 62a3e2e
Show file tree
Hide file tree
Showing 5 changed files with 3,360 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"presets": ["@babel/preset-react", "@babel/preset-flow"]
"presets": ["@babel/preset-react", "@babel/preset-env", "@babel/preset-flow"]
}
11 changes: 11 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[ignore]

[include]

[libs]

[lints]

[options]

[strict]
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
"license": "MIT",
"scripts": {
"build": "rollup -c rollup.config.js",
"test": "jest",
"lint": "eslint ./src"
},
"devDependencies": {
"@babel/core": "^7.1.5",
"@babel/preset-env": "^7.1.5",
"@babel/preset-flow": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"babel-core": "^7.0.0-bridge",
"babel-eslint": "^10.0.1",
"babel-jest": "^23.6.0",
"eslint": "^5.8.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-flowtype": "^3.2.0",
Expand All @@ -24,6 +28,9 @@
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-react": "^7.11.1",
"eslint-plugin-standard": "^4.0.0",
"flow-bin": "^0.86.0",
"jest": "^23.6.0",
"react-test-renderer": "^16.7.0-alpha.0",
"rollup": "^0.67.0",
"rollup-plugin-babel": "^4.0.3"
},
Expand Down
40 changes: 40 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React, { useState } from 'react'
import renderer from 'react-test-renderer'
import { createSharedHook, withSharedHooks, useSharedHooksState } from '../src'

test('should update shared state in all dependent components', () => {
const counter = createSharedHook(useState, { count: 0 })

const Counter = () => {
const [state] = useSharedHooksState(state => state.counter)
return state.count.toString()
}

const Increment = () => {
const [, setState] = useSharedHooksState(state => state.counter)

return <button onClick={() => setState({ count: 1 })}>+</button>
}

const Component = () => (
<React.Fragment>
<Increment />
<Counter />
<Counter />
</React.Fragment>
)

const WithSharedHooks = withSharedHooks({ counter })(Component)

const component = renderer.create(<WithSharedHooks />)
const instance = component.root

const [, ...before] = component.toJSON()
expect(before).toEqual(['0', '0'])

const button = instance.find(el => el.type === 'button')
button.props.onClick()

const [, ...after] = component.toJSON()
expect(after).toEqual(['1', '1'])
})
Loading

0 comments on commit 62a3e2e

Please sign in to comment.