This repository was archived by the owner on Mar 5, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 8 files changed +134
-1
lines changed
react-book-by-chris-noring/thinking-in-components Expand file tree Collapse file tree 8 files changed +134
-1
lines changed Original file line number Diff line number Diff line change @@ -102,6 +102,7 @@ Spec | Description
102
102
[ unmount] ( cypress/component/basic/unmount ) | Verifies the component's behavior when it is unmounted from the DOM
103
103
[ use-lodash-fp] ( cypress/component/basic/use-lodash-fp ) | Imports and tests methods from ` lodash/fp ` dependency
104
104
[ document-spec] ( cypress/component/basic/document ) | Checks ` document ` dimensions from the component
105
+ [ styled-components] ( cypress/component/basic/styled-components ) | Test components that use [ styled-components] ( https://www.styled-components.com/ )
105
106
<!-- prettier-ignore-end -->
106
107
107
108
plus a few smaller sanity specs in [ cypress/component/basic] ( cypress/component/basic ) folder.
Original file line number Diff line number Diff line change 2
2
import { mount } from 'cypress-react-unit-test'
3
3
import React , { Component } from 'react'
4
4
import Todos from './Todos'
5
- import './App.css'
6
5
7
6
const todos = [
8
7
{
Original file line number Diff line number Diff line change
1
+ https://softchris.github.io/books/react/thinkingincomponents/
2
+
3
+ Components are using [ styled-components] ( https://www.styled-components.com/ )
Original file line number Diff line number Diff line change
1
+ /// <reference types="cypress" />
2
+ import React from 'react'
3
+ import { mount } from 'cypress-react-unit-test'
4
+ import Todo from './Todo'
5
+
6
+ it ( 'renders Todo' , ( ) => {
7
+ const todo = {
8
+ title : 'clean' ,
9
+ done : false ,
10
+ id : 1 ,
11
+ }
12
+ const handleChecked = cy . stub ( )
13
+ mount ( < Todo todo = { todo } key = { todo . id } handleChecked = { handleChecked } /> )
14
+ cy . contains ( 'clean' )
15
+ cy . get ( 'input[type=checkbox]' )
16
+ . check ( )
17
+ . then ( ( ) => {
18
+ expect ( handleChecked ) . to . have . been . calledOnce
19
+ } )
20
+ } )
Original file line number Diff line number Diff line change
1
+ import React from 'react'
2
+ import styled from 'styled-components'
3
+ import PropTypes from 'prop-types'
4
+
5
+ const TodoContainer = styled . div `
6
+ box-shadow: 0 0 5px gray;
7
+ padding: 30px;
8
+ margin-bottom: 10px;
9
+ `
10
+
11
+ const Todo = ( { todo, handleChecked } ) => (
12
+ < TodoContainer key = { todo . id } data-cy = "todo" >
13
+ < input
14
+ type = "checkbox"
15
+ onChange = { ( ) => handleChecked ( todo ) }
16
+ checked = { todo . done }
17
+ />
18
+ { todo . title }
19
+ </ TodoContainer >
20
+ )
21
+
22
+ Todo . propTypes = {
23
+ todo : PropTypes . shape ( {
24
+ title : PropTypes . string ,
25
+ done : PropTypes . bool ,
26
+ id : PropTypes . number ,
27
+ } ) ,
28
+ handleChecked : PropTypes . func ,
29
+ }
30
+
31
+ export default Todo
Original file line number Diff line number Diff line change
1
+ /// <reference types="cypress" />
2
+ import { mount } from 'cypress-react-unit-test'
3
+ import React , { Component } from 'react'
4
+ import Todos from './Todos'
5
+
6
+ const todos = [
7
+ {
8
+ title : 'clean' ,
9
+ done : false ,
10
+ id : 1 ,
11
+ } ,
12
+ {
13
+ title : 'do the dishes' ,
14
+ done : true ,
15
+ id : 2 ,
16
+ } ,
17
+ ]
18
+
19
+ class App extends Component {
20
+ render ( ) {
21
+ return < Todos todos = { todos } />
22
+ }
23
+ }
24
+
25
+ it ( 'renders todos' , ( ) => {
26
+ cy . viewport ( 400 , 500 )
27
+ mount ( < App /> )
28
+ cy . contains ( '[data-cy="todo"]' , 'clean' )
29
+ . find ( 'input[type=checkbox]' )
30
+ . should ( 'not.be.checked' )
31
+ cy . contains ( '[data-cy="todo"]' , 'do the dishes' )
32
+ . find ( 'input[type=checkbox]' )
33
+ . should ( 'be.checked' )
34
+ } )
Original file line number Diff line number Diff line change
1
+ import React , { Component } from 'react'
2
+ import styled from 'styled-components'
3
+ import PropTypes from 'prop-types'
4
+
5
+ import Todo from './Todo'
6
+
7
+ const TodosContainer = styled . div `
8
+ padding: 30px;
9
+ `
10
+
11
+ class Todos extends Component {
12
+ static propTypes = {
13
+ todos : PropTypes . array . isRequired ,
14
+ }
15
+
16
+ state = {
17
+ todos : this . props . todos ,
18
+ }
19
+
20
+ handleChecked = todo => {
21
+ const newTodos = this . state . todos . map ( t => {
22
+ if ( t . id === todo . id ) {
23
+ return { ...t , done : ! t . done }
24
+ }
25
+ return t
26
+ } )
27
+
28
+ this . setState ( {
29
+ todos : newTodos ,
30
+ } )
31
+ }
32
+
33
+ render ( ) {
34
+ return (
35
+ < TodosContainer >
36
+ < h2 > Todos</ h2 >
37
+ { this . state . todos . map ( todo => (
38
+ < Todo todo = { todo } key = { todo . id } handleChecked = { this . handleChecked } />
39
+ ) ) }
40
+ </ TodosContainer >
41
+ )
42
+ }
43
+ }
44
+
45
+ export default Todos
You can’t perform that action at this time.
0 commit comments