react-native-camera/docs/tests.html
MateusAndrade de6333141e Deploy website
Deploy website version based on 970248c1a6
2020-05-11 13:06:56 +00:00

298 lines
26 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html><html lang="en"><head><meta charSet="utf-8"/><meta http-equiv="X-UA-Compatible" content="IE=edge"/><title>Testing · React Native Camera</title><meta name="viewport" content="width=device-width"/><meta name="generator" content="Docusaurus"/><meta name="description" content="&lt;h2&gt;&lt;a class=&quot;anchor&quot; aria-hidden=&quot;true&quot; id=&quot;jest&quot;&gt;&lt;/a&gt;&lt;a href=&quot;#jest&quot; aria-hidden=&quot;true&quot; class=&quot;hash-link&quot;&gt;&lt;svg class=&quot;hash-link-icon&quot; aria-hidden=&quot;true&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Jest&lt;/h2&gt;
"/><meta name="docsearch:language" content="en"/><meta property="og:title" content="Testing · React Native Camera"/><meta property="og:type" content="website"/><meta property="og:url" content="undefined/react-native-camera/"/><meta property="og:description" content="&lt;h2&gt;&lt;a class=&quot;anchor&quot; aria-hidden=&quot;true&quot; id=&quot;jest&quot;&gt;&lt;/a&gt;&lt;a href=&quot;#jest&quot; aria-hidden=&quot;true&quot; class=&quot;hash-link&quot;&gt;&lt;svg class=&quot;hash-link-icon&quot; aria-hidden=&quot;true&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Jest&lt;/h2&gt;
"/><meta property="og:image" content="undefined/react-native-camera/img/undraw_online.svg"/><meta name="twitter:card" content="summary"/><meta name="twitter:image" content="undefined/react-native-camera/img/undraw_tweetstorm.svg"/><link rel="shortcut icon" href="/react-native-camera/img/community.png"/><link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/atom-one-dark.min.css"/><link rel="stylesheet" href="/css/code-block-buttons.css"/><script type="text/javascript" src="https://buttons.github.io/buttons.js"></script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.0/clipboard.min.js"></script><script type="text/javascript" src="/js/code-blocks-buttons.js"></script><script src="/react-native-camera/js/scrollSpy.js"></script><link rel="stylesheet" href="/react-native-camera/css/prism.css"/><link rel="stylesheet" href="/react-native-camera/css/main.css"/><script src="/react-native-camera/js/codetabs.js"></script></head><body class="sideNavVisible separateOnPageNav"><div class="fixedHeaderContainer"><div class="headerWrapper wrapper"><header><a href="/react-native-camera/"><img class="logo" src="/react-native-camera/img/community.png" alt="React Native Camera"/><h2 class="headerTitleWithLogo">React Native Camera</h2></a><div class="navigationWrapper navigationSlider"><nav class="slidingNav"><ul class="nav-site nav-site-internal"><li class="siteNavGroupActive"><a href="/react-native-camera/docs/installation" target="_self">Docs</a></li><li class="siteNavGroupActive"><a href="/react-native-camera/docs/expo_usage" target="_self">Guides</a></li><li class="siteNavGroupActive"><a href="/react-native-camera/docs/api" target="_self">API</a></li><li class=""><a href="/react-native-camera/docs/qa" target="_self">Help</a></li><li class="siteNavGroupActive"><a href="/react-native-camera/docs/Tidelift" target="_self">For Enterprise</a></li><li class=""><a href="https://github.com/react-native-community/react-native-camera" target="_self">GitHub</a></li></ul></nav></div></header></div></div><div class="navPusher"><div class="docMainWrapper wrapper"><div class="container docsNavContainer" id="docsNav"><nav class="toc"><div class="toggleNav"><section class="navWrapper wrapper"><div class="navBreadcrumb wrapper"><div class="navToggle" id="navToggler"><div class="hamburger-menu"><div class="line1"></div><div class="line2"></div><div class="line3"></div></div></div><h2><i></i><span>Guides</span></h2><div class="tocToggler" id="tocToggler"><i class="icon-toc"></i></div></div><div class="navGroups"><div class="navGroup"><h3 class="navGroupCategoryTitle">Getting Started</h3><ul class=""><li class="navListItem"><a class="navItem" href="/react-native-camera/docs/installation">Installation</a></li><li class="navListItem"><a class="navItem" href="/react-native-camera/docs/contributing">Contributing</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Guides</h3><ul class=""><li class="navListItem"><a class="navItem" href="/react-native-camera/docs/expo_usage">Expo Usage</a></li><li class="navListItem"><a class="navItem" href="/react-native-camera/docs/GradleUpgradeGuide">Gradle Upgrade Guide</a></li><li class="navListItem"><a class="navItem" href="/react-native-camera/docs/migration">Migrating from RCTCamera to RNCamera</a></li><li class="navListItem"><a class="navItem" href="/react-native-camera/docs/migrationv2">Migrating from version 1.x to 2.x</a></li><li class="navListItem"><a class="navItem" href="/react-native-camera/docs/react-navigation">React Navigation</a></li><li class="navListItem"><a class="navItem" href="/react-native-camera/docs/recipes">Recipes</a></li><li class="navListItem"><a class="navItem" href="/react-native-camera/docs/rncamera">RNCamera</a></li><li class="navListItem navListItemActive"><a class="navItem" href="/react-native-camera/docs/tests">Testing</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">API</h3><ul class=""><li class="navListItem"><a class="navItem" href="/react-native-camera/docs/api">Work in progress</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Help</h3><ul class=""><li class="navListItem"><a class="navItem" href="/react-native-camera/docs/qa">Q &amp; A</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Enterprise</h3><ul class=""><li class="navListItem"><a class="navItem" href="/react-native-camera/docs/Tidelift">React-Native-Camera for Enterprise</a></li></ul></div></div></section></div><script>
var coll = document.getElementsByClassName('collapsible');
var checkActiveCategory = true;
for (var i = 0; i < coll.length; i++) {
var links = coll[i].nextElementSibling.getElementsByTagName('*');
if (checkActiveCategory){
for (var j = 0; j < links.length; j++) {
if (links[j].classList.contains('navListItemActive')){
coll[i].nextElementSibling.classList.toggle('hide');
coll[i].childNodes[1].classList.toggle('rotate');
checkActiveCategory = false;
break;
}
}
}
coll[i].addEventListener('click', function() {
var arrow = this.childNodes[1];
arrow.classList.toggle('rotate');
var content = this.nextElementSibling;
content.classList.toggle('hide');
});
}
document.addEventListener('DOMContentLoaded', function() {
createToggler('#navToggler', '#docsNav', 'docsSliderActive');
createToggler('#tocToggler', 'body', 'tocActive');
var headings = document.querySelector('.toc-headings');
headings && headings.addEventListener('click', function(event) {
var el = event.target;
while(el !== headings){
if (el.tagName === 'A') {
document.body.classList.remove('tocActive');
break;
} else{
el = el.parentNode;
}
}
}, false);
function createToggler(togglerSelector, targetSelector, className) {
var toggler = document.querySelector(togglerSelector);
var target = document.querySelector(targetSelector);
if (!toggler) {
return;
}
toggler.onclick = function(event) {
event.preventDefault();
target.classList.toggle(className);
};
}
});
</script></nav></div><div class="container mainContainer"><div class="wrapper"><div class="post"><header class="postHeader"><a class="edit-page-link button" href="https://github.com/react-native-community/react-native-camera/master/docs/tests.md" target="_blank" rel="noreferrer noopener">Edit</a><h1 class="postHeaderTitle">Testing</h1></header><article><div><span><h2><a class="anchor" aria-hidden="true" id="jest"></a><a href="#jest" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Jest</h2>
<p>To test a component which use RNCamera, you need to create a react-native-camera.js file inside your <strong>mocks</strong> folder on the root of your project with the following content:</p>
<pre><code class="hljs css language-javascript"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">const</span> timeout = <span class="hljs-function"><span class="hljs-params">ms</span> =&gt;</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function"><span class="hljs-params">resolve</span> =&gt;</span> setTimeout(resolve, ms));
<span class="hljs-keyword">export</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RNCamera</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
<span class="hljs-keyword">static</span> Constants = {
<span class="hljs-attr">Aspect</span>: {},
<span class="hljs-attr">BarCodeType</span>: {},
<span class="hljs-attr">Type</span>: { <span class="hljs-attr">back</span>: <span class="hljs-string">'back'</span>, <span class="hljs-attr">front</span>: <span class="hljs-string">'front'</span> },
<span class="hljs-attr">CaptureMode</span>: {},
<span class="hljs-attr">CaptureTarget</span>: {},
<span class="hljs-attr">CaptureQuality</span>: {},
<span class="hljs-attr">Orientation</span>: {},
<span class="hljs-attr">FlashMode</span>: {},
<span class="hljs-attr">TorchMode</span>: {},
};
takePictureAsync = <span class="hljs-keyword">async</span> () =&gt; {
<span class="hljs-keyword">await</span> timeout(<span class="hljs-number">2000</span>);
<span class="hljs-keyword">return</span> {
<span class="hljs-attr">base64</span>: <span class="hljs-string">'base64string'</span>,
};
};
render() {
<span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;
}
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> RNCamera;
</code></pre>
<p>You don't need to do anything else in your test, because Jest will use the mock in your test instead of the native module.</p>
<h3><a class="anchor" aria-hidden="true" id="example"></a><a href="#example" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Example</h3>
<p>We are going to create a component which uses RNCamera which two simple features:</p>
<ul>
<li>Take a photo</li>
<li>Change camera between front or back</li>
</ul>
<p>The custom component PhotoCamera is the following:</p>
<pre><code class="hljs css language-javascript"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> { View, TouchableOpacity, StyleSheet, Dimensions } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-native'</span>;
<span class="hljs-keyword">import</span> { RNCamera } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-native-camera'</span>;
<span class="hljs-keyword">import</span> Icon <span class="hljs-keyword">from</span> <span class="hljs-string">'react-native-vector-icons/FontAwesome'</span>;
<span class="hljs-keyword">const</span> styles = StyleSheet.create({
<span class="hljs-attr">container</span>: {
<span class="hljs-attr">flex</span>: <span class="hljs-number">1</span>,
<span class="hljs-attr">display</span>: <span class="hljs-string">'flex'</span>,
<span class="hljs-attr">flexDirection</span>: <span class="hljs-string">'column'</span>,
<span class="hljs-attr">backgroundColor</span>: <span class="hljs-string">'black'</span>,
},
<span class="hljs-attr">preview</span>: {
<span class="hljs-attr">flex</span>: <span class="hljs-number">1</span>,
<span class="hljs-attr">justifyContent</span>: <span class="hljs-string">'flex-end'</span>,
<span class="hljs-attr">alignItems</span>: <span class="hljs-string">'center'</span>,
},
<span class="hljs-attr">topButtons</span>: {
<span class="hljs-attr">flex</span>: <span class="hljs-number">1</span>,
<span class="hljs-attr">width</span>: Dimensions.get(<span class="hljs-string">'window'</span>).width,
<span class="hljs-attr">alignItems</span>: <span class="hljs-string">'flex-start'</span>,
},
<span class="hljs-attr">bottomButtons</span>: {
<span class="hljs-attr">flex</span>: <span class="hljs-number">1</span>,
<span class="hljs-attr">width</span>: Dimensions.get(<span class="hljs-string">'window'</span>).width,
<span class="hljs-attr">justifyContent</span>: <span class="hljs-string">'flex-end'</span>,
<span class="hljs-attr">alignItems</span>: <span class="hljs-string">'center'</span>,
},
<span class="hljs-attr">flipButton</span>: {
<span class="hljs-attr">flex</span>: <span class="hljs-number">1</span>,
<span class="hljs-attr">marginTop</span>: <span class="hljs-number">20</span>,
<span class="hljs-attr">right</span>: <span class="hljs-number">20</span>,
<span class="hljs-attr">alignSelf</span>: <span class="hljs-string">'flex-end'</span>,
},
<span class="hljs-attr">recordingButton</span>: {
<span class="hljs-attr">marginBottom</span>: <span class="hljs-number">10</span>,
},
});
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PhotoCamera</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">PureComponent</span> </span>{
state = {
<span class="hljs-attr">type</span>: RNCamera.Constants.Type.back,
};
flipCamera = <span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span>
<span class="hljs-keyword">this</span>.setState({
<span class="hljs-attr">type</span>:
<span class="hljs-keyword">this</span>.state.type === RNCamera.Constants.Type.back
? RNCamera.Constants.Type.front
: RNCamera.Constants.Type.back,
});
takePhoto = <span class="hljs-keyword">async</span> () =&gt; {
<span class="hljs-keyword">const</span> { onTakePhoto } = <span class="hljs-keyword">this</span>.props;
<span class="hljs-keyword">const</span> options = {
<span class="hljs-attr">quality</span>: <span class="hljs-number">0.5</span>,
<span class="hljs-attr">base64</span>: <span class="hljs-literal">true</span>,
<span class="hljs-attr">width</span>: <span class="hljs-number">300</span>,
<span class="hljs-attr">height</span>: <span class="hljs-number">300</span>,
};
<span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> <span class="hljs-keyword">this</span>.camera.takePictureAsync(options);
onTakePhoto(data.base64);
};
render() {
<span class="hljs-keyword">const</span> { type } = <span class="hljs-keyword">this</span>.state;
<span class="hljs-keyword">return</span> (
&lt;View style={styles.container}&gt;
&lt;RNCamera
ref={cam =&gt; {
this.camera = cam;
}}
type={type}
style={styles.preview}
/&gt;
&lt;View style={styles.topButtons}&gt;
&lt;TouchableOpacity onPress={this.flipCamera} style={styles.flipButton}&gt;
&lt;Icon name="refresh" size={35} color="orange" /&gt;
&lt;/TouchableOpacity&gt;
&lt;/View&gt;
&lt;View style={styles.bottomButtons}&gt;
&lt;TouchableOpacity onPress={this.takePhoto} style={styles.recordingButton}&gt;
&lt;Icon name="camera" size={50} color="orange" /&gt;
&lt;/TouchableOpacity&gt;
&lt;/View&gt;
&lt;/View&gt;
);
}
}
export default PhotoCamera;
</code></pre>
<p>And here is our test to check if it renders properly:</p>
<pre><code class="hljs css language-javascript"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> Adapter <span class="hljs-keyword">from</span> <span class="hljs-string">'enzyme-adapter-react-16'</span>;
<span class="hljs-keyword">import</span> { shallow, configure } <span class="hljs-keyword">from</span> <span class="hljs-string">'enzyme'</span>;
<span class="hljs-keyword">import</span> MyPhotoCamera <span class="hljs-keyword">from</span> <span class="hljs-string">'./'</span>;
configure({ <span class="hljs-attr">adapter</span>: <span class="hljs-keyword">new</span> Adapter() });
describe(<span class="hljs-string">'PhotoCamera Tests'</span>, () =&gt; {
test(<span class="hljs-string">'renders correctly'</span>, () =&gt; {
<span class="hljs-keyword">const</span> wrapper = shallow(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">MyPhotoCamera</span> /&gt;</span>);
expect(wrapper).toMatchSnapshot();
});
});
</span></code></pre>
<p>Also, here is the complete test of the whole component with 100% coverage:</p>
<pre><code class="hljs css language-javascript"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> { TouchableOpacity } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-native'</span>;
<span class="hljs-keyword">import</span> Adapter <span class="hljs-keyword">from</span> <span class="hljs-string">'enzyme-adapter-react-16'</span>;
<span class="hljs-keyword">import</span> { shallow, configure, mount } <span class="hljs-keyword">from</span> <span class="hljs-string">'enzyme'</span>;
<span class="hljs-keyword">import</span> PhotoCamera <span class="hljs-keyword">from</span> <span class="hljs-string">'../'</span>;
<span class="hljs-keyword">const</span> { JSDOM } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'jsdom'</span>);
<span class="hljs-keyword">const</span> jsdom = <span class="hljs-keyword">new</span> JSDOM();
<span class="hljs-keyword">const</span> { <span class="hljs-built_in">window</span> } = jsdom;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">copyProps</span>(<span class="hljs-params">src, target</span>) </span>{
<span class="hljs-keyword">const</span> props = <span class="hljs-built_in">Object</span>.getOwnPropertyNames(src)
.filter(<span class="hljs-function"><span class="hljs-params">prop</span> =&gt;</span> <span class="hljs-keyword">typeof</span> target[prop] === <span class="hljs-string">'undefined'</span>)
.map(<span class="hljs-function"><span class="hljs-params">prop</span> =&gt;</span> <span class="hljs-built_in">Object</span>.getOwnPropertyDescriptor(src, prop));
<span class="hljs-built_in">Object</span>.defineProperties(target, props);
}
global.window = <span class="hljs-built_in">window</span>;
global.document = <span class="hljs-built_in">window</span>.document;
global.navigator = {
<span class="hljs-attr">userAgent</span>: <span class="hljs-string">'node.js'</span>,
};
copyProps(<span class="hljs-built_in">window</span>, global);
<span class="hljs-comment">// Ignore React Web errors when using React Native</span>
<span class="hljs-comment">// but still show relevant errors</span>
<span class="hljs-keyword">const</span> suppressedErrors = <span class="hljs-regexp">/(React does not recognize the.*prop on a DOM element|Unknown event handler property|is using uppercase HTML|Received `true` for a non-boolean attribute `accessible`|The tag.*is unrecognized in this browser)|is using incorrect casing|Received `true` for a non-boolean attribute `enabled`/</span>;
<span class="hljs-keyword">const</span> realConsoleError = <span class="hljs-built_in">console</span>.error; <span class="hljs-comment">// eslint-disable-line</span>
<span class="hljs-comment">// eslint-disable-next-line</span>
<span class="hljs-built_in">console</span>.error = <span class="hljs-function"><span class="hljs-params">message</span> =&gt;</span> {
<span class="hljs-keyword">if</span> (message.match(suppressedErrors)) {
<span class="hljs-keyword">return</span>;
}
realConsoleError(message);
};
configure({ <span class="hljs-attr">adapter</span>: <span class="hljs-keyword">new</span> Adapter() });
describe(<span class="hljs-string">'PhotoCamera Tests'</span>, () =&gt; {
test(<span class="hljs-string">'renders correctly'</span>, () =&gt; {
<span class="hljs-keyword">const</span> wrapper = shallow(&lt;PhotoCamera /&gt;);
expect(wrapper).toMatchSnapshot();
});
test('initial state should be back camera', () =&gt; {
const wrapper = shallow(&lt;PhotoCamera /&gt;);
expect(wrapper.state().type).toBe('back');
});
test('should flip the camera from back to front', () =&gt; {
const wrapper = shallow(&lt;PhotoCamera /&gt;);
expect(wrapper.state().type).toBe('back');
wrapper
.find(TouchableOpacity)
.first()
.props()
.onPress();
expect(wrapper.state().type).toBe('front');
});
test('should flip the camera from front to back if touch flip button and curren state is ', () =&gt; {
const wrapper = shallow(&lt;PhotoCamera /&gt;);
wrapper.setState({
type: 'front',
});
wrapper.update();
wrapper
.find(TouchableOpacity)
.first()
.props()
.onPress();
expect(wrapper.state().type).toBe('back');
});
test('should have a reference to the React Native Camera module', () =&gt; {
const wrapper = mount(&lt;PhotoCamera /&gt;);
expect(wrapper.instance().camera).toBeDefined();
});
test('test onPress functionality', async () =&gt; {
const onTakePhotoEvent = jest.fn(data =&gt; data);
const wrapper = mount(&lt;PhotoCamera onTakePhoto={onTakePhotoEvent} /&gt;);
await wrapper
.find(TouchableOpacity)
.at(1)
.props()
.onPress();
expect(onTakePhotoEvent.mock.calls.length).toBe(1);
});
});
</code></pre>
</span></div></article></div><div class="docLastUpdate"><em>Last updated on 6/2/2019 by Ronaldo Lima</em></div><div class="docs-prevnext"><a class="docs-prev button" href="/react-native-camera/docs/rncamera"><span class="arrow-prev"></span><span>RNCamera</span></a><a class="docs-next button" href="/react-native-camera/docs/api"><span>Work in progress</span><span class="arrow-next"></span></a></div></div></div><nav class="onPageNav"><ul class="toc-headings"><li><a href="#jest">Jest</a><ul class="toc-headings"><li><a href="#example">Example</a></li></ul></li></ul></nav></div><footer class="nav-footer" id="footer"><section class="sitemap"><a href="/react-native-camera/" class="nav-home"><img src="/react-native-camera/img/community.png" alt="React Native Camera" width="66" height="58"/></a><div><h5>Docs</h5><a href="/react-native-camera/docs/en/installation.html">Getting Started</a><a href="/react-native-camera/docs/en/expo_usage.html">Guides</a><a href="/react-native-camera/docs/en/api.html">API Reference</a></div><div><h5>Community</h5><a href="/react-native-camera/en/users.html">User Showcase</a><a href="https://stackoverflow.com/questions/tagged/react-native-camera" target="_blank" rel="noreferrer noopener">Stack Overflow</a><a href="https://spectrum.chat/">Project Chat</a></div><div><h5>More</h5><a href="https://github.com/react-native-community/react-native-camera">GitHub</a><a class="github-button" href="https://github.com/react-native-community/react-native-camera" data-icon="octicon-star" data-count-href="/facebook/docusaurus/stargazers" data-show-count="true" data-count-aria-label="# stargazers on GitHub" aria-label="Star this project on GitHub">Stars</a></div></section><a href="https://opensource.facebook.com/" target="_blank" rel="noreferrer noopener" class="fbOpenSource"><img src="/react-native-camera/img/oss_logo.png" alt="Facebook Open Source" width="170" height="45"/></a><section class="copyright">Copyright © 2020</section></footer></div></body></html>