This repository was archived by the owner on Mar 5, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +53
-9
lines changed Expand file tree Collapse file tree 4 files changed +53
-9
lines changed Original file line number Diff line number Diff line change 11{
22 "viewportWidth" : 300 ,
3- "viewportHeight" : 100
3+ "viewportHeight" : 100 ,
4+ "port" : 3456
45}
Original file line number Diff line number Diff line change @@ -12,4 +12,36 @@ describe('Counter', () => {
1212 . click ( )
1313 . contains ( 'count: 2' )
1414 } )
15+
16+ it ( 'counts clicks 2' , ( ) => {
17+ mount ( < Counter /> )
18+ cy . contains ( 'count: 0' )
19+ . click ( )
20+ . contains ( 'count: 1' )
21+ . click ( )
22+ . contains ( 'count: 2' )
23+ } )
24+ } )
25+
26+ describe ( 'Counter mounted before each test' , ( ) => {
27+ beforeEach ( ( ) => {
28+ mount ( < Counter /> )
29+ } )
30+
31+ it ( 'goes to 3' , ( ) => {
32+ cy . contains ( 'count: 0' )
33+ . click ( )
34+ . click ( )
35+ . click ( )
36+ . contains ( 'count: 3' )
37+ } )
38+
39+ it ( 'has count in state' , ( ) => {
40+ cy . contains ( 'count: 0' )
41+ . click ( )
42+ . click ( )
43+ . click ( )
44+ Cypress . component ( ) . its ( 'state' )
45+ . should ( 'deep.equal' , { count : 3 } )
46+ } )
1547} )
Original file line number Diff line number Diff line change @@ -11,11 +11,10 @@ describe('HelloX component', () => {
1111} )
1212
1313describe ( 'HelloState component' , ( ) => {
14- it ( 'works ' , ( ) => {
14+ it ( 'changes state ' , ( ) => {
1515 mount ( < HelloState /> )
16- cy . contains ( 'Hello Spider-man!' ) . then ( ( ) => {
17- Cypress . component . setState ( { name : 'React' } )
18- } )
16+ cy . contains ( 'Hello Spider-man!' )
17+ Cypress . component ( ) . invoke ( 'setState' , { name : 'React' } )
1918 cy . contains ( 'Hello React!' )
2019 } )
2120} )
Original file line number Diff line number Diff line change 1- import { render } from 'react-dom'
2-
31// having weak reference to styles prevents garbage collection
42// and "losing" styles when the next test starts
53const stylesCache = new Map ( )
@@ -39,13 +37,27 @@ const copyStyles = component => {
3937
4038/* eslint-env mocha */
4139export const mount = jsx => {
42- const html = '<body><div id="app"></div></body>'
40+ // include React and ReactDOM from CDN to force DOM to register all DOM event listeners
41+ // otherwise the component will NOT be able to dispatch any events
42+ // when it runs the second time
43+ // https://github.com/bahmutov/cypress-react-unit-test/issues/3
44+ const html = `<body>
45+ <div id="app"></div>
46+ <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.2.0/umd/react.development.js"></script>
47+ <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.2.0/umd/react-dom.development.js"></script>
48+ </body>`
4349
4450 const document = cy . state ( 'document' )
4551 document . write ( html )
4652 document . close ( )
4753
48- Cypress . component = render ( jsx , document . getElementById ( 'app' ) )
54+ cy . window ( { log : false } )
55+ . its ( 'ReactDOM.render' )
56+ . then ( render => {
57+ Cypress . _component = render ( jsx , document . getElementById ( 'app' ) )
58+ Cypress . component = ( ) =>
59+ cy . then ( ( ) => Cypress . _component )
60+ } )
4961
5062 copyStyles ( jsx )
5163}
You can’t perform that action at this time.
0 commit comments