Skip to content

Commit

Permalink
Add async actions test
Browse files Browse the repository at this point in the history
  • Loading branch information
didierfranc committed Jun 3, 2018
1 parent 1ba528d commit 647bbd4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
"version": "4.0.0-beta.2",
"description": "",
"main": "index.js",
"files": ["index.js", "package.json", "README.md", "dist/"],
"files": [
"index.js",
"package.json",
"README.md",
"dist/"
],
"scripts": {
"build": "rollup -c -m && yarn gen-flow",
"gen-flow": "cp src/types.js dist/react-waterfall.js.flow",
Expand Down Expand Up @@ -42,6 +47,7 @@
"eslint-plugin-react": "^7.8.2",
"flow-bin": "^0.72.0",
"jest": "^22.4.4",
"node-fetch": "^2.1.2",
"react": "^16.3.2",
"react-dom": "^16.3.2",
"react-test-renderer": "^16.3.2",
Expand Down
30 changes: 29 additions & 1 deletion src/__test__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
/* global store */
import React from 'react'
import renderer from 'react-test-renderer'
import fetch from 'node-fetch'

import createStore from '../'

global.console = { error: jest.fn() }
global.console = { ...console, error: jest.fn() }
global.fetch = fetch

const REPO = 'https://api.github.com/repos/didierfranc/react-waterfall'

beforeEach(() => {
const config = {
initialState: {
count: 0,
stars: null,
},
actionsCreators: {
increment: ({ count }) => ({ count: count + 1 }),
getStars: async () => {
const { stargazers_count: stars } = await fetch(REPO).then(r =>
r.json())
return { stars }
},
},
}

Expand Down Expand Up @@ -61,3 +71,21 @@ test('actions triggered and state updated', () => {
actions.increment()
expect(tree.toJSON()).toMatchSnapshot()
})

test('async actions', async () => {
const { Provider, connect, actions } = store

const Stars = connect(({ stars }) => ({ stars }))(({ stars }) => stars)

const App = () => (
<Provider>
<Stars />
</Provider>
)

const tree = renderer.create(<App />)
await actions.getStars()

const instance = tree.root.findByType(Stars).children[0]
expect(typeof instance.props.stars).toBe('number')
})
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3240,6 +3240,10 @@ node-fetch@^1.0.1:
encoding "^0.1.11"
is-stream "^1.0.1"

node-fetch@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.1.2.tgz#ab884e8e7e57e38a944753cec706f788d1768bb5"

node-int64@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"
Expand Down

0 comments on commit 647bbd4

Please sign in to comment.