├── .codesandbox └── workspace.json ├── .github └── workflows │ └── validate.yml ├── .gitignore ├── .prettierrc ├── coverage ├── clover.xml ├── coverage-final.json ├── lcov-report │ ├── base.css │ ├── block-navigation.js │ ├── component-did-catch.js.html │ ├── favicon.png │ ├── fetch-greeting.js.html │ ├── index.html │ ├── index.js.html │ ├── lazy-component.js.html │ ├── prettify.css │ ├── prettify.js │ ├── react-context.js.html │ ├── sort-arrow-sprite.png │ ├── sorter.js │ └── utils.js.html └── lcov.info ├── package.json ├── public └── index.html ├── sandbox.config.json └── src ├── __tests__ ├── async.js ├── component-did-catch.js ├── i18next.js ├── mock.react-router.js ├── mock.react-transition-group.js ├── on-change.js ├── portals.js ├── react-context.js ├── react-intl.js ├── react-lazy-and-suspense.js ├── react-redux.js ├── react-router.js ├── shallow.react-transition-group.js ├── update-props.js ├── upload-file.js └── window-resize.js ├── component-did-catch.js ├── fetch-greeting.js ├── index.js ├── lazy-component.js ├── react-context.js ├── setupTests.js └── utils.js /.codesandbox/workspace.json: -------------------------------------------------------------------------------- 1 | { 2 | "preview": [ 3 | { 4 | "views": [ 5 | { 6 | "id": "codesandbox.tests" 7 | }, 8 | { 9 | "id": "codesandbox.browser" 10 | } 11 | ] 12 | }, 13 | { 14 | "views": [ 15 | { 16 | "id": "codesandbox.console" 17 | }, 18 | { 19 | "id": "codesandbox.problems" 20 | } 21 | ] 22 | } 23 | ] 24 | } -------------------------------------------------------------------------------- /.github/workflows/validate.yml: -------------------------------------------------------------------------------- 1 | name: validate 2 | on: 3 | - push 4 | 5 | concurrency: 6 | group: ${{ github.workflow }}-${{ github.ref }} 7 | cancel-in-progress: true 8 | 9 | jobs: 10 | main: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - name: ⬇️ Checkout repo 14 | uses: actions/checkout@v2 15 | 16 | - name: ⎔ Setup node 17 | uses: actions/setup-node@v1 18 | 19 | - name: 📥 Download deps 20 | uses: bahmutov/npm-install@v1 21 | with: 22 | useLockFile: false 23 | 24 | - name: 🧪 Run test script 25 | run: npm run test:coverage 26 | 27 | - name: 🏗 Run build script 28 | run: npm run build 29 | 30 | - name: ⬆️ Upload coverage report 31 | uses: codecov/codecov-action@v1 32 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | build 3 | package-lock.json 4 | yarn.lock 5 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "printWidth": 80, 3 | "tabWidth": 2, 4 | "useTabs": false, 5 | "semi": false, 6 | "singleQuote": true, 7 | "trailingComma": "all", 8 | "bracketSpacing": false, 9 | "jsxBracketSameLine": false, 10 | "arrowParens": "avoid" 11 | } 12 | -------------------------------------------------------------------------------- /coverage/clover.xml: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /coverage/coverage-final.json: -------------------------------------------------------------------------------- 1 | {"/Users/kentcdodds/Desktop/react-testing-library-examples/src/component-did-catch.js": {"path":"/Users/kentcdodds/Desktop/react-testing-library-examples/src/component-did-catch.js","statementMap":{"0":{"start":{"line":5,"column":10},"end":{"line":5,"column":27}},"1":{"start":{"line":7,"column":4},"end":{"line":7,"column":35}},"2":{"start":{"line":8,"column":4},"end":{"line":8,"column":28}},"3":{"start":{"line":11,"column":4},"end":{"line":15,"column":5}},"4":{"start":{"line":21,"column":4},"end":{"line":21,"column":30}},"5":{"start":{"line":26,"column":10},"end":{"line":26,"column":29}},"6":{"start":{"line":27,"column":20},"end":{"line":27,"column":59}},"7":{"start":{"line":27,"column":26},"end":{"line":27,"column":59}},"8":{"start":{"line":29,"column":4},"end":{"line":41,"column":5}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":6,"column":2},"end":{"line":6,"column":3}},"loc":{"start":{"line":6,"column":33},"end":{"line":9,"column":3}},"line":6},"1":{"name":"(anonymous_1)","decl":{"start":{"line":10,"column":2},"end":{"line":10,"column":3}},"loc":{"start":{"line":10,"column":11},"end":{"line":16,"column":3}},"line":10},"2":{"name":"(anonymous_2)","decl":{"start":{"line":20,"column":2},"end":{"line":20,"column":3}},"loc":{"start":{"line":20,"column":11},"end":{"line":22,"column":3}},"line":20},"3":{"name":"(anonymous_3)","decl":{"start":{"line":27,"column":20},"end":{"line":27,"column":21}},"loc":{"start":{"line":27,"column":26},"end":{"line":27,"column":59}},"line":27},"4":{"name":"(anonymous_4)","decl":{"start":{"line":28,"column":2},"end":{"line":28,"column":3}},"loc":{"start":{"line":28,"column":11},"end":{"line":42,"column":3}},"line":28}},"branchMap":{"0":{"loc":{"start":{"line":11,"column":11},"end":{"line":15,"column":5}},"type":"cond-expr","locations":[{"start":{"line":12,"column":6},"end":{"line":12,"column":36}},{"start":{"line":14,"column":6},"end":{"line":14,"column":25}}],"line":11},"1":{"loc":{"start":{"line":31,"column":9},"end":{"line":39,"column":9}},"type":"cond-expr","locations":[{"start":{"line":32,"column":10},"end":{"line":32,"column":18}},{"start":{"line":34,"column":10},"end":{"line":38,"column":19}}],"line":31}},"s":{"0":1,"1":1,"2":1,"3":3,"4":2,"5":1,"6":1,"7":1,"8":2},"f":{"0":1,"1":3,"2":2,"3":1,"4":2},"b":{"0":[1,2],"1":[1,1]},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"df62e411584b1bab3f446a399ccdf8caf262a4fe"} 2 | ,"/Users/kentcdodds/Desktop/react-testing-library-examples/src/fetch-greeting.js": {"path":"/Users/kentcdodds/Desktop/react-testing-library-examples/src/fetch-greeting.js","statementMap":{"0":{"start":{"line":5,"column":21},"end":{"line":8,"column":1}},"1":{"start":{"line":11,"column":2},"end":{"line":27,"column":3}},"2":{"start":{"line":13,"column":6},"end":{"line":16,"column":7}},"3":{"start":{"line":19,"column":6},"end":{"line":22,"column":7}},"4":{"start":{"line":25,"column":6},"end":{"line":25,"column":18}},"5":{"start":{"line":31,"column":40},"end":{"line":34,"column":3}},"6":{"start":{"line":35,"column":44},"end":{"line":35,"column":59}},"7":{"start":{"line":37,"column":24},"end":{"line":49,"column":3}},"8":{"start":{"line":38,"column":4},"end":{"line":48,"column":8}},"9":{"start":{"line":41,"column":23},"end":{"line":41,"column":31}},"10":{"start":{"line":42,"column":27},"end":{"line":42,"column":31}},"11":{"start":{"line":43,"column":8},"end":{"line":43,"column":45}},"12":{"start":{"line":44,"column":8},"end":{"line":44,"column":30}},"13":{"start":{"line":47,"column":8},"end":{"line":47,"column":40}},"14":{"start":{"line":51,"column":21},"end":{"line":51,"column":59}},"15":{"start":{"line":53,"column":2},"end":{"line":61,"column":3}}},"fnMap":{"0":{"name":"greetingReducer","decl":{"start":{"line":10,"column":9},"end":{"line":10,"column":24}},"loc":{"start":{"line":10,"column":40},"end":{"line":28,"column":1}},"line":10},"1":{"name":"FetchGreeting","decl":{"start":{"line":30,"column":9},"end":{"line":30,"column":22}},"loc":{"start":{"line":30,"column":25},"end":{"line":62,"column":1}},"line":30},"2":{"name":"(anonymous_2)","decl":{"start":{"line":37,"column":24},"end":{"line":37,"column":25}},"loc":{"start":{"line":37,"column":36},"end":{"line":49,"column":3}},"line":37},"3":{"name":"(anonymous_3)","decl":{"start":{"line":40,"column":12},"end":{"line":40,"column":13}},"loc":{"start":{"line":40,"column":26},"end":{"line":45,"column":7}},"line":40},"4":{"name":"(anonymous_4)","decl":{"start":{"line":46,"column":13},"end":{"line":46,"column":14}},"loc":{"start":{"line":46,"column":24},"end":{"line":48,"column":7}},"line":46}},"branchMap":{"0":{"loc":{"start":{"line":11,"column":2},"end":{"line":27,"column":3}},"type":"switch","locations":[{"start":{"line":12,"column":4},"end":{"line":17,"column":5}},{"start":{"line":18,"column":4},"end":{"line":23,"column":5}},{"start":{"line":24,"column":4},"end":{"line":26,"column":5}}],"line":11},"1":{"loc":{"start":{"line":51,"column":21},"end":{"line":51,"column":59}},"type":"cond-expr","locations":[{"start":{"line":51,"column":37},"end":{"line":51,"column":41}},{"start":{"line":51,"column":44},"end":{"line":51,"column":59}}],"line":51},"2":{"loc":{"start":{"line":58,"column":7},"end":{"line":58,"column":38}},"type":"binary-expr","locations":[{"start":{"line":58,"column":7},"end":{"line":58,"column":15}},{"start":{"line":58,"column":19},"end":{"line":58,"column":38}}],"line":58},"3":{"loc":{"start":{"line":59,"column":7},"end":{"line":59,"column":58}},"type":"binary-expr","locations":[{"start":{"line":59,"column":7},"end":{"line":59,"column":12}},{"start":{"line":59,"column":16},"end":{"line":59,"column":58}}],"line":59}},"s":{"0":1,"1":2,"2":1,"3":1,"4":0,"5":5,"6":5,"7":5,"8":2,"9":1,"10":1,"11":1,"12":1,"13":1,"14":5,"15":5},"f":{"0":2,"1":5,"2":2,"3":1,"4":1},"b":{"0":[1,1,0],"1":[1,4],"2":[5,2],"3":[5,1]},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"3ff91edf72ec5a93da5125b9d48a0970647b33d3"} 3 | ,"/Users/kentcdodds/Desktop/react-testing-library-examples/src/index.js": {"path":"/Users/kentcdodds/Desktop/react-testing-library-examples/src/index.js","statementMap":{"0":{"start":{"line":5,"column":2},"end":{"line":38,"column":3}},"1":{"start":{"line":41,"column":0},"end":{"line":41,"column":48}}},"fnMap":{"0":{"name":"App","decl":{"start":{"line":4,"column":9},"end":{"line":4,"column":12}},"loc":{"start":{"line":4,"column":15},"end":{"line":39,"column":1}},"line":4}},"branchMap":{},"s":{"0":0,"1":0},"f":{"0":0},"b":{}} 4 | ,"/Users/kentcdodds/Desktop/react-testing-library-examples/src/lazy-component.js": {"path":"/Users/kentcdodds/Desktop/react-testing-library-examples/src/lazy-component.js","statementMap":{"0":{"start":{"line":4,"column":2},"end":{"line":4,"column":30}}},"fnMap":{"0":{"name":"LazyComponent","decl":{"start":{"line":3,"column":9},"end":{"line":3,"column":22}},"loc":{"start":{"line":3,"column":25},"end":{"line":5,"column":1}},"line":3}},"branchMap":{},"s":{"0":2},"f":{"0":2},"b":{},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"aea0971e644548027b45cf610d5ea0ab22298143"} 5 | ,"/Users/kentcdodds/Desktop/react-testing-library-examples/src/react-context.js": {"path":"/Users/kentcdodds/Desktop/react-testing-library-examples/src/react-context.js","statementMap":{"0":{"start":{"line":3,"column":20},"end":{"line":3,"column":50}},"1":{"start":{"line":5,"column":21},"end":{"line":10,"column":1}},"2":{"start":{"line":6,"column":19},"end":{"line":6,"column":37}},"3":{"start":{"line":7,"column":2},"end":{"line":9,"column":3}},"4":{"start":{"line":12,"column":21},"end":{"line":16,"column":1}},"5":{"start":{"line":13,"column":2},"end":{"line":15,"column":25}},"6":{"start":{"line":14,"column":14},"end":{"line":14,"column":44}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":5,"column":21},"end":{"line":5,"column":22}},"loc":{"start":{"line":5,"column":50},"end":{"line":10,"column":1}},"line":5},"1":{"name":"(anonymous_1)","decl":{"start":{"line":12,"column":21},"end":{"line":12,"column":22}},"loc":{"start":{"line":13,"column":2},"end":{"line":15,"column":25}},"line":13},"2":{"name":"(anonymous_2)","decl":{"start":{"line":14,"column":5},"end":{"line":14,"column":6}},"loc":{"start":{"line":14,"column":14},"end":{"line":14,"column":44}},"line":14}},"branchMap":{},"s":{"0":1,"1":1,"2":2,"3":2,"4":1,"5":3,"6":3},"f":{"0":2,"1":3,"2":3},"b":{},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"6ce0b108c1f289b7cef74a582ac7a72b69c39f57"} 6 | ,"/Users/kentcdodds/Desktop/react-testing-library-examples/src/utils.js": {"path":"/Users/kentcdodds/Desktop/react-testing-library-examples/src/utils.js","statementMap":{"0":{"start":{"line":4,"column":2},"end":{"line":4,"column":41}}},"fnMap":{"0":{"name":"reportError","decl":{"start":{"line":1,"column":9},"end":{"line":1,"column":20}},"loc":{"start":{"line":1,"column":23},"end":{"line":5,"column":1}},"line":1}},"branchMap":{},"s":{"0":0},"f":{"0":0},"b":{}} 7 | } 8 | -------------------------------------------------------------------------------- /coverage/lcov-report/base.css: -------------------------------------------------------------------------------- 1 | body, html { 2 | margin:0; padding: 0; 3 | height: 100%; 4 | } 5 | body { 6 | font-family: Helvetica Neue, Helvetica, Arial; 7 | font-size: 14px; 8 | color:#333; 9 | } 10 | .small { font-size: 12px; } 11 | *, *:after, *:before { 12 | -webkit-box-sizing:border-box; 13 | -moz-box-sizing:border-box; 14 | box-sizing:border-box; 15 | } 16 | h1 { font-size: 20px; margin: 0;} 17 | h2 { font-size: 14px; } 18 | pre { 19 | font: 12px/1.4 Consolas, "Liberation Mono", Menlo, Courier, monospace; 20 | margin: 0; 21 | padding: 0; 22 | -moz-tab-size: 2; 23 | -o-tab-size: 2; 24 | tab-size: 2; 25 | } 26 | a { color:#0074D9; text-decoration:none; } 27 | a:hover { text-decoration:underline; } 28 | .strong { font-weight: bold; } 29 | .space-top1 { padding: 10px 0 0 0; } 30 | .pad2y { padding: 20px 0; } 31 | .pad1y { padding: 10px 0; } 32 | .pad2x { padding: 0 20px; } 33 | .pad2 { padding: 20px; } 34 | .pad1 { padding: 10px; } 35 | .space-left2 { padding-left:55px; } 36 | .space-right2 { padding-right:20px; } 37 | .center { text-align:center; } 38 | .clearfix { display:block; } 39 | .clearfix:after { 40 | content:''; 41 | display:block; 42 | height:0; 43 | clear:both; 44 | visibility:hidden; 45 | } 46 | .fl { float: left; } 47 | @media only screen and (max-width:640px) { 48 | .col3 { width:100%; max-width:100%; } 49 | .hide-mobile { display:none!important; } 50 | } 51 | 52 | .quiet { 53 | color: #7f7f7f; 54 | color: rgba(0,0,0,0.5); 55 | } 56 | .quiet a { opacity: 0.7; } 57 | 58 | .fraction { 59 | font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; 60 | font-size: 10px; 61 | color: #555; 62 | background: #E8E8E8; 63 | padding: 4px 5px; 64 | border-radius: 3px; 65 | vertical-align: middle; 66 | } 67 | 68 | div.path a:link, div.path a:visited { color: #333; } 69 | table.coverage { 70 | border-collapse: collapse; 71 | margin: 10px 0 0 0; 72 | padding: 0; 73 | } 74 | 75 | table.coverage td { 76 | margin: 0; 77 | padding: 0; 78 | vertical-align: top; 79 | } 80 | table.coverage td.line-count { 81 | text-align: right; 82 | padding: 0 5px 0 20px; 83 | } 84 | table.coverage td.line-coverage { 85 | text-align: right; 86 | padding-right: 10px; 87 | min-width:20px; 88 | } 89 | 90 | table.coverage td span.cline-any { 91 | display: inline-block; 92 | padding: 0 5px; 93 | width: 100%; 94 | } 95 | .missing-if-branch { 96 | display: inline-block; 97 | margin-right: 5px; 98 | border-radius: 3px; 99 | position: relative; 100 | padding: 0 4px; 101 | background: #333; 102 | color: yellow; 103 | } 104 | 105 | .skip-if-branch { 106 | display: none; 107 | margin-right: 10px; 108 | position: relative; 109 | padding: 0 4px; 110 | background: #ccc; 111 | color: white; 112 | } 113 | .missing-if-branch .typ, .skip-if-branch .typ { 114 | color: inherit !important; 115 | } 116 | .coverage-summary { 117 | border-collapse: collapse; 118 | width: 100%; 119 | } 120 | .coverage-summary tr { border-bottom: 1px solid #bbb; } 121 | .keyline-all { border: 1px solid #ddd; } 122 | .coverage-summary td, .coverage-summary th { padding: 10px; } 123 | .coverage-summary tbody { border: 1px solid #bbb; } 124 | .coverage-summary td { border-right: 1px solid #bbb; } 125 | .coverage-summary td:last-child { border-right: none; } 126 | .coverage-summary th { 127 | text-align: left; 128 | font-weight: normal; 129 | white-space: nowrap; 130 | } 131 | .coverage-summary th.file { border-right: none !important; } 132 | .coverage-summary th.pct { } 133 | .coverage-summary th.pic, 134 | .coverage-summary th.abs, 135 | .coverage-summary td.pct, 136 | .coverage-summary td.abs { text-align: right; } 137 | .coverage-summary td.file { white-space: nowrap; } 138 | .coverage-summary td.pic { min-width: 120px !important; } 139 | .coverage-summary tfoot td { } 140 | 141 | .coverage-summary .sorter { 142 | height: 10px; 143 | width: 7px; 144 | display: inline-block; 145 | margin-left: 0.5em; 146 | background: url(sort-arrow-sprite.png) no-repeat scroll 0 0 transparent; 147 | } 148 | .coverage-summary .sorted .sorter { 149 | background-position: 0 -20px; 150 | } 151 | .coverage-summary .sorted-desc .sorter { 152 | background-position: 0 -10px; 153 | } 154 | .status-line { height: 10px; } 155 | /* yellow */ 156 | .cbranch-no { background: yellow !important; color: #111; } 157 | /* dark red */ 158 | .red.solid, .status-line.low, .low .cover-fill { background:#C21F39 } 159 | .low .chart { border:1px solid #C21F39 } 160 | .highlighted, 161 | .highlighted .cstat-no, .highlighted .fstat-no, .highlighted .cbranch-no{ 162 | background: #C21F39 !important; 163 | } 164 | /* medium red */ 165 | .cstat-no, .fstat-no, .cbranch-no, .cbranch-no { background:#F6C6CE } 166 | /* light red */ 167 | .low, .cline-no { background:#FCE1E5 } 168 | /* light green */ 169 | .high, .cline-yes { background:rgb(230,245,208) } 170 | /* medium green */ 171 | .cstat-yes { background:rgb(161,215,106) } 172 | /* dark green */ 173 | .status-line.high, .high .cover-fill { background:rgb(77,146,33) } 174 | .high .chart { border:1px solid rgb(77,146,33) } 175 | /* dark yellow (gold) */ 176 | .status-line.medium, .medium .cover-fill { background: #f9cd0b; } 177 | .medium .chart { border:1px solid #f9cd0b; } 178 | /* light yellow */ 179 | .medium { background: #fff4c2; } 180 | 181 | .cstat-skip { background: #ddd; color: #111; } 182 | .fstat-skip { background: #ddd; color: #111 !important; } 183 | .cbranch-skip { background: #ddd !important; color: #111; } 184 | 185 | span.cline-neutral { background: #eaeaea; } 186 | 187 | .coverage-summary td.empty { 188 | opacity: .5; 189 | padding-top: 4px; 190 | padding-bottom: 4px; 191 | line-height: 1; 192 | color: #888; 193 | } 194 | 195 | .cover-fill, .cover-empty { 196 | display:inline-block; 197 | height: 12px; 198 | } 199 | .chart { 200 | line-height: 0; 201 | } 202 | .cover-empty { 203 | background: white; 204 | } 205 | .cover-full { 206 | border-right: none !important; 207 | } 208 | pre.prettyprint { 209 | border: none !important; 210 | padding: 0 !important; 211 | margin: 0 !important; 212 | } 213 | .com { color: #999 !important; } 214 | .ignore-none { color: #999; font-weight: normal; } 215 | 216 | .wrapper { 217 | min-height: 100%; 218 | height: auto !important; 219 | height: 100%; 220 | margin: 0 auto -48px; 221 | } 222 | .footer, .push { 223 | height: 48px; 224 | } 225 | -------------------------------------------------------------------------------- /coverage/lcov-report/block-navigation.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable */ 2 | var jumpToCode = (function init() { 3 | // Classes of code we would like to highlight in the file view 4 | var missingCoverageClasses = ['.cbranch-no', '.cstat-no', '.fstat-no']; 5 | 6 | // Elements to highlight in the file listing view 7 | var fileListingElements = ['td.pct.low']; 8 | 9 | // We don't want to select elements that are direct descendants of another match 10 | var notSelector = ':not(' + missingCoverageClasses.join('):not(') + ') > '; // becomes `:not(a):not(b) > ` 11 | 12 | // Selecter that finds elements on the page to which we can jump 13 | var selector = 14 | fileListingElements.join(', ') + 15 | ', ' + 16 | notSelector + 17 | missingCoverageClasses.join(', ' + notSelector); // becomes `:not(a):not(b) > a, :not(a):not(b) > b` 18 | 19 | // The NodeList of matching elements 20 | var missingCoverageElements = document.querySelectorAll(selector); 21 | 22 | var currentIndex; 23 | 24 | function toggleClass(index) { 25 | missingCoverageElements 26 | .item(currentIndex) 27 | .classList.remove('highlighted'); 28 | missingCoverageElements.item(index).classList.add('highlighted'); 29 | } 30 | 31 | function makeCurrent(index) { 32 | toggleClass(index); 33 | currentIndex = index; 34 | missingCoverageElements.item(index).scrollIntoView({ 35 | behavior: 'smooth', 36 | block: 'center', 37 | inline: 'center' 38 | }); 39 | } 40 | 41 | function goToPrevious() { 42 | var nextIndex = 0; 43 | if (typeof currentIndex !== 'number' || currentIndex === 0) { 44 | nextIndex = missingCoverageElements.length - 1; 45 | } else if (missingCoverageElements.length > 1) { 46 | nextIndex = currentIndex - 1; 47 | } 48 | 49 | makeCurrent(nextIndex); 50 | } 51 | 52 | function goToNext() { 53 | var nextIndex = 0; 54 | 55 | if ( 56 | typeof currentIndex === 'number' && 57 | currentIndex < missingCoverageElements.length - 1 58 | ) { 59 | nextIndex = currentIndex + 1; 60 | } 61 | 62 | makeCurrent(nextIndex); 63 | } 64 | 65 | return function jump(event) { 66 | switch (event.which) { 67 | case 78: // n 68 | case 74: // j 69 | goToNext(); 70 | break; 71 | case 66: // b 72 | case 75: // k 73 | case 80: // p 74 | goToPrevious(); 75 | break; 76 | } 77 | }; 78 | })(); 79 | window.addEventListener('keydown', jumpToCode); 80 | -------------------------------------------------------------------------------- /coverage/lcov-report/component-did-catch.js.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Code coverage report for component-did-catch.js 7 | 8 | 9 | 10 | 11 | 12 | 17 | 18 | 19 | 20 |
21 |
22 |

All files component-did-catch.js

23 |
24 | 25 |
26 | 100% 27 | Statements 28 | 9/9 29 |
30 | 31 | 32 |
33 | 100% 34 | Branches 35 | 4/4 36 |
37 | 38 | 39 |
40 | 100% 41 | Functions 42 | 5/5 43 |
44 | 45 | 46 |
47 | 100% 48 | Lines 49 | 8/8 50 |
51 | 52 | 53 |
54 |

55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |

57 |
58 |
59 |

 60 | 
1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8 68 | 9 69 | 10 70 | 11 71 | 12 72 | 13 73 | 14 74 | 15 75 | 16 76 | 17 77 | 18 78 | 19 79 | 20 80 | 21 81 | 22 82 | 23 83 | 24 84 | 25 85 | 26 86 | 27 87 | 28 88 | 29 89 | 30 90 | 31 91 | 32 92 | 33 93 | 34 94 | 35 95 | 36 96 | 37 97 | 38 98 | 39 99 | 40 100 | 41 101 | 42 102 | 43 103 | 44 104 | 45 105 | 46  106 |   107 |   108 |   109 | 1x 110 |   111 | 1x 112 | 1x 113 |   114 |   115 | 3x 116 |   117 |   118 |   119 |   120 |   121 |   122 |   123 |   124 |   125 | 2x 126 |   127 |   128 |   129 |   130 | 1x 131 | 1x 132 |   133 | 2x 134 |   135 |   136 |   137 |   138 |   139 |   140 |   141 |   142 |   143 |   144 |   145 |   146 |   147 |   148 |   149 |   150 |  
import * as React from 'react'
151 | import {reportError} from './utils'
152 |  
153 | class ErrorBoundary extends React.Component {
154 |   state = {hasError: false}
155 |   componentDidCatch(error, info) {
156 |     this.setState({hasError: true})
157 |     reportError(error, info)
158 |   }
159 |   render() {
160 |     return this.state.hasError ? (
161 |       <div>There was a problem</div>
162 |     ) : (
163 |       this.props.children
164 |     )
165 |   }
166 | }
167 |  
168 | class Bomb extends React.Component {
169 |   render() {
170 |     return this['💣'].caboom()
171 |   }
172 | }
173 |  
174 | class BombButton extends React.Component {
175 |   state = {renderBomb: false}
176 |   handleBombClick = () => this.setState({renderBomb: true})
177 |   render() {
178 |     return (
179 |       <ErrorBoundary>
180 |         {this.state.renderBomb ? (
181 |           <Bomb />
182 |         ) : (
183 |           <button onClick={this.handleBombClick}>
184 |             <span role="img" aria-label="bomb">
185 |               💣
186 |             </span>
187 |           </button>
188 |         )}
189 |       </ErrorBoundary>
190 |     )
191 |   }
192 | }
193 |  
194 | export {BombButton}
195 |  
196 | 197 |
198 |
199 | 204 | 205 | 206 | 211 | 212 | 213 | 214 | 215 | -------------------------------------------------------------------------------- /coverage/lcov-report/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kentcdodds/react-testing-library-examples/def00171bc999e00bc4f75ffa15e6db9754ad854/coverage/lcov-report/favicon.png -------------------------------------------------------------------------------- /coverage/lcov-report/fetch-greeting.js.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Code coverage report for fetch-greeting.js 7 | 8 | 9 | 10 | 11 | 12 | 17 | 18 | 19 | 20 |
21 |
22 |

All files fetch-greeting.js

23 |
24 | 25 |
26 | 93.75% 27 | Statements 28 | 15/16 29 |
30 | 31 | 32 |
33 | 88.89% 34 | Branches 35 | 8/9 36 |
37 | 38 | 39 |
40 | 100% 41 | Functions 42 | 5/5 43 |
44 | 45 | 46 |
47 | 93.75% 48 | Lines 49 | 15/16 50 |
51 | 52 | 53 |
54 |

55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |

57 |
58 |
59 |

 60 | 
1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8 68 | 9 69 | 10 70 | 11 71 | 12 72 | 13 73 | 14 74 | 15 75 | 16 76 | 17 77 | 18 78 | 19 79 | 20 80 | 21 81 | 22 82 | 23 83 | 24 84 | 25 85 | 26 86 | 27 87 | 28 88 | 29 89 | 30 90 | 31 91 | 32 92 | 33 93 | 34 94 | 35 95 | 36 96 | 37 97 | 38 98 | 39 99 | 40 100 | 41 101 | 42 102 | 43 103 | 44 104 | 45 105 | 46 106 | 47 107 | 48 108 | 49 109 | 50 110 | 51 111 | 52 112 | 53 113 | 54 114 | 55 115 | 56 116 | 57 117 | 58 118 | 59 119 | 60 120 | 61 121 | 62 122 | 63 123 | 64 124 | 65  125 |   126 |   127 |   128 | 1x 129 |   130 |   131 |   132 |   133 |   134 | 2x 135 |   136 | 1x 137 |   138 |   139 |   140 |   141 |   142 | 1x 143 |   144 |   145 |   146 |   147 |   148 |   149 |   150 |   151 |   152 |   153 |   154 | 5x 155 |   156 |   157 |   158 | 5x 159 |   160 | 5x 161 | 2x 162 |   163 |   164 | 1x 165 | 1x 166 | 1x 167 | 1x 168 |   169 |   170 | 1x 171 |   172 |   173 |   174 | 5x 175 |   176 | 5x 177 |   178 |   179 |   180 |   181 |   182 |   183 |   184 |   185 |   186 |   187 |   188 |  
import * as React from 'react'
189 | import {useState, useReducer} from 'react'
190 | import axios from 'axios'
191 |  
192 | const initialState = {
193 |   error: null,
194 |   greeting: null,
195 | }
196 |  
197 | function greetingReducer(state, action) {
198 |   switch (action.type) {
199 |     case 'SUCCESS': {
200 |       return {
201 |         error: null,
202 |         greeting: action.greeting,
203 |       }
204 |     }
205 |     case 'ERROR': {
206 |       return {
207 |         error: action.error,
208 |         greeting: null,
209 |       }
210 |     }
211 |     default: {
212 |       return state
213 |     }
214 |   }
215 | }
216 |  
217 | function FetchGreeting() {
218 |   const [{error, greeting}, dispatch] = useReducer(
219 |     greetingReducer,
220 |     initialState,
221 |   )
222 |   const [buttonClicked, setButtonClicked] = useState(false)
223 |  
224 |   const fetchGreeting = async () => {
225 |     axios
226 |       .get('/greeting')
227 |       .then((response) => {
228 |         const {data} = response
229 |         const {greeting} = data
230 |         dispatch({type: 'SUCCESS', greeting})
231 |         setButtonClicked(true)
232 |       })
233 |       .catch((error) => {
234 |         dispatch({type: 'ERROR', error})
235 |       })
236 |   }
237 |  
238 |   const buttonText = buttonClicked ? 'Ok' : 'Load Greeting'
239 |  
240 |   return (
241 |     <div>
242 |       <button onClick={fetchGreeting} disabled={buttonClicked}>
243 |         {buttonText}
244 |       </button>
245 |       {greeting && <h1>{greeting}</h1>}
246 |       {error && <p role="alert">Oops, failed to fetch!</p>}
247 |     </div>
248 |   )
249 | }
250 |  
251 | export {FetchGreeting}
252 |  
253 | 254 |
255 |
256 | 261 | 262 | 263 | 268 | 269 | 270 | 271 | 272 | -------------------------------------------------------------------------------- /coverage/lcov-report/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Code coverage report for All files 7 | 8 | 9 | 10 | 11 | 12 | 17 | 18 | 19 | 20 |
21 |
22 |

All files

23 |
24 | 25 |
26 | 88.89% 27 | Statements 28 | 32/36 29 |
30 | 31 | 32 |
33 | 92.31% 34 | Branches 35 | 12/13 36 |
37 | 38 | 39 |
40 | 87.5% 41 | Functions 42 | 14/16 43 |
44 | 45 | 46 |
47 | 88.57% 48 | Lines 49 | 31/35 50 |
51 | 52 | 53 |
54 |

55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |

57 |
58 |
59 |
60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 |
FileStatementsBranchesFunctionsLines
component-did-catch.js 78 |
79 |
100%9/9100%4/4100%5/5100%8/8
fetch-greeting.js 93 |
94 |
93.75%15/1688.89%8/9100%5/593.75%15/16
index.js 108 |
109 |
0%0/2100%0/00%0/10%0/2
lazy-component.js 123 |
124 |
100%1/1100%0/0100%1/1100%1/1
react-context.js 138 |
139 |
100%7/7100%0/0100%3/3100%7/7
utils.js 153 |
154 |
0%0/1100%0/00%0/10%0/1
167 |
168 |
169 |
170 | 175 | 176 | 177 | 182 | 183 | 184 | 185 | 186 | -------------------------------------------------------------------------------- /coverage/lcov-report/index.js.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Code coverage report for index.js 7 | 8 | 9 | 10 | 11 | 12 | 17 | 18 | 19 | 20 |
21 |
22 |

All files index.js

23 |
24 | 25 |
26 | 0% 27 | Statements 28 | 0/2 29 |
30 | 31 | 32 |
33 | 100% 34 | Branches 35 | 0/0 36 |
37 | 38 | 39 |
40 | 0% 41 | Functions 42 | 0/1 43 |
44 | 45 | 46 |
47 | 0% 48 | Lines 49 | 0/2 50 |
51 | 52 | 53 |
54 |

55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |

57 |
58 |
59 |

 60 | 
1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8 68 | 9 69 | 10 70 | 11 71 | 12 72 | 13 73 | 14 74 | 15 75 | 16 76 | 17 77 | 18 78 | 19 79 | 20 80 | 21 81 | 22 82 | 23 83 | 24 84 | 25 85 | 26 86 | 27 87 | 28 88 | 29 89 | 30 90 | 31 91 | 32 92 | 33 93 | 34 94 | 35 95 | 36 96 | 37 97 | 38 98 | 39 99 | 40 100 | 41 101 | 42  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 |  
import * as React from 'react'
143 | import {render} from 'react-dom'
144 |  
145 | function App() {
146 |   return (
147 |     <div>
148 |       <h1>
149 |         react-testing-library examples{' '}
150 |         <span role="img" aria-label="goat">
151 |           🐐
152 |         </span>
153 |       </h1>
154 |       <div>
155 |         <p>{`
156 |           This is an example project of how to test react components using
157 |           react-testing-library and Jest. The idea is that most of the tests
158 |           should be runnable in codesandbox to make it easy to try things out,
159 |           fork, contribute, etc. However, there will be some tests which do not
160 |           work in codesandbox and we'll put those in a special folder called
161 |           __local_tests__. Locally (and in CI) all tests are run via the "test"
162 |           script, in codesandbox only the tests in __tests__ will be run.
163 |           If you can make it work in codesandbox then do because it's a lot
164 |           easier for people to tinker with things in codesandbox.
165 |         `}</p>
166 |         <p>
167 |           <a href="https://codesandbox.io/s/github/kentcdodds/react-testing-library-examples">
168 |             Open this in codesandbox
169 |           </a>{' '}
170 |           to play around with things. You can also use the codesandbox to
171 |           contribute to{' '}
172 |           <a href="https://github.com/kentcdodds/react-testing-library-examples">
173 |             the project on GitHub
174 |           </a>
175 |           !
176 |         </p>
177 |       </div>
178 |     </div>
179 |   )
180 | }
181 |  
182 | render(<App />, document.getElementById('root'))
183 |  
184 | 185 |
186 |
187 | 192 | 193 | 194 | 199 | 200 | 201 | 202 | 203 | -------------------------------------------------------------------------------- /coverage/lcov-report/lazy-component.js.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Code coverage report for lazy-component.js 7 | 8 | 9 | 10 | 11 | 12 | 17 | 18 | 19 | 20 |
21 |
22 |

All files lazy-component.js

23 |
24 | 25 |
26 | 100% 27 | Statements 28 | 1/1 29 |
30 | 31 | 32 |
33 | 100% 34 | Branches 35 | 0/0 36 |
37 | 38 | 39 |
40 | 100% 41 | Functions 42 | 1/1 43 |
44 | 45 | 46 |
47 | 100% 48 | Lines 49 | 1/1 50 |
51 | 52 | 53 |
54 |

55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |

57 |
58 |
59 |

 60 | 
1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8  68 |   69 |   70 | 2x 71 |   72 |   73 |   74 |  
import * as React from 'react'
 75 |  
 76 | function LazyComponent() {
 77 |   return <div>I am lazy!</div>
 78 | }
 79 |  
 80 | export default LazyComponent
 81 |  
82 | 83 |
84 |
85 | 90 | 91 | 92 | 97 | 98 | 99 | 100 | 101 | -------------------------------------------------------------------------------- /coverage/lcov-report/prettify.css: -------------------------------------------------------------------------------- 1 | .pln{color:#000}@media screen{.str{color:#080}.kwd{color:#008}.com{color:#800}.typ{color:#606}.lit{color:#066}.pun,.opn,.clo{color:#660}.tag{color:#008}.atn{color:#606}.atv{color:#080}.dec,.var{color:#606}.fun{color:red}}@media print,projection{.str{color:#060}.kwd{color:#006;font-weight:bold}.com{color:#600;font-style:italic}.typ{color:#404;font-weight:bold}.lit{color:#044}.pun,.opn,.clo{color:#440}.tag{color:#006;font-weight:bold}.atn{color:#404}.atv{color:#060}}pre.prettyprint{padding:2px;border:1px solid #888}ol.linenums{margin-top:0;margin-bottom:0}li.L0,li.L1,li.L2,li.L3,li.L5,li.L6,li.L7,li.L8{list-style-type:none}li.L1,li.L3,li.L5,li.L7,li.L9{background:#eee} 2 | -------------------------------------------------------------------------------- /coverage/lcov-report/prettify.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable */ 2 | window.PR_SHOULD_USE_CONTINUATION=true;(function(){var h=["break,continue,do,else,for,if,return,while"];var u=[h,"auto,case,char,const,default,double,enum,extern,float,goto,int,long,register,short,signed,sizeof,static,struct,switch,typedef,union,unsigned,void,volatile"];var p=[u,"catch,class,delete,false,import,new,operator,private,protected,public,this,throw,true,try,typeof"];var l=[p,"alignof,align_union,asm,axiom,bool,concept,concept_map,const_cast,constexpr,decltype,dynamic_cast,explicit,export,friend,inline,late_check,mutable,namespace,nullptr,reinterpret_cast,static_assert,static_cast,template,typeid,typename,using,virtual,where"];var x=[p,"abstract,boolean,byte,extends,final,finally,implements,import,instanceof,null,native,package,strictfp,super,synchronized,throws,transient"];var R=[x,"as,base,by,checked,decimal,delegate,descending,dynamic,event,fixed,foreach,from,group,implicit,in,interface,internal,into,is,lock,object,out,override,orderby,params,partial,readonly,ref,sbyte,sealed,stackalloc,string,select,uint,ulong,unchecked,unsafe,ushort,var"];var r="all,and,by,catch,class,else,extends,false,finally,for,if,in,is,isnt,loop,new,no,not,null,of,off,on,or,return,super,then,true,try,unless,until,when,while,yes";var w=[p,"debugger,eval,export,function,get,null,set,undefined,var,with,Infinity,NaN"];var s="caller,delete,die,do,dump,elsif,eval,exit,foreach,for,goto,if,import,last,local,my,next,no,our,print,package,redo,require,sub,undef,unless,until,use,wantarray,while,BEGIN,END";var I=[h,"and,as,assert,class,def,del,elif,except,exec,finally,from,global,import,in,is,lambda,nonlocal,not,or,pass,print,raise,try,with,yield,False,True,None"];var f=[h,"alias,and,begin,case,class,def,defined,elsif,end,ensure,false,in,module,next,nil,not,or,redo,rescue,retry,self,super,then,true,undef,unless,until,when,yield,BEGIN,END"];var H=[h,"case,done,elif,esac,eval,fi,function,in,local,set,then,until"];var A=[l,R,w,s+I,f,H];var e=/^(DIR|FILE|vector|(de|priority_)?queue|list|stack|(const_)?iterator|(multi)?(set|map)|bitset|u?(int|float)\d*)/;var C="str";var z="kwd";var j="com";var O="typ";var G="lit";var L="pun";var F="pln";var m="tag";var E="dec";var J="src";var P="atn";var n="atv";var N="nocode";var M="(?:^^\\.?|[+-]|\\!|\\!=|\\!==|\\#|\\%|\\%=|&|&&|&&=|&=|\\(|\\*|\\*=|\\+=|\\,|\\-=|\\->|\\/|\\/=|:|::|\\;|<|<<|<<=|<=|=|==|===|>|>=|>>|>>=|>>>|>>>=|\\?|\\@|\\[|\\^|\\^=|\\^\\^|\\^\\^=|\\{|\\||\\|=|\\|\\||\\|\\|=|\\~|break|case|continue|delete|do|else|finally|instanceof|return|throw|try|typeof)\\s*";function k(Z){var ad=0;var S=false;var ac=false;for(var V=0,U=Z.length;V122)){if(!(al<65||ag>90)){af.push([Math.max(65,ag)|32,Math.min(al,90)|32])}if(!(al<97||ag>122)){af.push([Math.max(97,ag)&~32,Math.min(al,122)&~32])}}}}af.sort(function(av,au){return(av[0]-au[0])||(au[1]-av[1])});var ai=[];var ap=[NaN,NaN];for(var ar=0;arat[0]){if(at[1]+1>at[0]){an.push("-")}an.push(T(at[1]))}}an.push("]");return an.join("")}function W(al){var aj=al.source.match(new RegExp("(?:\\[(?:[^\\x5C\\x5D]|\\\\[\\s\\S])*\\]|\\\\u[A-Fa-f0-9]{4}|\\\\x[A-Fa-f0-9]{2}|\\\\[0-9]+|\\\\[^ux0-9]|\\(\\?[:!=]|[\\(\\)\\^]|[^\\x5B\\x5C\\(\\)\\^]+)","g"));var ah=aj.length;var an=[];for(var ak=0,am=0;ak=2&&ai==="["){aj[ak]=X(ag)}else{if(ai!=="\\"){aj[ak]=ag.replace(/[a-zA-Z]/g,function(ao){var ap=ao.charCodeAt(0);return"["+String.fromCharCode(ap&~32,ap|32)+"]"})}}}}return aj.join("")}var aa=[];for(var V=0,U=Z.length;V=0;){S[ac.charAt(ae)]=Y}}var af=Y[1];var aa=""+af;if(!ag.hasOwnProperty(aa)){ah.push(af);ag[aa]=null}}ah.push(/[\0-\uffff]/);V=k(ah)})();var X=T.length;var W=function(ah){var Z=ah.sourceCode,Y=ah.basePos;var ad=[Y,F];var af=0;var an=Z.match(V)||[];var aj={};for(var ae=0,aq=an.length;ae=5&&"lang-"===ap.substring(0,5);if(am&&!(ai&&typeof ai[1]==="string")){am=false;ap=J}if(!am){aj[ag]=ap}}var ab=af;af+=ag.length;if(!am){ad.push(Y+ab,ap)}else{var al=ai[1];var ak=ag.indexOf(al);var ac=ak+al.length;if(ai[2]){ac=ag.length-ai[2].length;ak=ac-al.length}var ar=ap.substring(5);B(Y+ab,ag.substring(0,ak),W,ad);B(Y+ab+ak,al,q(ar,al),ad);B(Y+ab+ac,ag.substring(ac),W,ad)}}ah.decorations=ad};return W}function i(T){var W=[],S=[];if(T.tripleQuotedStrings){W.push([C,/^(?:\'\'\'(?:[^\'\\]|\\[\s\S]|\'{1,2}(?=[^\']))*(?:\'\'\'|$)|\"\"\"(?:[^\"\\]|\\[\s\S]|\"{1,2}(?=[^\"]))*(?:\"\"\"|$)|\'(?:[^\\\']|\\[\s\S])*(?:\'|$)|\"(?:[^\\\"]|\\[\s\S])*(?:\"|$))/,null,"'\""])}else{if(T.multiLineStrings){W.push([C,/^(?:\'(?:[^\\\']|\\[\s\S])*(?:\'|$)|\"(?:[^\\\"]|\\[\s\S])*(?:\"|$)|\`(?:[^\\\`]|\\[\s\S])*(?:\`|$))/,null,"'\"`"])}else{W.push([C,/^(?:\'(?:[^\\\'\r\n]|\\.)*(?:\'|$)|\"(?:[^\\\"\r\n]|\\.)*(?:\"|$))/,null,"\"'"])}}if(T.verbatimStrings){S.push([C,/^@\"(?:[^\"]|\"\")*(?:\"|$)/,null])}var Y=T.hashComments;if(Y){if(T.cStyleComments){if(Y>1){W.push([j,/^#(?:##(?:[^#]|#(?!##))*(?:###|$)|.*)/,null,"#"])}else{W.push([j,/^#(?:(?:define|elif|else|endif|error|ifdef|include|ifndef|line|pragma|undef|warning)\b|[^\r\n]*)/,null,"#"])}S.push([C,/^<(?:(?:(?:\.\.\/)*|\/?)(?:[\w-]+(?:\/[\w-]+)+)?[\w-]+\.h|[a-z]\w*)>/,null])}else{W.push([j,/^#[^\r\n]*/,null,"#"])}}if(T.cStyleComments){S.push([j,/^\/\/[^\r\n]*/,null]);S.push([j,/^\/\*[\s\S]*?(?:\*\/|$)/,null])}if(T.regexLiterals){var X=("/(?=[^/*])(?:[^/\\x5B\\x5C]|\\x5C[\\s\\S]|\\x5B(?:[^\\x5C\\x5D]|\\x5C[\\s\\S])*(?:\\x5D|$))+/");S.push(["lang-regex",new RegExp("^"+M+"("+X+")")])}var V=T.types;if(V){S.push([O,V])}var U=(""+T.keywords).replace(/^ | $/g,"");if(U.length){S.push([z,new RegExp("^(?:"+U.replace(/[\s,]+/g,"|")+")\\b"),null])}W.push([F,/^\s+/,null," \r\n\t\xA0"]);S.push([G,/^@[a-z_$][a-z_$@0-9]*/i,null],[O,/^(?:[@_]?[A-Z]+[a-z][A-Za-z_$@0-9]*|\w+_t\b)/,null],[F,/^[a-z_$][a-z_$@0-9]*/i,null],[G,new RegExp("^(?:0x[a-f0-9]+|(?:\\d(?:_\\d+)*\\d*(?:\\.\\d*)?|\\.\\d\\+)(?:e[+\\-]?\\d+)?)[a-z]*","i"),null,"0123456789"],[F,/^\\[\s\S]?/,null],[L,/^.[^\s\w\.$@\'\"\`\/\#\\]*/,null]);return g(W,S)}var K=i({keywords:A,hashComments:true,cStyleComments:true,multiLineStrings:true,regexLiterals:true});function Q(V,ag){var U=/(?:^|\s)nocode(?:\s|$)/;var ab=/\r\n?|\n/;var ac=V.ownerDocument;var S;if(V.currentStyle){S=V.currentStyle.whiteSpace}else{if(window.getComputedStyle){S=ac.defaultView.getComputedStyle(V,null).getPropertyValue("white-space")}}var Z=S&&"pre"===S.substring(0,3);var af=ac.createElement("LI");while(V.firstChild){af.appendChild(V.firstChild)}var W=[af];function ae(al){switch(al.nodeType){case 1:if(U.test(al.className)){break}if("BR"===al.nodeName){ad(al);if(al.parentNode){al.parentNode.removeChild(al)}}else{for(var an=al.firstChild;an;an=an.nextSibling){ae(an)}}break;case 3:case 4:if(Z){var am=al.nodeValue;var aj=am.match(ab);if(aj){var ai=am.substring(0,aj.index);al.nodeValue=ai;var ah=am.substring(aj.index+aj[0].length);if(ah){var ak=al.parentNode;ak.insertBefore(ac.createTextNode(ah),al.nextSibling)}ad(al);if(!ai){al.parentNode.removeChild(al)}}}break}}function ad(ak){while(!ak.nextSibling){ak=ak.parentNode;if(!ak){return}}function ai(al,ar){var aq=ar?al.cloneNode(false):al;var ao=al.parentNode;if(ao){var ap=ai(ao,1);var an=al.nextSibling;ap.appendChild(aq);for(var am=an;am;am=an){an=am.nextSibling;ap.appendChild(am)}}return aq}var ah=ai(ak.nextSibling,0);for(var aj;(aj=ah.parentNode)&&aj.nodeType===1;){ah=aj}W.push(ah)}for(var Y=0;Y=S){ah+=2}if(V>=ap){Z+=2}}}var t={};function c(U,V){for(var S=V.length;--S>=0;){var T=V[S];if(!t.hasOwnProperty(T)){t[T]=U}else{if(window.console){console.warn("cannot override language handler %s",T)}}}}function q(T,S){if(!(T&&t.hasOwnProperty(T))){T=/^\s*]*(?:>|$)/],[j,/^<\!--[\s\S]*?(?:-\->|$)/],["lang-",/^<\?([\s\S]+?)(?:\?>|$)/],["lang-",/^<%([\s\S]+?)(?:%>|$)/],[L,/^(?:<[%?]|[%?]>)/],["lang-",/^]*>([\s\S]+?)<\/xmp\b[^>]*>/i],["lang-js",/^]*>([\s\S]*?)(<\/script\b[^>]*>)/i],["lang-css",/^]*>([\s\S]*?)(<\/style\b[^>]*>)/i],["lang-in.tag",/^(<\/?[a-z][^<>]*>)/i]]),["default-markup","htm","html","mxml","xhtml","xml","xsl"]);c(g([[F,/^[\s]+/,null," \t\r\n"],[n,/^(?:\"[^\"]*\"?|\'[^\']*\'?)/,null,"\"'"]],[[m,/^^<\/?[a-z](?:[\w.:-]*\w)?|\/?>$/i],[P,/^(?!style[\s=]|on)[a-z](?:[\w:-]*\w)?/i],["lang-uq.val",/^=\s*([^>\'\"\s]*(?:[^>\'\"\s\/]|\/(?=\s)))/],[L,/^[=<>\/]+/],["lang-js",/^on\w+\s*=\s*\"([^\"]+)\"/i],["lang-js",/^on\w+\s*=\s*\'([^\']+)\'/i],["lang-js",/^on\w+\s*=\s*([^\"\'>\s]+)/i],["lang-css",/^style\s*=\s*\"([^\"]+)\"/i],["lang-css",/^style\s*=\s*\'([^\']+)\'/i],["lang-css",/^style\s*=\s*([^\"\'>\s]+)/i]]),["in.tag"]);c(g([],[[n,/^[\s\S]+/]]),["uq.val"]);c(i({keywords:l,hashComments:true,cStyleComments:true,types:e}),["c","cc","cpp","cxx","cyc","m"]);c(i({keywords:"null,true,false"}),["json"]);c(i({keywords:R,hashComments:true,cStyleComments:true,verbatimStrings:true,types:e}),["cs"]);c(i({keywords:x,cStyleComments:true}),["java"]);c(i({keywords:H,hashComments:true,multiLineStrings:true}),["bsh","csh","sh"]);c(i({keywords:I,hashComments:true,multiLineStrings:true,tripleQuotedStrings:true}),["cv","py"]);c(i({keywords:s,hashComments:true,multiLineStrings:true,regexLiterals:true}),["perl","pl","pm"]);c(i({keywords:f,hashComments:true,multiLineStrings:true,regexLiterals:true}),["rb"]);c(i({keywords:w,cStyleComments:true,regexLiterals:true}),["js"]);c(i({keywords:r,hashComments:3,cStyleComments:true,multilineStrings:true,tripleQuotedStrings:true,regexLiterals:true}),["coffee"]);c(g([],[[C,/^[\s\S]+/]]),["regex"]);function d(V){var U=V.langExtension;try{var S=a(V.sourceNode);var T=S.sourceCode;V.sourceCode=T;V.spans=S.spans;V.basePos=0;q(U,T)(V);D(V)}catch(W){if("console" in window){console.log(W&&W.stack?W.stack:W)}}}function y(W,V,U){var S=document.createElement("PRE");S.innerHTML=W;if(U){Q(S,U)}var T={langExtension:V,numberLines:U,sourceNode:S};d(T);return S.innerHTML}function b(ad){function Y(af){return document.getElementsByTagName(af)}var ac=[Y("pre"),Y("code"),Y("xmp")];var T=[];for(var aa=0;aa=0){var ah=ai.match(ab);var am;if(!ah&&(am=o(aj))&&"CODE"===am.tagName){ah=am.className.match(ab)}if(ah){ah=ah[1]}var al=false;for(var ak=aj.parentNode;ak;ak=ak.parentNode){if((ak.tagName==="pre"||ak.tagName==="code"||ak.tagName==="xmp")&&ak.className&&ak.className.indexOf("prettyprint")>=0){al=true;break}}if(!al){var af=aj.className.match(/\blinenums\b(?::(\d+))?/);af=af?af[1]&&af[1].length?+af[1]:true:false;if(af){Q(aj,af)}S={langExtension:ah,sourceNode:aj,numberLines:af};d(S)}}}if(X]*(?:>|$)/],[PR.PR_COMMENT,/^<\!--[\s\S]*?(?:-\->|$)/],[PR.PR_PUNCTUATION,/^(?:<[%?]|[%?]>)/],["lang-",/^<\?([\s\S]+?)(?:\?>|$)/],["lang-",/^<%([\s\S]+?)(?:%>|$)/],["lang-",/^]*>([\s\S]+?)<\/xmp\b[^>]*>/i],["lang-handlebars",/^]*type\s*=\s*['"]?text\/x-handlebars-template['"]?\b[^>]*>([\s\S]*?)(<\/script\b[^>]*>)/i],["lang-js",/^]*>([\s\S]*?)(<\/script\b[^>]*>)/i],["lang-css",/^]*>([\s\S]*?)(<\/style\b[^>]*>)/i],["lang-in.tag",/^(<\/?[a-z][^<>]*>)/i],[PR.PR_DECLARATION,/^{{[#^>/]?\s*[\w.][^}]*}}/],[PR.PR_DECLARATION,/^{{&?\s*[\w.][^}]*}}/],[PR.PR_DECLARATION,/^{{{>?\s*[\w.][^}]*}}}/],[PR.PR_COMMENT,/^{{![^}]*}}/]]),["handlebars","hbs"]);PR.registerLangHandler(PR.createSimpleLexer([[PR.PR_PLAIN,/^[ \t\r\n\f]+/,null," \t\r\n\f"]],[[PR.PR_STRING,/^\"(?:[^\n\r\f\\\"]|\\(?:\r\n?|\n|\f)|\\[\s\S])*\"/,null],[PR.PR_STRING,/^\'(?:[^\n\r\f\\\']|\\(?:\r\n?|\n|\f)|\\[\s\S])*\'/,null],["lang-css-str",/^url\(([^\)\"\']*)\)/i],[PR.PR_KEYWORD,/^(?:url|rgb|\!important|@import|@page|@media|@charset|inherit)(?=[^\-\w]|$)/i,null],["lang-css-kw",/^(-?(?:[_a-z]|(?:\\[0-9a-f]+ ?))(?:[_a-z0-9\-]|\\(?:\\[0-9a-f]+ ?))*)\s*:/i],[PR.PR_COMMENT,/^\/\*[^*]*\*+(?:[^\/*][^*]*\*+)*\//],[PR.PR_COMMENT,/^(?:)/],[PR.PR_LITERAL,/^(?:\d+|\d*\.\d+)(?:%|[a-z]+)?/i],[PR.PR_LITERAL,/^#(?:[0-9a-f]{3}){1,2}/i],[PR.PR_PLAIN,/^-?(?:[_a-z]|(?:\\[\da-f]+ ?))(?:[_a-z\d\-]|\\(?:\\[\da-f]+ ?))*/i],[PR.PR_PUNCTUATION,/^[^\s\w\'\"]+/]]),["css"]);PR.registerLangHandler(PR.createSimpleLexer([],[[PR.PR_KEYWORD,/^-?(?:[_a-z]|(?:\\[\da-f]+ ?))(?:[_a-z\d\-]|\\(?:\\[\da-f]+ ?))*/i]]),["css-kw"]);PR.registerLangHandler(PR.createSimpleLexer([],[[PR.PR_STRING,/^[^\)\"\']+/]]),["css-str"]); 3 | -------------------------------------------------------------------------------- /coverage/lcov-report/react-context.js.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Code coverage report for react-context.js 7 | 8 | 9 | 10 | 11 | 12 | 17 | 18 | 19 | 20 |
21 |
22 |

All files react-context.js

23 |
24 | 25 |
26 | 100% 27 | Statements 28 | 7/7 29 |
30 | 31 | 32 |
33 | 100% 34 | Branches 35 | 0/0 36 |
37 | 38 | 39 |
40 | 100% 41 | Functions 42 | 3/3 43 |
44 | 45 | 46 |
47 | 100% 48 | Lines 49 | 7/7 50 |
51 | 52 | 53 |
54 |

55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |

57 |
58 |
59 |

 60 | 
1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8 68 | 9 69 | 10 70 | 11 71 | 12 72 | 13 73 | 14 74 | 15 75 | 16 76 | 17 77 | 18 78 | 19  79 |   80 | 1x 81 |   82 | 1x 83 | 2x 84 | 2x 85 |   86 |   87 |   88 |   89 | 1x 90 | 3x 91 | 3x 92 |   93 |   94 |   95 |   96 |  
import * as React from 'react'
 97 |  
 98 | const NameContext = React.createContext('Unknown')
 99 |  
100 | const NameProvider = ({children, first, last}) => {
101 |   const fullName = `${first} ${last}`
102 |   return (
103 |     <NameContext.Provider value={fullName}>{children}</NameContext.Provider>
104 |   )
105 | }
106 |  
107 | const NameConsumer = () => (
108 |   <NameContext.Consumer>
109 |     {value => <div>My Name Is: {value}</div>}
110 |   </NameContext.Consumer>
111 | )
112 |  
113 | export {NameContext, NameConsumer, NameProvider}
114 |  
115 | 116 |
117 |
118 | 123 | 124 | 125 | 130 | 131 | 132 | 133 | 134 | -------------------------------------------------------------------------------- /coverage/lcov-report/sort-arrow-sprite.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kentcdodds/react-testing-library-examples/def00171bc999e00bc4f75ffa15e6db9754ad854/coverage/lcov-report/sort-arrow-sprite.png -------------------------------------------------------------------------------- /coverage/lcov-report/sorter.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable */ 2 | var addSorting = (function() { 3 | 'use strict'; 4 | var cols, 5 | currentSort = { 6 | index: 0, 7 | desc: false 8 | }; 9 | 10 | // returns the summary table element 11 | function getTable() { 12 | return document.querySelector('.coverage-summary'); 13 | } 14 | // returns the thead element of the summary table 15 | function getTableHeader() { 16 | return getTable().querySelector('thead tr'); 17 | } 18 | // returns the tbody element of the summary table 19 | function getTableBody() { 20 | return getTable().querySelector('tbody'); 21 | } 22 | // returns the th element for nth column 23 | function getNthColumn(n) { 24 | return getTableHeader().querySelectorAll('th')[n]; 25 | } 26 | 27 | // loads all columns 28 | function loadColumns() { 29 | var colNodes = getTableHeader().querySelectorAll('th'), 30 | colNode, 31 | cols = [], 32 | col, 33 | i; 34 | 35 | for (i = 0; i < colNodes.length; i += 1) { 36 | colNode = colNodes[i]; 37 | col = { 38 | key: colNode.getAttribute('data-col'), 39 | sortable: !colNode.getAttribute('data-nosort'), 40 | type: colNode.getAttribute('data-type') || 'string' 41 | }; 42 | cols.push(col); 43 | if (col.sortable) { 44 | col.defaultDescSort = col.type === 'number'; 45 | colNode.innerHTML = 46 | colNode.innerHTML + ''; 47 | } 48 | } 49 | return cols; 50 | } 51 | // attaches a data attribute to every tr element with an object 52 | // of data values keyed by column name 53 | function loadRowData(tableRow) { 54 | var tableCols = tableRow.querySelectorAll('td'), 55 | colNode, 56 | col, 57 | data = {}, 58 | i, 59 | val; 60 | for (i = 0; i < tableCols.length; i += 1) { 61 | colNode = tableCols[i]; 62 | col = cols[i]; 63 | val = colNode.getAttribute('data-value'); 64 | if (col.type === 'number') { 65 | val = Number(val); 66 | } 67 | data[col.key] = val; 68 | } 69 | return data; 70 | } 71 | // loads all row data 72 | function loadData() { 73 | var rows = getTableBody().querySelectorAll('tr'), 74 | i; 75 | 76 | for (i = 0; i < rows.length; i += 1) { 77 | rows[i].data = loadRowData(rows[i]); 78 | } 79 | } 80 | // sorts the table using the data for the ith column 81 | function sortByIndex(index, desc) { 82 | var key = cols[index].key, 83 | sorter = function(a, b) { 84 | a = a.data[key]; 85 | b = b.data[key]; 86 | return a < b ? -1 : a > b ? 1 : 0; 87 | }, 88 | finalSorter = sorter, 89 | tableBody = document.querySelector('.coverage-summary tbody'), 90 | rowNodes = tableBody.querySelectorAll('tr'), 91 | rows = [], 92 | i; 93 | 94 | if (desc) { 95 | finalSorter = function(a, b) { 96 | return -1 * sorter(a, b); 97 | }; 98 | } 99 | 100 | for (i = 0; i < rowNodes.length; i += 1) { 101 | rows.push(rowNodes[i]); 102 | tableBody.removeChild(rowNodes[i]); 103 | } 104 | 105 | rows.sort(finalSorter); 106 | 107 | for (i = 0; i < rows.length; i += 1) { 108 | tableBody.appendChild(rows[i]); 109 | } 110 | } 111 | // removes sort indicators for current column being sorted 112 | function removeSortIndicators() { 113 | var col = getNthColumn(currentSort.index), 114 | cls = col.className; 115 | 116 | cls = cls.replace(/ sorted$/, '').replace(/ sorted-desc$/, ''); 117 | col.className = cls; 118 | } 119 | // adds sort indicators for current column being sorted 120 | function addSortIndicators() { 121 | getNthColumn(currentSort.index).className += currentSort.desc 122 | ? ' sorted-desc' 123 | : ' sorted'; 124 | } 125 | // adds event listeners for all sorter widgets 126 | function enableUI() { 127 | var i, 128 | el, 129 | ithSorter = function ithSorter(i) { 130 | var col = cols[i]; 131 | 132 | return function() { 133 | var desc = col.defaultDescSort; 134 | 135 | if (currentSort.index === i) { 136 | desc = !currentSort.desc; 137 | } 138 | sortByIndex(i, desc); 139 | removeSortIndicators(); 140 | currentSort.index = i; 141 | currentSort.desc = desc; 142 | addSortIndicators(); 143 | }; 144 | }; 145 | for (i = 0; i < cols.length; i += 1) { 146 | if (cols[i].sortable) { 147 | // add the click event handler on the th so users 148 | // dont have to click on those tiny arrows 149 | el = getNthColumn(i).querySelector('.sorter').parentElement; 150 | if (el.addEventListener) { 151 | el.addEventListener('click', ithSorter(i)); 152 | } else { 153 | el.attachEvent('onclick', ithSorter(i)); 154 | } 155 | } 156 | } 157 | } 158 | // adds sorting functionality to the UI 159 | return function() { 160 | if (!getTable()) { 161 | return; 162 | } 163 | cols = loadColumns(); 164 | loadData(); 165 | addSortIndicators(); 166 | enableUI(); 167 | }; 168 | })(); 169 | 170 | window.addEventListener('load', addSorting); 171 | -------------------------------------------------------------------------------- /coverage/lcov-report/utils.js.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Code coverage report for utils.js 7 | 8 | 9 | 10 | 11 | 12 | 17 | 18 | 19 | 20 |
21 |
22 |

All files utils.js

23 |
24 | 25 |
26 | 0% 27 | Statements 28 | 0/1 29 |
30 | 31 | 32 |
33 | 100% 34 | Branches 35 | 0/0 36 |
37 | 38 | 39 |
40 | 0% 41 | Functions 42 | 0/1 43 |
44 | 45 | 46 |
47 | 0% 48 | Lines 49 | 0/1 50 |
51 | 52 | 53 |
54 |

55 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 56 |

57 |
58 |
59 |

 60 | 
1 61 | 2 62 | 3 63 | 4 64 | 5 65 | 6 66 | 7 67 | 8  68 |   69 |   70 |   71 |   72 |   73 |   74 |  
function reportError() {
 75 |   // in a real app this would report an error
 76 |   // but this isn't a real app and is only here to be mocked.
 77 |   return Promise.resolve({success: true})
 78 | }
 79 |  
 80 | export {reportError}
 81 |  
82 | 83 |
84 |
85 | 90 | 91 | 92 | 97 | 98 | 99 | 100 | 101 | -------------------------------------------------------------------------------- /coverage/lcov.info: -------------------------------------------------------------------------------- 1 | TN: 2 | SF:src/component-did-catch.js 3 | FN:6,(anonymous_0) 4 | FN:10,(anonymous_1) 5 | FN:20,(anonymous_2) 6 | FN:27,(anonymous_3) 7 | FN:28,(anonymous_4) 8 | FNF:5 9 | FNH:5 10 | FNDA:1,(anonymous_0) 11 | FNDA:3,(anonymous_1) 12 | FNDA:2,(anonymous_2) 13 | FNDA:1,(anonymous_3) 14 | FNDA:2,(anonymous_4) 15 | DA:5,1 16 | DA:7,1 17 | DA:8,1 18 | DA:11,3 19 | DA:21,2 20 | DA:26,1 21 | DA:27,1 22 | DA:29,2 23 | LF:8 24 | LH:8 25 | BRDA:11,0,0,1 26 | BRDA:11,0,1,2 27 | BRDA:31,1,0,1 28 | BRDA:31,1,1,1 29 | BRF:4 30 | BRH:4 31 | end_of_record 32 | TN: 33 | SF:src/fetch-greeting.js 34 | FN:10,greetingReducer 35 | FN:30,FetchGreeting 36 | FN:37,(anonymous_2) 37 | FN:40,(anonymous_3) 38 | FN:46,(anonymous_4) 39 | FNF:5 40 | FNH:5 41 | FNDA:2,greetingReducer 42 | FNDA:5,FetchGreeting 43 | FNDA:2,(anonymous_2) 44 | FNDA:1,(anonymous_3) 45 | FNDA:1,(anonymous_4) 46 | DA:5,1 47 | DA:11,2 48 | DA:13,1 49 | DA:19,1 50 | DA:25,0 51 | DA:31,5 52 | DA:35,5 53 | DA:37,5 54 | DA:38,2 55 | DA:41,1 56 | DA:42,1 57 | DA:43,1 58 | DA:44,1 59 | DA:47,1 60 | DA:51,5 61 | DA:53,5 62 | LF:16 63 | LH:15 64 | BRDA:11,0,0,1 65 | BRDA:11,0,1,1 66 | BRDA:11,0,2,0 67 | BRDA:51,1,0,1 68 | BRDA:51,1,1,4 69 | BRDA:58,2,0,5 70 | BRDA:58,2,1,2 71 | BRDA:59,3,0,5 72 | BRDA:59,3,1,1 73 | BRF:9 74 | BRH:8 75 | end_of_record 76 | TN: 77 | SF:src/index.js 78 | FN:4,App 79 | FNF:1 80 | FNH:0 81 | FNDA:0,App 82 | DA:5,0 83 | DA:41,0 84 | LF:2 85 | LH:0 86 | BRF:0 87 | BRH:0 88 | end_of_record 89 | TN: 90 | SF:src/lazy-component.js 91 | FN:3,LazyComponent 92 | FNF:1 93 | FNH:1 94 | FNDA:2,LazyComponent 95 | DA:4,2 96 | LF:1 97 | LH:1 98 | BRF:0 99 | BRH:0 100 | end_of_record 101 | TN: 102 | SF:src/react-context.js 103 | FN:5,(anonymous_0) 104 | FN:12,(anonymous_1) 105 | FN:14,(anonymous_2) 106 | FNF:3 107 | FNH:3 108 | FNDA:2,(anonymous_0) 109 | FNDA:3,(anonymous_1) 110 | FNDA:3,(anonymous_2) 111 | DA:3,1 112 | DA:5,1 113 | DA:6,2 114 | DA:7,2 115 | DA:12,1 116 | DA:13,3 117 | DA:14,3 118 | LF:7 119 | LH:7 120 | BRF:0 121 | BRH:0 122 | end_of_record 123 | TN: 124 | SF:src/utils.js 125 | FN:1,reportError 126 | FNF:1 127 | FNH:0 128 | FNDA:0,reportError 129 | DA:4,0 130 | LF:1 131 | LH:0 132 | BRF:0 133 | BRH:0 134 | end_of_record 135 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-testing-library-examples", 3 | "version": "1.0.0", 4 | "description": "A repo full of examples of how to use react-testing-library and test your react components", 5 | "keywords": [], 6 | "main": "src/index.js", 7 | "dependencies": { 8 | "@testing-library/jest-dom": "^5.16.5", 9 | "@testing-library/react": "^11.2.5", 10 | "@testing-library/user-event": "^14.4.3", 11 | "axios": "^0.21.1", 12 | "history": "^5.0.0", 13 | "i18next": "20.0.0", 14 | "i18next-browser-languagedetector": "6.1.0", 15 | "i18next-xhr-backend": "3.2.2", 16 | "intl": "^1.2.5", 17 | "mq-polyfill": "^1.1.8", 18 | "msw": "^0.27.2", 19 | "react": "^17.0.2", 20 | "react-dom": "^17.0.2", 21 | "react-i18next": "^11.8.10", 22 | "react-intl": "^5.13.5", 23 | "react-redux": "^7.2.3", 24 | "react-router": "^6.0.0", 25 | "react-router-dom": "^6.14.1", 26 | "react-scripts": "^4.0.3", 27 | "react-transition-group": "^4.4.1", 28 | "redux": "^4.0.5" 29 | }, 30 | "devDependencies": {}, 31 | "scripts": { 32 | "start": "react-scripts start", 33 | "build": "react-scripts build", 34 | "test": "react-scripts test", 35 | "test:coverage": "npm run test -- --coverage --watchAll=false" 36 | }, 37 | "eslintConfig": { 38 | "extends": "react-app" 39 | }, 40 | "babel": { 41 | "presets": [ 42 | "react-app" 43 | ] 44 | }, 45 | "browserslist": { 46 | "development": [ 47 | "last 2 chrome versions", 48 | "last 2 firefox versions", 49 | "last 2 edge versions" 50 | ], 51 | "production": [ 52 | ">1%", 53 | "last 4 versions", 54 | "Firefox ESR", 55 | "not ie < 11" 56 | ] 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 14 | 23 | React App 24 | 25 | 26 | 27 | 30 |
31 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /sandbox.config.json: -------------------------------------------------------------------------------- 1 | { 2 | "template": "node", 3 | "container": { 4 | "startScript": "start", 5 | "port": 3000 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/__tests__/async.js: -------------------------------------------------------------------------------- 1 | import * as React from 'react' 2 | import {rest} from 'msw' 3 | import {setupServer} from 'msw/node' 4 | import {render, screen, waitFor} from '@testing-library/react' 5 | import userEvent from '@testing-library/user-event' 6 | import '@testing-library/jest-dom/extend-expect' 7 | import {FetchGreeting} from '../fetch-greeting' 8 | 9 | const server = setupServer( 10 | rest.get('/greeting', (req, res, ctx) => { 11 | return res(ctx.json({greeting: 'hello there'})) 12 | }), 13 | ) 14 | 15 | beforeAll(() => server.listen()) 16 | afterEach(() => server.resetHandlers()) 17 | afterAll(() => server.close()) 18 | 19 | test('loads and displays greeting', async () => { 20 | render() 21 | 22 | await userEvent.click(screen.getByText('Load Greeting')) 23 | 24 | await screen.findByRole('heading', {name: 'hello there'}) 25 | 26 | expect(screen.getByRole('button')).toHaveAttribute('disabled') 27 | }) 28 | 29 | test('handles server error', 30 | async () => { 31 | server.use( 32 | rest.get('/greeting', (req, res, ctx) => { 33 | return res(ctx.status(500)) 34 | }), 35 | ) 36 | 37 | render() 38 | 39 | await userEvent.click(screen.getByText('Load Greeting')) 40 | 41 | await screen.queryByRole('alert', {name: 'Oops, failed to fetch!'}) 42 | 43 | await waitFor(() => { expect(screen.getByRole('button')).toBeInTheDocument() }) 44 | await waitFor(() => { expect(screen.getByRole('button')).not.toHaveAttribute('disabled') }) 45 | }) 46 | -------------------------------------------------------------------------------- /src/__tests__/component-did-catch.js: -------------------------------------------------------------------------------- 1 | // watch me write this test in 16 minutes on YouTube: 2 | // https://www.youtube.com/watch?v=dxWrHEOD5DU&list=PLV5CVI1eNcJgCrPH_e6d57KRUTiDZgs0u 3 | import * as React from 'react' 4 | import {render, screen} from '@testing-library/react' 5 | import userEvent from '@testing-library/user-event' 6 | import {reportError} from '../utils' 7 | import {BombButton} from '../component-did-catch' 8 | 9 | jest.mock('../utils', () => { 10 | return { 11 | reportError: jest.fn(() => Promise.resolve({success: true})), 12 | } 13 | }) 14 | 15 | beforeEach(() => { 16 | // when the error's thrown a bunch of console.errors are called even though 17 | // the error boundary handles the error. This makes the test output noisy, 18 | // so we'll mock out console.error 19 | jest.spyOn(console, 'error') 20 | console.error.mockImplementation(() => {}) 21 | }) 22 | 23 | afterEach(() => { 24 | console.error.mockRestore() 25 | }) 26 | 27 | test('calls reportError and renders that there was a problem', async () => { 28 | // Arrange 29 | render() 30 | 31 | // Act 32 | await userEvent.click(screen.getByText('💣')) 33 | 34 | // Assert 35 | expect(reportError).toHaveBeenCalledTimes(1) 36 | const error = expect.any(TypeError) 37 | const info = {componentStack: expect.stringContaining('BombButton')} 38 | expect(reportError).toHaveBeenCalledWith(error, info) 39 | 40 | expect(screen.getByText(/there was a problem/i)).toBeInTheDocument() 41 | 42 | // by mocking out console.error we may inadvertantly be missing out on logs 43 | // in the future that could be important, so let's reduce that liklihood by 44 | // adding an assertion for how frequently console.error is called. 45 | expect(console.error).toHaveBeenCalledTimes(2) 46 | }) 47 | -------------------------------------------------------------------------------- /src/__tests__/i18next.js: -------------------------------------------------------------------------------- 1 | import * as React from 'react' 2 | import { 3 | initReactI18next, 4 | I18nextProvider, 5 | Trans, 6 | withTranslation, 7 | } from 'react-i18next' 8 | import i18n from 'i18next' 9 | import Backend from 'i18next-xhr-backend' 10 | import LanguageDetector from 'i18next-browser-languagedetector' 11 | import {render as rtlRender, screen} from '@testing-library/react' 12 | import userEvent from '@testing-library/user-event' 13 | 14 | const resources = { 15 | en: { 16 | translation: { 17 | 'Welcome to React': 'Welcome to React and react-i18next', 18 | }, 19 | }, 20 | pt: { 21 | translation: { 22 | 'Welcome to React': 'Bem vindo ao React e ao react-i18next', 23 | }, 24 | }, 25 | } 26 | 27 | i18n 28 | .use(Backend) 29 | .use(LanguageDetector) 30 | .use(initReactI18next) 31 | .init({ 32 | fallbackLng: 'en', 33 | debug: false, 34 | resources: { 35 | en: resources.en, 36 | pt: resources.pt, 37 | }, 38 | interpolation: { 39 | escapeValue: false, // not needed for react as it escapes by default 40 | }, 41 | }) 42 | 43 | const MainView = withTranslation()((props) => { 44 | return ( 45 | 46 |
47 | 48 | 49 |
50 |

51 | Welcome to React 52 |

53 |
54 | ) 55 | }) 56 | 57 | function render(ui, options) { 58 | function Wrapper({children}) { 59 | return {children} 60 | } 61 | return { 62 | user: userEvent.setup(), 63 | ...rtlRender(ui, {wrapper: Wrapper, ...options}), 64 | } 65 | } 66 | 67 | test('it should test lang', async () => { 68 | const {user} = render() 69 | const heading = screen.getByRole('heading') 70 | const pt = screen.getByText('pt') 71 | const en = screen.getByText('en') 72 | 73 | expect(heading).toHaveTextContent('Welcome to React and react-i18next') 74 | await user.click(pt) 75 | expect(heading).toHaveTextContent('Bem vindo ao React e ao react-i18next') 76 | await user.click(en) 77 | expect(heading).toHaveTextContent('Welcome to React and react-i18next') 78 | }) 79 | -------------------------------------------------------------------------------- /src/__tests__/mock.react-router.js: -------------------------------------------------------------------------------- 1 | import * as React from 'react' 2 | import {withRouter} from 'react-router' 3 | import {render, screen} from '@testing-library/react' 4 | 5 | // pretend this is in another file, and we: 6 | // import {LocationDisplay} from './location-display' 7 | const LocationDisplay = withRouter(({location}) => ( 8 |
{location.pathname}
9 | )) 10 | 11 | // your tests should look like this below: 12 | 13 | jest.mock('react-router', () => ({ 14 | withRouter: Comp => props => , 15 | })) 16 | 17 | test('displays location', () => { 18 | const pathname = '/some-route' 19 | render() 20 | expect(screen.getByTestId('location-display')).toHaveTextContent(pathname) 21 | }) 22 | -------------------------------------------------------------------------------- /src/__tests__/mock.react-transition-group.js: -------------------------------------------------------------------------------- 1 | import * as React from 'react' 2 | import {CSSTransition, Transition} from 'react-transition-group' 3 | import {render, screen} from '@testing-library/react' 4 | import userEvent from '@testing-library/user-event' 5 | 6 | function Fade({children, ...props}) { 7 | return ( 8 | 9 | {children} 10 | 11 | ) 12 | } 13 | 14 | function HiddenMessage({initialShow = false}) { 15 | const [show, setShow] = React.useState(initialShow) 16 | const toggle = () => setShow(s => !s) 17 | return ( 18 |
19 | 20 | 21 |
Hello world
22 |
23 |
24 | ) 25 | } 26 | 27 | jest.mock('react-transition-group', () => { 28 | const FakeTransition = jest.fn(({children}) => children) 29 | const FakeCSSTransition = jest.fn() 30 | return {CSSTransition: FakeCSSTransition, Transition: FakeTransition} 31 | }) 32 | 33 | beforeEach(() => { 34 | Transition.mockImplementation(({children}) => children) 35 | CSSTransition.mockImplementation(props => 36 | props.in ? {props.children} : null, 37 | ) 38 | }) 39 | 40 | test('you can mock things with jest.mock', async () => { 41 | // render(hi) 42 | render() 43 | expect(screen.getByText('Hello world')).toBeTruthy() // we just care it exists 44 | // hide the message 45 | await userEvent.click(screen.getByText('Toggle')) 46 | // in the real world, the CSSTransition component would take some time 47 | // before finishing the animation which would actually hide the message. 48 | // So we've mocked it out for our tests to make it happen instantly 49 | expect(screen.queryByText('Hello World')).toBeNull() // we just care it doesn't exist 50 | }) 51 | -------------------------------------------------------------------------------- /src/__tests__/on-change.js: -------------------------------------------------------------------------------- 1 | import * as React from 'react' 2 | import {render, screen} from '@testing-library/react' 3 | import userEvent from '@testing-library/user-event' 4 | 5 | function UpperInput() { 6 | const [upper, setUpper] = React.useState('') 7 | const handleChange = e => setUpper(e.currentTarget.value.toUpperCase()) 8 | return ( 9 |
10 | 11 | 12 |
13 | ) 14 | } 15 | 16 | test('sets the value to the upper version of the value', async () => { 17 | render() 18 | const upperInput = screen.getByLabelText(/upper/i) 19 | const text = 'stuff' 20 | await userEvent.type(upperInput, text) 21 | expect(upperInput.value).toEqual(text.toUpperCase()) 22 | }) 23 | 24 | test('checkboxes (and radios) must use click', async () => { 25 | const handleChange = jest.fn() 26 | const {container} = render() 27 | const checkbox = container.firstChild 28 | // for checkboxes, the event that's fired is the click event, 29 | // and that causes the change event handler to be called. 30 | // learn more: https://github.com/testing-library/react-testing-library/issues/156 31 | await userEvent.click(checkbox) 32 | expect(handleChange).toHaveBeenCalledTimes(1) 33 | expect(checkbox.checked).toBe(true) 34 | }) 35 | -------------------------------------------------------------------------------- /src/__tests__/portals.js: -------------------------------------------------------------------------------- 1 | import { useRef, useEffect } from 'react' 2 | import ReactDOM from 'react-dom' 3 | import { render, screen } from '@testing-library/react' 4 | import userEvent from '@testing-library/user-event' 5 | 6 | // this is only here for HMR/codesandbox purposes 7 | // in a real scenario, you'd probably just do the stuff 8 | // that's in the if statement. 9 | let modalRoot = document.getElementById('modal-root') 10 | if (!modalRoot) { 11 | modalRoot = document.createElement('div') 12 | modalRoot.setAttribute('id', 'modal-root') 13 | document.body.appendChild(modalRoot) 14 | } 15 | 16 | const Modal = ({ onClose, children }) => { 17 | const el = useRef(document.createElement('div')); 18 | 19 | useEffect(() => { 20 | const elToAppend = el.current; 21 | modalRoot.appendChild(elToAppend) 22 | return () => { 23 | modalRoot.removeChild(elToAppend) 24 | } 25 | }, []); 26 | 27 | return ReactDOM.createPortal( 28 |
42 |
e.stopPropagation()} 56 | > 57 | {children} 58 |
59 | 60 |
61 |
, 62 | el.current, 63 | ) 64 | } 65 | 66 | test('modal shows the children and a close button', async () => { 67 | // Arrange 68 | const handleClose = jest.fn() 69 | 70 | // Act 71 | render( 72 | 73 |
test
74 |
, 75 | ) 76 | // Assert 77 | expect(screen.getByText('test')).toBeTruthy() 78 | 79 | // Act 80 | await userEvent.click(screen.getByText(/close/i)) 81 | 82 | // Assert 83 | expect(handleClose).toHaveBeenCalledTimes(1) 84 | }) 85 | -------------------------------------------------------------------------------- /src/__tests__/react-context.js: -------------------------------------------------------------------------------- 1 | import * as React from 'react' 2 | import {render, screen} from '@testing-library/react' 3 | import {NameContext, NameProvider, NameConsumer} from '../react-context' 4 | 5 | /** 6 | * Test default values by rendering a context consumer without a 7 | * matching provider 8 | */ 9 | test('NameConsumer shows default value', () => { 10 | render() 11 | expect(screen.getByText(/^My Name Is:/)).toHaveTextContent( 12 | 'My Name Is: Unknown', 13 | ) 14 | }) 15 | 16 | /** 17 | * To test a component tree that uses a context consumer but not the provider, 18 | * wrap the tree with a matching provider 19 | */ 20 | test('NameConsumer shows value from provider', () => { 21 | render( 22 | 23 | 24 | , 25 | ) 26 | expect(screen.getByText(/^My Name Is:/)).toHaveTextContent('My Name Is: C3P0') 27 | }) 28 | 29 | /** 30 | * To test a component that provides a context value, render a matching 31 | * consumer as the child 32 | */ 33 | test('NameProvider composes full name from first, last', () => { 34 | render( 35 | 36 | 37 | {value => Received: {value}} 38 | 39 | , 40 | ) 41 | expect(screen.getByText(/^Received:/).textContent).toBe('Received: Boba Fett') 42 | }) 43 | 44 | /** 45 | * A tree containing both a providers and consumer can be rendered normally 46 | */ 47 | test('NameProvider/Consumer shows name of character', () => { 48 | render( 49 | 50 | 51 | , 52 | ) 53 | expect(screen.getByText(/^My Name Is:/).textContent).toBe( 54 | 'My Name Is: Leia Organa', 55 | ) 56 | }) 57 | -------------------------------------------------------------------------------- /src/__tests__/react-intl.js: -------------------------------------------------------------------------------- 1 | import 'intl' 2 | import * as React from 'react' 3 | import {render as rtlRender, screen} from '@testing-library/react' 4 | import {IntlProvider, FormattedDate} from 'react-intl' 5 | 6 | const FormatDateView = () => { 7 | return ( 8 |
9 | 16 |
17 | ) 18 | } 19 | 20 | function render(ui, options) { 21 | function Wrapper({children}) { 22 | return {children} 23 | } 24 | return { 25 | ...rtlRender(ui, {wrapper: Wrapper, ...options}), 26 | } 27 | } 28 | 29 | // this should work, but for some reason it's not able to load the locale 30 | // even though we have the IntlPolyfill installed 31 | // I promise it is. Just try console.log(global.IntlPolyfill) 32 | test('it should render FormattedDate and have a formated pt date', () => { 33 | render() 34 | expect(screen.getByTestId('date-display')).toHaveTextContent('11/03/2019') 35 | }) 36 | -------------------------------------------------------------------------------- /src/__tests__/react-lazy-and-suspense.js: -------------------------------------------------------------------------------- 1 | import * as React from 'react' 2 | import {render, screen} from '@testing-library/react' 3 | 4 | const LazyComponent = React.lazy(() => import('../lazy-component')) 5 | 6 | function Main() { 7 | return ( 8 |
9 |
Lazy loaded component is here:
10 | 11 |
12 | ) 13 | } 14 | 15 | function App() { 16 | return ( 17 | 18 |
19 | 20 | ) 21 | } 22 | 23 | test('renders lazy', async () => { 24 | // this is how you test a component that needs a suspense component 25 | // just render it with your own suspense! 26 | render( 27 | 28 |
29 | , 30 | ) 31 | const lazyElement = await screen.findByText(/i am lazy/i) 32 | expect(lazyElement).toBeInTheDocument() 33 | }) 34 | 35 | test('app renders stuff!', async () => { 36 | // this is how you render a component that has it's own suspense. 37 | // no need to render our own suspense in the test. 38 | render() 39 | const lazyElement = await screen.findByText(/i am lazy/i) 40 | expect(lazyElement).toBeInTheDocument() 41 | }) 42 | -------------------------------------------------------------------------------- /src/__tests__/react-redux.js: -------------------------------------------------------------------------------- 1 | import {render as rtlRender, screen} from '@testing-library/react' 2 | import userEvent from '@testing-library/user-event' 3 | import * as React from 'react' 4 | import {Provider, connect} from 'react-redux' 5 | import {createStore} from 'redux' 6 | 7 | // counter.js 8 | const Counter = ({dispatch, count}) => { 9 | const increment = () => { 10 | dispatch({type: 'INCREMENT'}) 11 | } 12 | 13 | const decrement = () => { 14 | dispatch({type: 'DECREMENT'}) 15 | } 16 | 17 | return ( 18 |
19 |

Counter

20 |
21 | 22 | {count} 23 | 24 |
25 |
26 | ) 27 | } 28 | 29 | // normally this would be: 30 | // export default connect((state) => ({count: state.count}))(Counter) 31 | // but for this test we'll give it a variable name 32 | // because we're doing this all in one file 33 | const ConnectedCounter = connect((state) => ({count: state.count}))(Counter) 34 | 35 | // app.js 36 | const initialReducerState = { 37 | count: 0, 38 | } 39 | 40 | const reducer = (state = initialReducerState, action) => { 41 | switch (action.type) { 42 | case 'INCREMENT': 43 | return { 44 | count: state.count + 1, 45 | } 46 | case 'DECREMENT': 47 | return { 48 | count: state.count - 1, 49 | } 50 | default: 51 | return state 52 | } 53 | } 54 | 55 | // normally here you'd do: 56 | // const store = createStore(reducer) 57 | // ReactDOM.render( 58 | // 59 | // 60 | // , 61 | // document.getElementById('root'), 62 | // ) 63 | // but for this test we'll umm... not do that :) 64 | 65 | // Now here's what your test will look like: 66 | 67 | // this is a handy function that I normally make available for all my tests 68 | // that deal with connected components. 69 | // you can provide initialState or the entire store that the ui is rendered with 70 | const render = ( 71 | ui, 72 | { 73 | initialState = initialReducerState, 74 | store = createStore(reducer, initialState), 75 | ...renderOptions 76 | } = {}, 77 | ) => { 78 | const Wrapper = ({children}) => {children} 79 | 80 | return rtlRender(ui, {wrapper: Wrapper, ...renderOptions}) 81 | } 82 | 83 | test('can render with redux with defaults', async () => { 84 | render() 85 | 86 | await userEvent.click(screen.getByText('+')) 87 | 88 | expect(screen.getByTestId('count-value')).toHaveTextContent('1') 89 | }) 90 | 91 | test('can render with redux with custom initial state', async () => { 92 | render(, {initialState: {count: 3}}) 93 | 94 | await userEvent.click(screen.getByText('-')) 95 | 96 | expect(screen.getByTestId('count-value')).toHaveTextContent('2') 97 | }) 98 | 99 | test('can render with redux with custom store', async () => { 100 | // this is a silly store that can never be changed 101 | const store = createStore(() => ({count: 1000})) 102 | render(, {store}) 103 | 104 | await userEvent.click(screen.getByText('+')) 105 | expect(screen.getByTestId('count-value')).toHaveTextContent('1000') 106 | 107 | await userEvent.click(screen.getByText('-')) 108 | expect(screen.getByTestId('count-value')).toHaveTextContent('1000') 109 | }) 110 | -------------------------------------------------------------------------------- /src/__tests__/react-router.js: -------------------------------------------------------------------------------- 1 | import {render as rtlRender, screen} from '@testing-library/react' 2 | import userEvent from '@testing-library/user-event' 3 | import * as React from 'react' 4 | import { 5 | Link, 6 | Route, 7 | Routes, 8 | BrowserRouter as Router, 9 | useLocation, 10 | } from 'react-router-dom' 11 | 12 | const About = () =>
You are on the about page
13 | const Home = () =>
You are home
14 | const NoMatch = () =>
No match
15 | 16 | const LocationDisplay = () => { 17 | const location = useLocation() 18 | 19 | return
{location.pathname}
20 | } 21 | 22 | const App = () => ( 23 |
24 | Home 25 | 26 | About 27 | 28 | 29 | } /> 30 | 31 | } /> 32 | 33 | } /> 34 | 35 | 36 | 37 |
38 | ) 39 | 40 | // Ok, so here's what your tests might look like 41 | 42 | // this is a handy function that I would utilize for any component 43 | // that relies on the router being in context 44 | const render = (ui, {route = '/'} = {}) => { 45 | window.history.pushState({}, 'Test page', route) 46 | 47 | return { 48 | user: userEvent.setup(), 49 | ...rtlRender(ui, {wrapper: Router}), 50 | } 51 | } 52 | 53 | test('full app rendering/navigating', async () => { 54 | const {user} = render() 55 | expect(screen.getByText(/you are home/i)).toBeInTheDocument() 56 | 57 | await user.click(screen.getByText(/about/i)) 58 | 59 | expect(screen.getByText(/you are on the about page/i)).toBeInTheDocument() 60 | }) 61 | 62 | test('landing on a bad page', () => { 63 | render(, {route: '/something-that-does-not-match'}) 64 | 65 | expect(screen.getByText(/no match/i)).toBeInTheDocument() 66 | }) 67 | 68 | test('rendering a component that uses useLocation', () => { 69 | const route = '/some-route' 70 | render(, {route}) 71 | 72 | // avoid using test IDs when you can 73 | expect(screen.getByTestId('location-display')).toHaveTextContent(route) 74 | }) 75 | -------------------------------------------------------------------------------- /src/__tests__/shallow.react-transition-group.js: -------------------------------------------------------------------------------- 1 | import * as React from 'react' 2 | import {CSSTransition} from 'react-transition-group' 3 | import {render, screen} from '@testing-library/react' 4 | import userEvent from '@testing-library/user-event' 5 | 6 | // NOTE: True shallow rendering is not possible with React Testing Library 7 | // This is by design: https://kcd.im/shallow 8 | // But mocking can be useful in some situations, so this is an example of that. 9 | 10 | function Fade({children, ...props}) { 11 | return ( 12 | 13 | {children} 14 | 15 | ) 16 | } 17 | 18 | function HiddenMessage({initialShow = false}) { 19 | const [show, setShow] = React.useState(initialShow) 20 | const toggle = () => setShow(s => !s) 21 | return ( 22 |
23 | 24 | 25 |
Hello world
26 |
27 |
28 | ) 29 | } 30 | 31 | jest.mock('react-transition-group', () => { 32 | const FakeCSSTransition = jest.fn() 33 | return {CSSTransition: FakeCSSTransition} 34 | }) 35 | 36 | beforeEach(() => { 37 | CSSTransition.mockImplementation(() => null) 38 | }) 39 | 40 | test('you can mock things with jest.mock', async () => { 41 | render() 42 | const context = expect.any(Object) 43 | const children = expect.any(Object) 44 | const defaultProps = {children, timeout: 1000, className: 'fade'} 45 | expect(CSSTransition).toHaveBeenCalledWith( 46 | {in: true, ...defaultProps}, 47 | context, 48 | ) 49 | await userEvent.click(screen.getByText(/toggle/i)) 50 | expect(CSSTransition).toHaveBeenCalledWith( 51 | {in: true, ...defaultProps}, 52 | context, 53 | ) 54 | }) 55 | -------------------------------------------------------------------------------- /src/__tests__/update-props.js: -------------------------------------------------------------------------------- 1 | // This is an example of how to update the props of a rendered component. 2 | // the basic idea is to simply call `render` again and provide the same container 3 | // that your first call created for you. 4 | 5 | import * as React from 'react' 6 | import {useRef} from 'react' 7 | import {render, screen} from '@testing-library/react' 8 | 9 | let idCounter = 1 10 | 11 | const NumberDisplay = ({number}) => { 12 | const id = useRef(idCounter++) // to ensure we don't remount a different instance 13 | 14 | return ( 15 |
16 | {number} 17 | {id.current} 18 |
19 | ) 20 | } 21 | 22 | test('calling render with the same component on the same container does not remount', () => { 23 | const {rerender} = render() 24 | expect(screen.getByTestId('number-display')).toHaveTextContent('1') 25 | 26 | // re-render the same component with different props 27 | rerender() 28 | expect(screen.getByTestId('number-display')).toHaveTextContent('2') 29 | 30 | expect(screen.getByTestId('instance-id')).toHaveTextContent('1') 31 | }) 32 | -------------------------------------------------------------------------------- /src/__tests__/upload-file.js: -------------------------------------------------------------------------------- 1 | import {render, screen} from '@testing-library/react' 2 | import userEvent from '@testing-library/user-event' 3 | import * as React from 'react' 4 | import {useState} from 'react' 5 | 6 | const UploadFile = () => { 7 | const [uploadedFileName, setUploadedFileName] = useState(null) 8 | 9 | const handleUploadFile = ({target: {files}}) => 10 | setUploadedFileName(files[0].name) 11 | 12 | return ( 13 |
14 | 15 | 16 | 22 | 23 | {uploadedFileName && ( 24 |
You have uploaded a file named {uploadedFileName}
25 | )} 26 |
27 | ) 28 | } 29 | 30 | test('Show the uploaded file name after the user uploads a file', async () => { 31 | render() 32 | const inputEl = screen.getByLabelText(/upload file/i) 33 | 34 | const file = new File(['(⌐□_□)'], 'chucknorris.png', { 35 | type: 'image/png', 36 | }) 37 | await userEvent.upload(inputEl, file) 38 | 39 | expect(screen.getByText(/chucknorris\.png/)).toBeInTheDocument() 40 | }) 41 | -------------------------------------------------------------------------------- /src/__tests__/window-resize.js: -------------------------------------------------------------------------------- 1 | import * as React from 'react' 2 | import matchMediaPolyfill from 'mq-polyfill' 3 | import {render, screen} from '@testing-library/react' 4 | 5 | function WindowSize() { 6 | return ( 7 |
8 | 9 |
{window.innerWidth}
10 | 11 |
{window.innerHeight}
12 | 13 |
{window.outerWidth}
14 | 15 |
{window.outerHeight}
16 |
17 | ) 18 | } 19 | 20 | beforeAll(() => { 21 | matchMediaPolyfill(window) 22 | window.resizeTo = function resizeTo(width, height) { 23 | Object.assign(this, { 24 | innerWidth: width, 25 | innerHeight: height, 26 | outerWidth: width, 27 | outerHeight: height, 28 | }).dispatchEvent(new this.Event('resize')) 29 | } 30 | }) 31 | 32 | test('shows default window size correctly', () => { 33 | render() 34 | 35 | expect(screen.getByLabelText('Inner Width')).toHaveTextContent(1024) 36 | expect(screen.getByLabelText('Inner Height')).toHaveTextContent(768) 37 | expect(screen.getByLabelText('Outer Width')).toHaveTextContent(1024) 38 | expect(screen.getByLabelText('Outer Height')).toHaveTextContent(768) 39 | }) 40 | 41 | test('shows modified window size correctly', () => { 42 | window.resizeTo(800, 300) 43 | 44 | render() 45 | 46 | expect(screen.getByLabelText('Inner Width')).toHaveTextContent(800) 47 | expect(screen.getByLabelText('Inner Height')).toHaveTextContent(300) 48 | expect(screen.getByLabelText('Outer Width')).toHaveTextContent(800) 49 | expect(screen.getByLabelText('Outer Height')).toHaveTextContent(300) 50 | }) 51 | -------------------------------------------------------------------------------- /src/component-did-catch.js: -------------------------------------------------------------------------------- 1 | import * as React from 'react' 2 | import {reportError} from './utils' 3 | 4 | class ErrorBoundary extends React.Component { 5 | state = {hasError: false} 6 | componentDidCatch(error, info) { 7 | this.setState({hasError: true}) 8 | reportError(error, info) 9 | } 10 | render() { 11 | return this.state.hasError ? ( 12 |
There was a problem
13 | ) : ( 14 | this.props.children 15 | ) 16 | } 17 | } 18 | 19 | function Bomb() { 20 | return window['💣'].caboom() 21 | } 22 | 23 | function BombButton() { 24 | const [renderBomb, setRenderBomb] = React.useState(false) 25 | const handleBombClick = () => setRenderBomb(true) 26 | return ( 27 | 28 | {renderBomb ? ( 29 | 30 | ) : ( 31 | 36 | )} 37 | 38 | ) 39 | } 40 | 41 | export {BombButton} 42 | -------------------------------------------------------------------------------- /src/fetch-greeting.js: -------------------------------------------------------------------------------- 1 | import * as React from 'react' 2 | import {useState, useReducer} from 'react' 3 | import axios from 'axios' 4 | 5 | const initialState = { 6 | error: null, 7 | greeting: null, 8 | } 9 | 10 | function greetingReducer(state, action) { 11 | switch (action.type) { 12 | case 'SUCCESS': { 13 | return { 14 | error: null, 15 | greeting: action.greeting, 16 | } 17 | } 18 | case 'ERROR': { 19 | return { 20 | error: action.error, 21 | greeting: null, 22 | } 23 | } 24 | default: { 25 | return state 26 | } 27 | } 28 | } 29 | 30 | function FetchGreeting() { 31 | const [{error, greeting}, dispatch] = useReducer( 32 | greetingReducer, 33 | initialState, 34 | ) 35 | const [buttonClicked, setButtonClicked] = useState(false) 36 | 37 | const fetchGreeting = async () => { 38 | axios 39 | .get('/greeting') 40 | .then((response) => { 41 | const {data} = response 42 | const {greeting} = data 43 | dispatch({type: 'SUCCESS', greeting}) 44 | setButtonClicked(true) 45 | }) 46 | .catch((error) => { 47 | dispatch({type: 'ERROR', error}) 48 | }) 49 | } 50 | 51 | const buttonText = buttonClicked ? 'Ok' : 'Load Greeting' 52 | 53 | return ( 54 |
55 | 58 | {greeting &&

{greeting}

} 59 | {error &&

Oops, failed to fetch!

} 60 |
61 | ) 62 | } 63 | 64 | export {FetchGreeting} 65 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import * as React from 'react' 2 | import {render} from 'react-dom' 3 | 4 | function App() { 5 | return ( 6 |
7 |

8 | react-testing-library examples{' '} 9 | 10 | 🐐 11 | 12 |

13 |
14 |

{` 15 | This is an example project of how to test react components using 16 | react-testing-library and Jest. The idea is that most of the tests 17 | should be runnable in codesandbox to make it easy to try things out, 18 | fork, contribute, etc. Either clone the repo locally or run the tests 19 | in a terminal in codesandbox (run "npm test") and it should watch your 20 | changes in the codesandbox and re-run the tests as you make changes. 21 | `}

22 |

23 | 24 | Open this in codesandbox 25 | {' '} 26 | to play around with things. You can also use the codesandbox to 27 | contribute to{' '} 28 | 29 | the project on GitHub 30 | 31 | ! 32 |

33 |
34 |
35 | ) 36 | } 37 | 38 | render(, document.getElementById('root')) 39 | -------------------------------------------------------------------------------- /src/lazy-component.js: -------------------------------------------------------------------------------- 1 | import * as React from 'react' 2 | 3 | function LazyComponent() { 4 | return
I am lazy!
5 | } 6 | 7 | export default LazyComponent 8 | -------------------------------------------------------------------------------- /src/react-context.js: -------------------------------------------------------------------------------- 1 | import * as React from 'react' 2 | 3 | const NameContext = React.createContext('Unknown') 4 | 5 | const NameProvider = ({children, first, last}) => { 6 | const fullName = `${first} ${last}` 7 | return ( 8 | {children} 9 | ) 10 | } 11 | 12 | const NameConsumer = () => ( 13 | 14 | {value =>
My Name Is: {value}
} 15 |
16 | ) 17 | 18 | export {NameContext, NameConsumer, NameProvider} 19 | -------------------------------------------------------------------------------- /src/setupTests.js: -------------------------------------------------------------------------------- 1 | import '@testing-library/jest-dom/extend-expect' 2 | -------------------------------------------------------------------------------- /src/utils.js: -------------------------------------------------------------------------------- 1 | function reportError() { 2 | // in a real app this would report an error 3 | // but this isn't a real app and is only here to be mocked. 4 | return Promise.resolve({success: true}) 5 | } 6 | 7 | export {reportError} 8 | --------------------------------------------------------------------------------