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 +47
-3
lines changed Expand file tree Collapse file tree 4 files changed +47
-3
lines changed Original file line number Diff line number Diff line change @@ -14,10 +14,13 @@ describe('HelloState component', () => {
1414 // mount the component under test
1515 mount (< HelloState / > )
1616 // 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'
2022 })
23+ // check if GUI has rerendered
2124 cy .contains (' Hello React!' )
2225 })
2326})
@@ -32,4 +35,5 @@ All components are in [src](src) folder. All tests are in [cypress/integration](
3235* [ hello-world-spec.js] ( cypress/integration/hello-world-spec.js ) - testing the simplest React component from [ hello-world.jsx] ( src/hello-world.jsx )
3336* [ hello-x-spec.js] ( cypress/integration/hello-x-spec.js ) - testing React component with props and state [ hello-x.jsx] ( src/hello-x.jsx )
3437* [ 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 )
3539* 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', () => {
1515 mount ( < HelloState /> )
1616 cy . contains ( 'Hello Spider-man!' )
1717 Cypress . component ( ) . invoke ( 'setState' , { name : 'React' } )
18+ Cypress . component ( ) . its ( 'state' ) . should ( 'deep.equal' , {
19+ name : 'React'
20+ } )
1821 cy . contains ( 'Hello React!' )
1922 } )
2023} )
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