-
-
Save cornedor/37ab65a3a505797b01c2e78156d5cf12 to your computer and use it in GitHub Desktop.
Unit test React Native with Mocha
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--compilers js:./test/support/compiler | |
--require ./test/support/init |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-env node, mocha */ | |
import NoteEntryScreen from '../src/containers/screens/NoteEntryScreen'; | |
import common from './support/common'; | |
const {createRenderer, React, expect, MockComponents} = common; | |
function setup() { | |
const props = {}; | |
const renderer = createRenderer(); | |
renderer.render(<NoteEntryScreen {...props} />); | |
const output = renderer.getRenderOutput(); | |
return { | |
props, | |
output, | |
renderer, | |
}; | |
} | |
describe('NoteEntryScreen', () => { | |
it('should render a scrollview', () => { | |
const { output } = setup(); | |
expect(output.type.displayName).to.equal(MockComponents.ScrollView.displayName); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import chai from 'chai'; | |
import chaiAsPromised from 'chai-as-promised'; | |
import React from 'react'; | |
import ReactTestUtils from 'react-addons-test-utils'; | |
import {MockComponents} from './mocks/react-native'; | |
chai.use(chaiAsPromised); | |
export default { | |
expect: chai.expect, | |
createRenderer: ReactTestUtils.createRenderer, | |
React, | |
MockComponents, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var path = require('path'); | |
var babel = require('babel-core'); | |
var origJs = require.extensions['.js']; | |
require.extensions['.js'] = function (module, fileName) { | |
var output; | |
if (fileName === '/app/node_modules/react-native/Libraries/react-native/react-native.js') { | |
fileName = path.resolve('./test/support/mocks/react-native.js'); | |
} | |
if (fileName.indexOf('node_modules/') >= 0) { | |
return (origJs || require.extensions['.js'])(module, fileName); | |
} | |
var src = fs.readFileSync(fileName, 'utf8'); | |
output = babel.transform(src, { | |
filename: fileName, | |
sourceFileName: fileName, | |
presets: ['react-native'], | |
}).code; | |
return module._compile(output, fileName); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
function mockComponent(type) { | |
const Component = React.createClass({ | |
displayName: type, | |
propTypes: { children: React.PropTypes.node }, | |
render() { return React.createElement(React.DOM.div, this.props, this.props.children); }, | |
}); | |
return Component; | |
} | |
const componentsToMock = [ | |
'View', | |
'Text', | |
'Component', | |
'ScrollView', | |
'TextInput', | |
]; | |
export const MockComponents = componentsToMock.reduce((agg, type) => { | |
agg[type] = mockComponent(type); | |
return agg; | |
}, {}); | |
export default { | |
...React, | |
...MockComponents, | |
StyleSheet: { | |
create: (ss) => ss, | |
}, | |
PropTypes: React.PropTypes, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment