Skip to content

Instantly share code, notes, and snippets.

@brunolm
Last active August 13, 2017 21:08
Show Gist options
  • Save brunolm/28e26596a1a6c11b5eac97dbadda0d5c to your computer and use it in GitHub Desktop.
Save brunolm/28e26596a1a6c11b5eac97dbadda0d5c to your computer and use it in GitHub Desktop.
react-native test example with jest
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { View, Text } from 'react-native';
export default class Header extends Component {
static propTypes = {
user: PropTypes.object,
}
render() {
return (
<View>
<Text key={ 'user' }>
{ this.props.user.name }
</Text>
</View>
);
}
}
import React from 'react';
import ReactShallowRenderer from 'react-test-renderer/shallow';
import { Header } from './';
describe('HeaderComponent', () => {
/** @type {ReactShallowRenderer} */
let renderer;
let componentJSON;
beforeEach(() => {
renderer = new ReactShallowRenderer();
renderer.render(
<Header user={ { name: 'Bruno' } } />
);
componentJSON = renderer.getRenderOutput();
});
describe('Header on top', () => {
it('should create component', () => {
expect(componentJSON).toBeDefined();
});
describe('Component elements', () => {
it('should display the name', () => {
const usuario = componentJSON.props.children.find(child => child.key === 'user');
expect(usuario.props.children).toBe('Bruno');
});
});
});
});
global.XMLHttpRequest = require('xhr2');
"jest": {
"preset": "jest-expo",
"setupFiles": [
"./jest.setup.js"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment