Skip to content
This repository was archived by the owner on Apr 6, 2021. It is now read-only.

Commit 9f14cc4

Browse files
authored
added: minimum test (#8)
1 parent 7c9f87d commit 9f14cc4

File tree

6 files changed

+229
-43
lines changed

6 files changed

+229
-43
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules/
22
*.swp
33
*.swp
44
dist/
5+
*.log

__tests__/index.js

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,60 @@
1-
test.todo('StateProvider')
2-
test.todo('useGlobalState')
1+
import React from 'react'
2+
import {useGlobalState, StateProvider} from '../lib'
3+
import {fireEvent, render} from '@testing-library/react'
4+
5+
const Provider = jest.fn(StateProvider)
6+
7+
test('StateProvider & useGlobalState test', () => {
8+
9+
const Props ={
10+
reducer: (state, action) => {
11+
switch(action.type){
12+
case 'TEST': return {
13+
...state,
14+
test: true
15+
}
16+
default: return state
17+
}
18+
},
19+
initialState: {test: false}
20+
}
21+
22+
const ShowTest = () => {
23+
const [{test}] = useGlobalState()
24+
return (
25+
<span data-testid="showTest">
26+
{JSON.stringify(test)}
27+
</span>
28+
)
29+
}
30+
31+
const ChangeTest = () => {
32+
const [, dispatch] = useGlobalState()
33+
34+
const handleClick = () => {
35+
dispatch({
36+
type: 'TEST'
37+
})
38+
}
39+
40+
return (<button data-testid="changeTest" onClick={handleClick}>Change Test</button>)
41+
}
42+
43+
const {container, getByTestId } = render(
44+
<Provider {...Props}>
45+
<ShowTest/>
46+
<ChangeTest/>
47+
</Provider>
48+
)
49+
50+
const showTest = getByTestId('showTest')
51+
const changeTest = getByTestId('changeTest')
52+
53+
expect(Provider).toHaveBeenCalled()
54+
expect(showTest).toHaveTextContent('false')
55+
expect(container).toBeInTheDocument()
56+
57+
fireEvent.click(changeTest)
58+
expect(showTest).toHaveTextContent('true')
59+
60+
})

jest.config.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

jest.setup.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import '@testing-library/jest-dom/extend-expect'
2+
import '@testing-library/react/cleanup-after-each';

package.json

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,28 @@
77
"license": "MIT",
88
"private": false,
99
"peerDependencies": {
10-
"react": "^16.8.6"
10+
"react": "^16.8.0"
1111
},
1212
"files": [
1313
"lib",
14-
"dist",
15-
"README.md"
14+
"dist"
1615
],
16+
"jest": {
17+
"bail": true,
18+
"browser": true,
19+
"displayName": "global-state v0.0.1",
20+
"testEnvironment": "jest-environment-jsdom",
21+
"transform": {
22+
"^.+\\.[t|j]sx?$": "babel-jest"
23+
},
24+
"setupFilesAfterEnv": [
25+
"./jest.setup.js"
26+
],
27+
"testMatch": [
28+
"**/__tests__/**/*.js?(x)"
29+
],
30+
"verbose": true
31+
},
1732
"babel": {
1833
"presets": [
1934
[
@@ -23,7 +38,8 @@
2338
"node": true
2439
}
2540
}
26-
]
41+
],
42+
"@babel/preset-react"
2743
],
2844
"plugins": []
2945
},
@@ -40,14 +56,17 @@
4056
"lint": "eslint ./lib --fix",
4157
"prepare": "rm -rf dist && yarn test && yarn build",
4258
"release": "yarn -s prepare && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish",
43-
"test": "jest --config jest.config.js",
59+
"test": "jest",
4460
"test:watch": "jest --watch",
4561
"fixreadme": "node -e 'var fs=require(\"fs\");fs.writeFileSync(\"README.md\", fs.readFileSync(\"README.md\", \"utf8\").replace(/^- /gm, \"- \"))'",
4662
"docs": "documentation readme lib/*.js -q --section API && yarn fixreadme"
4763
},
4864
"devDependencies": {
4965
"@babel/core": "^7.5.5",
5066
"@babel/preset-env": "7.5.5",
67+
"@babel/preset-react": "7.0.0",
68+
"@testing-library/jest-dom": "4.0.0",
69+
"@testing-library/react": "8.0.7",
5170
"babel-jest": "24.8.0",
5271
"documentation": "12.0.3",
5372
"eslint": "6.1.0",
@@ -57,6 +76,8 @@
5776
"jest": "24.8.0",
5877
"microbundle": "0.11.0",
5978
"npm-run-all": "4.1.5",
79+
"react": "16.8.0",
80+
"react-dom": "16.8.6",
6081
"strip-json-comments-cli": "1.0.1"
6182
}
6283
}

0 commit comments

Comments
 (0)