From 647bbd4d74f7c37137f1f64c5b3dc65a009ffa41 Mon Sep 17 00:00:00 2001 From: Didier Franc Date: Sun, 3 Jun 2018 22:02:16 +0200 Subject: [PATCH] Add async actions test --- package.json | 8 +++++++- src/__test__/index.test.js | 30 +++++++++++++++++++++++++++++- yarn.lock | 4 ++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index b66f58c..56373df 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/__test__/index.test.js b/src/__test__/index.test.js index f6cc54e..f7e6257 100644 --- a/src/__test__/index.test.js +++ b/src/__test__/index.test.js @@ -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 } + }, }, } @@ -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 = () => ( + + + + ) + + const tree = renderer.create() + await actions.getStars() + + const instance = tree.root.findByType(Stars).children[0] + expect(typeof instance.props.stars).toBe('number') +}) diff --git a/yarn.lock b/yarn.lock index 610aa25..b75a2bd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"