Skip to content

Commit be4c66e

Browse files
committed
Put some details in README
1 parent 8f887c4 commit be4c66e

File tree

8 files changed

+390
-6
lines changed

8 files changed

+390
-6
lines changed

.all-contributorsrc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"projectName": "react-hooks-testing-library",
3+
"projectOwner": "mpeyper",
4+
"repoType": "github",
5+
"repoHost": "https://github.com",
6+
"files": [
7+
"README.md"
8+
],
9+
"imageSize": 100,
10+
"commit": false,
11+
"contributors": [
12+
{
13+
"login": "mpeyper",
14+
"name": "Michael Peyper",
15+
"avatar_url": "https://avatars0.githubusercontent.com/u/23029903?v=4",
16+
"profile": "https://github.com/mpeyper",
17+
"contributions": [
18+
"code",
19+
"design",
20+
"doc",
21+
"ideas",
22+
"infra",
23+
"platform",
24+
"test",
25+
"tool"
26+
]
27+
}
28+
]
29+
}

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- "10"
4+
script:
5+
- npm run test

CODE_OF_CONDUCT.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as
6+
contributors and maintainers pledge to making participation in our project and
7+
our community a harassment-free experience for everyone, regardless of age, body
8+
size, disability, ethnicity, sex characteristics, gender identity and expression,
9+
level of experience, education, socio-economic status, nationality, personal
10+
appearance, race, religion, or sexual identity and orientation.
11+
12+
## Our Standards
13+
14+
Examples of behavior that contributes to creating a positive environment
15+
include:
16+
17+
- Using welcoming and inclusive language
18+
- Being respectful of differing viewpoints and experiences
19+
- Gracefully accepting constructive criticism
20+
- Focusing on what is best for the community
21+
- Showing empathy towards other community members
22+
23+
Examples of unacceptable behavior by participants include:
24+
25+
- The use of sexualized language or imagery and unwelcome sexual attention or
26+
advances
27+
- Trolling, insulting/derogatory comments, and personal or political attacks
28+
- Public or private harassment
29+
- Publishing others' private information, such as a physical or electronic
30+
address, without explicit permission
31+
- Other conduct which could reasonably be considered inappropriate in a
32+
professional setting
33+
34+
## Our Responsibilities
35+
36+
Project maintainers are responsible for clarifying the standards of acceptable
37+
behavior and are expected to take appropriate and fair corrective action in
38+
response to any instances of unacceptable behavior.
39+
40+
Project maintainers have the right and responsibility to remove, edit, or
41+
reject comments, commits, code, wiki edits, issues, and other contributions
42+
that are not aligned to this Code of Conduct, or to ban temporarily or
43+
permanently any contributor for other behaviors that they deem inappropriate,
44+
threatening, offensive, or harmful.
45+
46+
## Scope
47+
48+
This Code of Conduct applies both within project spaces and in public spaces
49+
when an individual is representing the project or its community. Examples of
50+
representing a project or community include using an official project e-mail
51+
address, posting via an official social media account, or acting as an appointed
52+
representative at an online or offline event. Representation of a project may be
53+
further defined and clarified by project maintainers.
54+
55+
## Enforcement
56+
57+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
58+
reported by contacting the project team at [INSERT EMAIL ADDRESS]. All
59+
complaints will be reviewed and investigated and will result in a response that
60+
is deemed necessary and appropriate to the circumstances. The project team is
61+
obligated to maintain confidentiality with regard to the reporter of an incident.
62+
Further details of specific enforcement policies may be posted separately.
63+
64+
Project maintainers who do not follow or enforce the Code of Conduct in good
65+
faith may face temporary or permanent repercussions as determined by other
66+
members of the project's leadership.
67+
68+
## Attribution
69+
70+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71+
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72+
73+
[homepage]: https://www.contributor-covenant.org
74+
75+
For answers to common questions about this code of conduct, see
76+
https://www.contributor-covenant.org/faq

README.md

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,164 @@
11
# react-hooks-testing-library
2+
3+
Simple component wrapper for testing React hooks.
4+
5+
---
6+
7+
[![Build Status](https://img.shields.io/travis/mpeyper/react-hooks-testing-library.svg?style=flat-square)](https://travis-ci.org/mpeyper/react-hooks-testing-library)
8+
[![version](https://img.shields.io/npm/v/react-hooks-testing-library.svg?style=flat-square)](https://www.npmjs.com/package/react-hooks-testing-library)
9+
[![downloads](https://img.shields.io/npm/dm/react-hooks-testing-library.svg?style=flat-square)](http://www.npmtrends.com/react-hooks-testing-library)
10+
[![MIT License](https://img.shields.io/npm/l/react-hooks-testing-library.svg?style=flat-square)](https://github.com/mpeyper/react-hooks-testing-library/blob/master/LICENSE.md)
11+
12+
[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors)
13+
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
14+
[![Code of Conduct](https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square)](https://github.com/mpeyper/react-hooks-testing-library/blob/master/CODE_OF_CONDUCT.md)
15+
16+
[![Watch on GitHub](https://img.shields.io/github/watchers/mpeyper/react-hooks-testing-library.svg?style=social)](https://github.com/mpeyper/react-hooks-testing-library/watchers)
17+
[![Star on GitHub](https://img.shields.io/github/stars/mpeyper/react-hooks-testing-library.svg?style=social)](https://github.com/mpeyper/react-hooks-testing-library/stargazers)
18+
[![Tweet](https://img.shields.io/twitter/url/https/github.com/mpeyper/react-hooks-testing-library.svg?style=social)](https://twitter.com/intent/tweet?text=Check%20out%20react-hooks-testing-library%20by%20%40mpeyper%20https%3A%2F%2Fgithub.com%2Fmpeyper%2Freact-hooks-testing-library%20%F0%9F%91%8D)
19+
20+
## The problem
21+
22+
You're writing an awesome custom hook and you want to test it, but as soon as you call it you see the following error:
23+
24+
> Invariant Violation: Hooks can only be called inside the body of a function component.
25+
26+
You don't really want to write a component solely for testing this hook and have to work out how you were going to trigger all the various ways the hook can be updated, especially given the complexities of how you've wired the whole thing together.
27+
28+
## The solution
29+
30+
The `react-hooks-testing-library` is build on top of the wonderful `react-testing-library` to create a simple test harness for React hooks that handles running them within the body of a function component, as well as privings various useful utility functions for updating the inputs and retrieving the outputs of your amazing custom hook.
31+
32+
Using this library, you do not have to concern yourself with how the to interact construct, render or interact with the react component in order to test your hook. You can just use the hook directly and assert the resulting values.
33+
34+
### When to use this library
35+
36+
1. You're writing a library with one or more custom hooks that are not directly tied a component
37+
2. You have a complex hook that is difficult to test through component interactions
38+
39+
### When not to use this library
40+
41+
1. Your hook is defined along side a component and is only used there
42+
2. Your hook is easy to test by just testing the components using it
43+
44+
## Example
45+
46+
```js
47+
// useTheme.js
48+
import { useState, createContext, useContext, useMemo } from 'react'
49+
50+
const themes = {
51+
light: { primaryLight: '#FFFFFF', primaryDark: '#000000' },
52+
dark: { primaryLight: '#000000', primaryDark: '#FFFFFF' }
53+
}
54+
55+
const ThemesContext = createContext(themes)
56+
57+
const useTheme = (initialTheme) => {
58+
const themes = useContext(ThemesContext)
59+
const [theme, setTheme] = useState(initialTheme)
60+
const changeTheme = () => {
61+
setTheme(theme === 'light' ? 'dark' : 'light')
62+
}
63+
return useMemo(() => ({ ...themes[theme], changeTheme }), [theme])
64+
}
65+
66+
// useTheme.test.js
67+
import { useHook, cleanup } from 'react-hooks-testing-library'
68+
69+
describe('custom hook tests', () => {
70+
afterEach(cleanup)
71+
72+
test('should use theme', () => {
73+
const { getCurrentValue } = useHook(() => useTheme('light'))
74+
75+
const theme = getCurrentValue()
76+
77+
expect(theme.primaryLight).toBe('#FFFFFF')
78+
expect(theme.primaryDark).toBe('#000000')
79+
})
80+
81+
test('should update theme', () => {
82+
const { getCurrentValue } = useHook(() => useTheme('light'))
83+
84+
const { changeTheme } = getCurrentValue()
85+
86+
changeTheme()
87+
88+
const theme = getCurrentValue()
89+
90+
expect(theme.primaryLight).toBe('#000000')
91+
expect(theme.primaryDark).toBe('#FFFFFF')
92+
})
93+
94+
test('should use custom theme', () => {
95+
const customThemes = {
96+
light: { primaryLight: '#AABBCC', primaryDark: '#CCBBAA' },
97+
dark: { primaryLight: '#CCBBAA', primaryDark: '#AABBCC' }
98+
}
99+
100+
const { getCurrentValue, addContextProvider } = useHook(() => useTheme('light'))
101+
102+
addContextProvider(ThemesContext, { value: customThemes })
103+
104+
const theme = getCurrentValue()
105+
106+
expect(theme.primaryLight).toBe('#AABBCC')
107+
expect(theme.primaryDark).toBe('#CCBBAA')
108+
})
109+
})
110+
```
111+
112+
## Installation
113+
114+
```sh
115+
npm install react-hooks-testing-library
116+
```
117+
118+
## Usage
119+
120+
```js
121+
// TODO: write this
122+
```
123+
124+
## Contributors
125+
126+
Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):
127+
128+
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
129+
<!-- prettier-ignore -->
130+
| [<img src="https://avatars0.githubusercontent.com/u/23029903?v=4" width="100px;"/><br /><sub><b>Michael Peyper</b></sub>](https://github.com/mpeyper)<br />[💻](https://github.com/mpeyper/react-hooks-testing-library/commits?author=mpeyper "Code") [🎨](#design-mpeyper "Design") [📖](https://github.com/mpeyper/react-hooks-testing-library/commits?author=mpeyper "Documentation") [🤔](#ideas-mpeyper "Ideas, Planning, & Feedback") [🚇](#infra-mpeyper "Infrastructure (Hosting, Build-Tools, etc)") [📦](#platform-mpeyper "Packaging/porting to new platform") [⚠️](https://github.com/mpeyper/react-hooks-testing-library/commits?author=mpeyper "Tests") [🔧](#tool-mpeyper "Tools") |
131+
| :---: |
132+
133+
<!-- ALL-CONTRIBUTORS-LIST:END -->
134+
135+
This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!
136+
137+
## Issues
138+
139+
_Looking to contribute? Look for the [Good First Issue](https://github.com/mpeyper/react-hooks-testing-library/issues?utf8=✓&q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3A"good+first+issue"+)
140+
label._
141+
142+
### 🐛 Bugs
143+
144+
Please file an issue for bugs, missing documentation, or unexpected behavior.
145+
146+
[**See Bugs**](https://github.com/mpeyper/react-hooks-testing-library/issues?q=is%3Aissue+is%3Aopen+label%3Abug+sort%3Acreated-desc)
147+
148+
### 💡 Feature Requests
149+
150+
Please file an issue to suggest new features. Vote on feature requests by adding
151+
a 👍. This helps maintainers prioritize what to work on.
152+
153+
[**See Feature Requests**](https://github.com/mpeyper/react-hooks-testing-library/issues?q=is%3Aissue+sort%3Areactions-%2B1-desc+label%3Aenhancement+is%3Aopen)
154+
155+
### ❓ Questions
156+
157+
For questions related to using the library, you can [raise issue here](https://github.com/mpeyper/react-hooks-testing-library/issues/new), or visit a support community:
158+
159+
- [Reactiflux on Discord](https://www.reactiflux.com/)
160+
- [Stack Overflow](https://stackoverflow.com/questions/tagged/react-testing-library)
161+
162+
## LICENSE
163+
164+
MIT

package-lock.json

Lines changed: 91 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"scripts": {
99
"build": "babel --out-dir lib src",
1010
"format": "prettier-eslint --write \"**/*.{js,json,md}\"",
11-
"test": "jest --coverage"
11+
"test": "jest --coverage",
12+
"contributors:add": "all-contributors add"
1213
},
1314
"dependencies": {
1415
"invariant": "^2.2.4",
@@ -22,6 +23,7 @@
2223
"@babel/plugin-transform-modules-commonjs": "^7.1.0",
2324
"@babel/preset-env": "^7.1.0",
2425
"@babel/preset-react": "^7.0.0",
26+
"all-contributors-cli": "^5.4.1",
2527
"babel-core": "^7.0.0-bridge.0",
2628
"babel-eslint": "^10.0.1",
2729
"babel-plugin-module-resolver": "^3.1.1",

0 commit comments

Comments
 (0)