From 9c2bb2a5a6fa0489f1c8e3185b00adb56d27213a Mon Sep 17 00:00:00 2001 From: Martin Bielik Date: Tue, 17 May 2016 12:52:47 +0200 Subject: [PATCH] jest tests setup --- .eslintrc | 4 +++- .gitignore | 1 + __mocks__/react-native.js | 35 ++++++++++++++++++++++++++++++++ __tests__/HelloComponent-test.js | 20 ++++++++++++++++++ __tests__/HelloComponent.js | 22 ++++++++++++++++++++ package.json | 19 +++++++++++++++-- 6 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 __mocks__/react-native.js create mode 100644 __tests__/HelloComponent-test.js create mode 100644 __tests__/HelloComponent.js diff --git a/.eslintrc b/.eslintrc index c54dc8d..077e609 100644 --- a/.eslintrc +++ b/.eslintrc @@ -26,7 +26,9 @@ "before": true, "beforeEach": true, "after": true, - "afterEach": true + "afterEach": true, + "jest": true, + "expect": true }, "rules": { diff --git a/.gitignore b/.gitignore index c5156dc..a6ffeee 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules *-key.keystore npm-debug.log +coverage diff --git a/__mocks__/react-native.js b/__mocks__/react-native.js new file mode 100644 index 0000000..78128ee --- /dev/null +++ b/__mocks__/react-native.js @@ -0,0 +1,35 @@ +const React = require('react'); +const ReactNative = React; + +ReactNative.StyleSheet = { + create: function create(styles) { + return styles; + } +}; + +class View extends React.Component { + render() { return false; } +} + +class ListView extends React.Component { + static DataSource() { + } +} + +class AppRegistry { + static registerComponent () { + } +} + +ReactNative.View = View; +ReactNative.ScrollView = View; +ReactNative.ListView = ListView; +ReactNative.Text = View; +ReactNative.TouchableOpacity = View; +ReactNative.TouchableHighlight = View; +ReactNative.TouchableWithoutFeedback = View; +ReactNative.ToolbarAndroid = View; +ReactNative.Image = View; +ReactNative.AppRegistry = AppRegistry; + +module.exports = ReactNative; \ No newline at end of file diff --git a/__tests__/HelloComponent-test.js b/__tests__/HelloComponent-test.js new file mode 100644 index 0000000..b83cf0f --- /dev/null +++ b/__tests__/HelloComponent-test.js @@ -0,0 +1,20 @@ +import React from 'react'; +import { View } from 'react-native'; +import utils from 'react-addons-test-utils'; + +jest.dontMock('./HelloComponent'); +const HelloComponent = require('./HelloComponent').default; + +describe('HelloComponent', () => { + + it('should render hello world', () => { + const renderer = utils.createRenderer(); + renderer.render( + + ); + const output = renderer.getRenderOutput(); + expect(output.type).toEqual(View); + expect(output.props.children.props.children).toBe("Hello world!"); + }); + +}); diff --git a/__tests__/HelloComponent.js b/__tests__/HelloComponent.js new file mode 100644 index 0000000..1b53a50 --- /dev/null +++ b/__tests__/HelloComponent.js @@ -0,0 +1,22 @@ +import React, { Component } from 'react'; +import { Text, View, StyleSheet } from 'react-native'; + +export default class HelloComponent extends Component { + render() { + return ( + + Hello world! + + ); + } +} + +var styles = StyleSheet.create({ + container: { + flex: 1, + flexDirection: 'row', + justifyContent: 'center', + alignItems: 'center', + backgroundColor: 'blue' + }, +}); diff --git a/package.json b/package.json index c5276a9..9f87fa4 100644 --- a/package.json +++ b/package.json @@ -7,13 +7,13 @@ "example": "examples" }, "scripts": { - "test": "mocha --compilers js:babel-core/register", + "test-mocha": "mocha --compilers js:babel-core/register", + "test": "rm -rf ./node_modules/jest-cli/.haste_cache && jest", "lint": "eslint ." }, "repository": { "type": "git", "url": "git+ssh://git@github.com:instea/react-native-popup-menu.git" - }, "author": "instea.co", "license": "ISC", @@ -21,15 +21,30 @@ "url": "https://github.com/instea/react-native-popup-menu/issues" }, "homepage": "https://github.com/instea/react-native-popup-menu", + "jest": { + "scriptPreprocessor": "/node_modules/babel-jest", + "testFileExtensions": [ "js" ], + "moduleFileExtensions": [ "js" ], + "unmockedModulePathPatterns": [ + "/node_modules/react", + "/node_modules/fbjs" + ], + "verbose": true, + "collectCoverage": true + }, "devDependencies": { "babel-core": "^6.8.0", "babel-eslint": "^6.0.4", + "babel-jest": "^12.0.2", "babel-preset-react": "^6.5.0", "babel-preset-react-native": "^1.7.0", "chai": "^3.5.0", "eslint": "^2.9.0", "eslint-plugin-react": "^5.1.1", + "jest-cli": "^12.0.2", "mocha": "^2.4.5", + "react": "^15.0.2", + "react-addons-test-utils": "^15.0.2", "sinon": "^1.17.4" } }