This repository was archived by the owner on Mar 5, 2022. It is now read-only.
File tree 4 files changed +47
-3
lines changed
4 files changed +47
-3
lines changed Original file line number Diff line number Diff line change @@ -14,10 +14,13 @@ describe('HelloState component', () => {
14
14
// mount the component under test
15
15
mount (< HelloState / > )
16
16
// start testing!
17
- cy .contains (' Hello Spider-man!' ).then (() => {
18
- // mounted component is at Cypress.component
19
- Cypress .component .setState ({ name: ' React' })
17
+ cy .contains (' Hello Spider-man!' )
18
+ // mounted component is returned from Cypress.component()
19
+ Cypress .component ().invoke (' setState' , {name: ' React' })
20
+ Cypress .component ().its (' state' ).should (' deep.equal' , {
21
+ name: ' React'
20
22
})
23
+ // check if GUI has rerendered
21
24
cy .contains (' Hello React!' )
22
25
})
23
26
})
@@ -32,4 +35,5 @@ All components are in [src](src) folder. All tests are in [cypress/integration](
32
35
* [ hello-world-spec.js] ( cypress/integration/hello-world-spec.js ) - testing the simplest React component from [ hello-world.jsx] ( src/hello-world.jsx )
33
36
* [ hello-x-spec.js] ( cypress/integration/hello-x-spec.js ) - testing React component with props and state [ hello-x.jsx] ( src/hello-x.jsx )
34
37
* [ counter-spec.js] ( cypress/integration/counter-spec.js ) clicks on the component and confirms the result
38
+ * [ stateless-spec.js] ( cypress/integration/stateless-spec.js ) shows testing a stateless component from [ stateless.jsx] ( src/stateless.jsx )
35
39
* separate repo [ bahmutov/calculator] ( https://github.com/bahmutov/calculator ) tests multiple components
Original file line number Diff line number Diff line change @@ -15,6 +15,9 @@ describe('HelloState component', () => {
15
15
mount ( < HelloState /> )
16
16
cy . contains ( 'Hello Spider-man!' )
17
17
Cypress . component ( ) . invoke ( 'setState' , { name : 'React' } )
18
+ Cypress . component ( ) . its ( 'state' ) . should ( 'deep.equal' , {
19
+ name : 'React'
20
+ } )
18
21
cy . contains ( 'Hello React!' )
19
22
} )
20
23
} )
Original file line number Diff line number Diff line change
1
+ import HelloWorld from '../../src/stateless.jsx'
2
+ import React from 'react'
3
+ import { mount } from '../../lib'
4
+
5
+ /* eslint-env mocha */
6
+ describe ( 'Stateless component' , ( ) => {
7
+ beforeEach ( ( ) => {
8
+ // pass spy and save it under an alias
9
+ // so we can easily get it later with cy.get('@greeting')
10
+ const spy = cy . spy ( ) . as ( 'greeting' )
11
+ mount ( < HelloWorld name = "Test Aficionado" click = { spy } /> )
12
+ } )
13
+
14
+ it ( 'shows link' , ( ) => {
15
+ cy . contains ( 'a' , 'Say Hi' )
16
+ } )
17
+
18
+ it ( 'alerts with name' , ( ) => {
19
+ cy . contains ( 'Say Hi' ) . click ( )
20
+ cy . get ( '@greeting' ) . should ( 'be.calledWith' , 'Hi Test Aficionado' )
21
+ } )
22
+ } )
Original file line number Diff line number Diff line change
1
+ import React from 'react'
2
+
3
+ // example stateless component from
4
+ // https://hackernoon.com/react-stateless-functional-components-nine-wins-you-might-have-overlooked-997b0d933dbc
5
+ const HelloWorld = ( { name, click} ) => {
6
+
7
+ return (
8
+ < div >
9
+ < a href = "#"
10
+ onClick = { click ( `Hi ${ name } ` ) } > Say Hi</ a >
11
+ </ div >
12
+ )
13
+ }
14
+
15
+ export default HelloWorld
You can’t perform that action at this time.
0 commit comments