Skip to content

Instantly share code, notes, and snippets.

@tdzl2003
Created October 27, 2016 06:12
Show Gist options
  • Save tdzl2003/9003cbb6959653976495937cbdfe5a1a to your computer and use it in GitHub Desktop.
Save tdzl2003/9003cbb6959653976495937cbdfe5a1a to your computer and use it in GitHub Desktop.
Test of react native component
import React from 'react';
import { shallow } from 'enzyme';
import {
View, Text,
StyleSheet,
} from 'react-native';
const styles = StyleSheet.create({
container: {
flex: 1,
},
title: {
fontSize: 20,
},
content: {
fontSize: 12,
}
});
class Target extends React.Component {
state = {gone: false};
render() {
const {gone} = this.state;
if (gone) {
return <View style={styles.container} />
}
return (
<View style={styles.container}>
<Text style={styles.title} onPress={()=>this.setState({gone: true})}>Hello</Text>
<Text style={styles.content}>World!</Text>
</View>
)
}
}
const result = shallow(<Target/>);
console.log(result.matchesElement(
<View style={styles.container}>
<Text style={styles.title}>Hello</Text>
<Text style={styles.content}>World!</Text>
</View>
));
result.find(Text).first().simulate('press');
console.log(result.matchesElement(
<View style={styles.container} />
));
{
"name": "react-test-study",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"babel-polyfill": "^6.16.0",
"babel-preset-react-native": "^1.9.0",
"babel-register": "^6.18.0",
"enzyme": "^2.5.1",
"react": "^15.3.2",
"react-addons-test-utils": "^15.3.2",
"react-dom": "^15.3.2",
"react-native": "^0.36.0",
"react-native-mock": "^0.2.7"
}
}
/**
* Created by tdzl2003 on 10/27/16.
*/
require("babel-polyfill");
var fs = require('fs');
var path = require('path');
global.__DEV__ = process.env.NODE_ENV !== 'production';
require('babel-register')({
ignore: filename => {
var dir = path.dirname(filename);
for (; dir !== path.dirname(dir); dir = path.dirname(dir)) {
if (path.basename(dir) === 'node_modules' || path.basename(dir) === 'lib') {
break;
}
if (fs.existsSync(path.join(dir, '.babelrc'))) {
return false;
}
}
return true;
},
});
require('react-native-mock/mock');
require('./index');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment