Skip to content
This repository was archived by the owner on Mar 5, 2022. It is now read-only.

Commit 92289a8

Browse files
committed
explicit test with styled-components, closes #40
1 parent f8734b0 commit 92289a8

File tree

8 files changed

+134
-1
lines changed

8 files changed

+134
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ Spec | Description
102102
[unmount](cypress/component/basic/unmount) | Verifies the component's behavior when it is unmounted from the DOM
103103
[use-lodash-fp](cypress/component/basic/use-lodash-fp) | Imports and tests methods from `lodash/fp` dependency
104104
[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/)
105106
<!-- prettier-ignore-end -->
106107

107108
plus a few smaller sanity specs in [cypress/component/basic](cypress/component/basic) folder.

cypress/component/basic/react-book-by-chris-noring/thinking-in-components/App.css

Whitespace-only changes.

cypress/component/basic/react-book-by-chris-noring/thinking-in-components/Todos-spec.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import { mount } from 'cypress-react-unit-test'
33
import React, { Component } from 'react'
44
import Todos from './Todos'
5-
import './App.css'
65

76
const todos = [
87
{
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
https://softchris.github.io/books/react/thinkingincomponents/
2+
3+
Components are using [styled-components](https://www.styled-components.com/)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
})
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
})
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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

0 commit comments

Comments
 (0)