UNPKG

i-react-utils

Version:
549 lines (548 loc) 20.3 kB
<!doctype html> <html lang="en"> <head> <title>Code coverage report for AjaxList.js</title> <meta charset="utf-8" /> <link rel="stylesheet" href="prettify.css" /> <link rel="stylesheet" href="base.css" /> <meta name="viewport" content="width=device-width, initial-scale=1"> <style type='text/css'> .coverage-summary .sorter { background-image: url(sort-arrow-sprite.png); } </style> </head> <body> <div class='wrapper'> <div class='pad1'> <h1> <a href="index.html">All files</a> AjaxList.js </h1> <div class='clearfix'> <div class='fl pad1y space-right2'> <span class="strong">56.25% </span> <span class="quiet">Statements</span> <span class='fraction'>36/64</span> </div> <div class='fl pad1y space-right2'> <span class="strong">33.33% </span> <span class="quiet">Branches</span> <span class='fraction'>11/33</span> </div> <div class='fl pad1y space-right2'> <span class="strong">50% </span> <span class="quiet">Functions</span> <span class='fraction'>8/16</span> </div> <div class='fl pad1y space-right2'> <span class="strong">57.14% </span> <span class="quiet">Lines</span> <span class='fraction'>36/63</span> </div> </div> </div> <div class='status-line medium'></div> <pre><table class="coverage"> <tr><td class="line-count quiet">1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162</td><td class="line-coverage quiet"><span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">3x</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">3x</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">3x</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">3x</span> <span class="cline-any cline-yes">3x</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">3x</span> <span class="cline-any cline-yes">3x</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span></td><td class="text"><pre class="prettyprint lang-js">import React, { PropTypes } from 'react'; import {SimpleListTable, ListPagination} from './ListComponents'; import {devOnly, shallowCopyExcept} from './utils'; import {_buildElement} from './utils-internal'; &nbsp; &nbsp; &nbsp; function copyProps(props) { return shallowCopyExcept({}, props, ['fetchDataCallback', 'onFetch', 'onSuccess', 'onError', 'renderRow', 'showPagination', 'loadingComponent', 'errorComponent', 'noDataComponent', 'headerAlwaysOn']); } &nbsp; export default class AjaxList extends React.Component { &nbsp; static propTypes = { id : React.PropTypes.string, // list id fetchDataCallback : React.PropTypes.func.isRequired, // func that returns ajax promise. onFetch : React.PropTypes.func, // when data loading starts, function(event) onError : React.PropTypes.func, // when data loading finishes with error, function(event) onSuccess : React.PropTypes.func, // when data loading finishes with success, function(event) errorComponent : React.PropTypes.oneOfType([ React.PropTypes.func, React.PropTypes.element ]), // component function or element loadingComponent : React.PropTypes.oneOfType([ React.PropTypes.func, React.PropTypes.element ]), // component function or element noDataComponent : React.PropTypes.oneOfType([ React.PropTypes.func, React.PropTypes.element ]), // component function or element renderRow : React.PropTypes.func.isRequired, showPagination : React.PropTypes.bool, // Default true headerAlwaysOn : React.PropTypes.bool // if show header and footer when no data is available. Default: true }; &nbsp; static defaultProps = { id : "ajaxList-" + Math.floor(Math.random() * 1000000).toString(22), showPagination : true, headerAlwaysOn : true, className : "ajaxList", errorComponent : (&lt;div className="center-block ajaxList-error"&gt;An error occurred.&lt;/div&gt;), loadingComponent : (&lt;div className="center-block ajaxList-loader"&gt;&lt;/div&gt;) }; &nbsp; constructor(props) { super(props); this.props = props; this.id = props.id || <span class="branch-1 cbranch-no" title="branch not covered" >'ajaxList-' + (Math.random() * 10000);</span> this._fetchData = this._fetchData.bind(this); this._handlePageChange = this._handlePageChange.bind(this); this.updateAndResetPage = this.updateAndResetPage.bind(this); this.update = this.update.bind(this); this.mounted = false; this.currentPage = 1; this.htmlProps = copyProps(props); this.loadingElement = _buildElement(props.loadingComponent, this.htmlProps, []); this.noDataElement = _buildElement(props.noDataComponent, this.htmlProps, []); this.state = {"items":null,"paging":{"total":0,"page":1,"count":1}, error:null}; } &nbsp; <span class="fstat-no" title="function not covered" > co</span>mponentWillReceiveProps(newProps) { <span class="cstat-no" title="statement not covered" > this.htmlProps = copyProps(newProps);</span> <span class="cstat-no" title="statement not covered" > if (this.props.loadingComponent != newProps.loadingComponent) {</span> <span class="cstat-no" title="statement not covered" > this.loadingElement = _buildElement(newProps.loadingComponent, this.htmlProps, []);</span> } <span class="cstat-no" title="statement not covered" > if (this.props.noDataComponent != newProps.noDataComponent) {</span> <span class="cstat-no" title="statement not covered" > this.noDataElement = _buildElement(newProps.noDataComponent, this.htmlProps, []);</span> } <span class="cstat-no" title="statement not covered" > this.props = newProps;</span> } &nbsp; <span class="fstat-no" title="function not covered" > _c</span>heckData(data) { <span class="cstat-no" title="statement not covered" > if (Array.isArray(data) &amp;&amp; this.props.showPagination &amp;&amp; this.pagesCount == undefined) {</span> <span class="cstat-no" title="statement not covered" > throw new Error('Got array of data and pagination was required but pagesCount is not set.');</span> } } &nbsp; _fetchData(page, withClear) { this.currentPage = page; <span class="missing-if-branch" title="else path not taken" >E</span>if (this.props.onFetch) { this.props.onFetch({ page : page }); } <span class="missing-if-branch" title="if path not taken" >I</span>if (withClear) { <span class="cstat-no" title="statement not covered" > this.setState({items: null, error:null});</span> } else { this.setState({error:null}); } let promise = this.props.fetchDataCallback(page); <span class="missing-if-branch" title="else path not taken" >E</span>if (promise) { promise.then((resp) =&gt; { <span class="missing-if-branch" title="else path not taken" >E</span>if (this.mounted) { <span class="missing-if-branch" title="if path not taken" >I</span>if (Array.isArray(resp.data)) { let page = <span class="cstat-no" title="statement not covered" >this.state.paging.page + 1;</span> const np = <span class="cstat-no" title="statement not covered" >{... this.state.paging};</span> <span class="cstat-no" title="statement not covered" > np.page = page;</span> <span class="cstat-no" title="statement not covered" > this.setState({items: resp.data, paging: np});</span> } else { this.setState(resp.data); } } <span class="missing-if-branch" title="else path not taken" >E</span>if (this.props.onSuccess) { this.props.onSuccess({ page : page, data : resp.data }); } }, <span class="fstat-no" title="function not covered" > (e</span>rr) =&gt; { <span class="cstat-no" title="statement not covered" > devOnly(<span class="fstat-no" title="function not covered" >()</span> =&gt; {<span class="cstat-no" title="statement not covered" >console.log("AjaxList: fetch rejected: ", err);}</span>);</span> <span class="cstat-no" title="statement not covered" > if (this.props.onError) {</span> <span class="cstat-no" title="statement not covered" > this.props.onError(err);</span> } <span class="cstat-no" title="statement not covered" > if (this.mounted) {</span> const errCompProps = <span class="cstat-no" title="statement not covered" >{...this.htmlProps, error: err};</span> const errorElement = <span class="cstat-no" title="statement not covered" >_buildElement(this.props.errorComponent, errCompProps, []);</span> <span class="cstat-no" title="statement not covered" > this.setState({error: errorElement});</span> } }); } else { <span class="cstat-no" title="statement not covered" > this.setState({"items":[],"paging":{"total":0,"page":1,"count":1}}); </span>// empty list } } &nbsp; componentWillMount() { this.mounted = true; } &nbsp; componentDidMount() { this._fetchData(1, false); } &nbsp; <span class="fstat-no" title="function not covered" > co</span>mponentWillUnmount() { <span class="cstat-no" title="statement not covered" > this.mounted = false;</span> } &nbsp; <span class="fstat-no" title="function not covered" > _h</span>andlePageChange(pg) { <span class="cstat-no" title="statement not covered" > this._fetchData(pg, true);</span> } &nbsp; <span class="fstat-no" title="function not covered" > up</span>dateAndResetPage() { <span class="cstat-no" title="statement not covered" > this._fetchData(1, true);</span> } &nbsp; <span class="fstat-no" title="function not covered" > up</span>date() { <span class="cstat-no" title="statement not covered" > this._fetchData(this.currentPage, false);</span> } &nbsp; render() { const {paging, items, error} = this.state; let noDataElem; <span class="missing-if-branch" title="if path not taken" >I</span>if (error) { <span class="cstat-no" title="statement not covered" > noDataElem = error;</span> } else { noDataElem = items == null ? this.loadingElement : this.noDataElement; } const indexOffset = Math.max(0, paging.page -1) * paging.count; return ( &lt;div id={this.id} {...this.htmlProps}&gt; &lt;SimpleListTable className={this.props.className} id={this.id} renderRow={this.props.renderRow} data={items} headerAlwaysOn={this.props.headerAlwaysOn} noDataElement={noDataElem} indexOffset={indexOffset}&gt; {this.props.children} &lt;/SimpleListTable&gt; {(() =&gt; { <span class="missing-if-branch" title="else path not taken" >E</span>if (this.props.showPagination) { return (&lt;ListPagination className={this.props.className} onPageChanged={this._handlePageChange} id={this.id} total={paging.total} count={paging.count} page={paging.page}/&gt;); } else { <span class="cstat-no" title="statement not covered" > return null;</span> } })()} &lt;/div&gt; ); } } &nbsp;</pre></td></tr> </table></pre> <div class='push'></div><!-- for sticky footer --> </div><!-- /wrapper --> <div class='footer quiet pad2 space-top1 center small'> Code coverage generated by <a href="https://istanbul.js.org/" target="_blank">istanbul</a> at Sun Dec 17 2017 22:40:08 GMT+0100 (CET) </div> </div> <script src="prettify.js"></script> <script> window.onload = function () { if (typeof prettyPrint === 'function') { prettyPrint(); } }; </script> <script src="sorter.js"></script> </body> </html>