-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from dimapaloskin/add-tests
Add tests
- Loading branch information
Showing
5 changed files
with
3,360 additions
and
46 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,3 +1,3 @@ | ||
{ | ||
"presets": ["@babel/preset-react", "@babel/preset-flow"] | ||
"presets": ["@babel/preset-react", "@babel/preset-env", "@babel/preset-flow"] | ||
} |
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,11 @@ | ||
[ignore] | ||
|
||
[include] | ||
|
||
[libs] | ||
|
||
[lints] | ||
|
||
[options] | ||
|
||
[strict] |
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,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']) | ||
}) |
Oops, something went wrong.