Slide 21
Slide 21 text
/* ~/__mocks__/react-native.js */
var React = module.exports = require('react');
const mockComponent = () => React.createClass({
render: () => null,
});
React.View = mockComponent();
React.Text = mockComponent();
React.StyleSheet = { create: x => x };
React.Image = mockComponent();
React.Picker = mockComponent();
React.TextInput = mockComponent();
React.TouchableOpacity = mockComponent();
// ...
React.Animated = /* OH GAWD. WHAT NOW?! */
Since React Native just uses React itself
under the hood, the react module turns
out to be a pretty good starting point
for a mock.
But then of course, you quickly figure
out that almost every one of your
components has at least a View node, a
Text node, and a stylesheet object… so
we create some simple “mock”
components for those that just return
null.
But React Native has 38 component
APIs that it exports, and 35 non-
component APIs.
Many of them that you are using can
end up being pretty complicated. At
this point, you are wondering just how
deep you are going to need to go just
to get a single little test to pass.