This repository was archived by the owner on Mar 5, 2022. It is now read-only.
File tree 4 files changed +53
-9
lines changed
4 files changed +53
-9
lines changed Original file line number Diff line number Diff line change 1
1
{
2
2
"viewportWidth" : 300 ,
3
- "viewportHeight" : 100
3
+ "viewportHeight" : 100 ,
4
+ "port" : 3456
4
5
}
Original file line number Diff line number Diff line change @@ -12,4 +12,36 @@ describe('Counter', () => {
12
12
. click ( )
13
13
. contains ( 'count: 2' )
14
14
} )
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
+ } )
15
47
} )
Original file line number Diff line number Diff line change @@ -11,11 +11,10 @@ describe('HelloX component', () => {
11
11
} )
12
12
13
13
describe ( 'HelloState component' , ( ) => {
14
- it ( 'works ' , ( ) => {
14
+ it ( 'changes state ' , ( ) => {
15
15
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' } )
19
18
cy . contains ( 'Hello React!' )
20
19
} )
21
20
} )
Original file line number Diff line number Diff line change 1
- import { render } from 'react-dom'
2
-
3
1
// having weak reference to styles prevents garbage collection
4
2
// and "losing" styles when the next test starts
5
3
const stylesCache = new Map ( )
@@ -39,13 +37,27 @@ const copyStyles = component => {
39
37
40
38
/* eslint-env mocha */
41
39
export 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>`
43
49
44
50
const document = cy . state ( 'document' )
45
51
document . write ( html )
46
52
document . close ( )
47
53
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
+ } )
49
61
50
62
copyStyles ( jsx )
51
63
}
You can’t perform that action at this time.
0 commit comments