Skip to content

Commit 04c165e

Browse files
committed
chore: add toggle action and test
1 parent 15bfbcb commit 04c165e

File tree

5 files changed

+30
-7
lines changed

5 files changed

+30
-7
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Start Cypress using `$(npm bin)/cypress open` and execute the spec. You have ful
7070
7171
* [src/index.js](src/index.js) the main file implementing `mount`
7272
* [components](components) different Hyper components for testing
73+
* [actions](actions) pure actions functions used from components and tests
7374
* [cypress/integration](cypress/integration) example spec files showing various test situations
7475
7576
See video of tests running on CI on the project's [Cypress Dashboard][cypress dashboard url]
@@ -81,6 +82,11 @@ See video of tests running on CI on the project's [Cypress Dashboard][cypress da
8182
- [single TodoItem component](cypress/integration/todo-item-spec.js)
8283
- [entire TodoList component](cypress/integration/todo-list-spec.js)
8384
85+
## Package scripts
86+
87+
* `npm run cy:open` starts Cypress GUI, which is great for TDD mode
88+
* `npm run cy:run` runs Cypress headlessly, testing all specs. Same command [runs on CI](.travis.yml) with additional `--record` argument to record the run and send to the [Cypress Dashboard][cypress dashboard url]
89+
8490
### Small print
8591
8692
Author: Gleb Bahmutov <[email protected]> © 2017

actions/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// common pure actions for different components AND tests
2+
3+
export const toggle = ({ id, done }) => state => {
4+
// poor man's find and toggle todo, can be really shortened
5+
// with Ramda or other functional libraries.
6+
7+
// Luckily such pure functions are
8+
// SUPER SIMPLE to test and refactor
9+
// as much as you want
10+
return {
11+
todos: state.todos.map(
12+
t => (t.id === id ? Cypress._.merge({}, t, { done }) : t)
13+
)
14+
}
15+
}

components/todo-item.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import { h } from 'hyperapp'
33
// TodoItem component from
44
// https://github.com/hyperapp/hyperapp/blob/master/docs/concepts/components.md
55
export const TodoItem = ({ id, value, done, toggle }) => {
6+
done = Boolean(done)
7+
68
const onclick = e =>
79
toggle({
8-
value: done,
10+
done: !done,
911
id
1012
})
1113

cypress/integration/todo-list-spec.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { h } from 'hyperapp'
22
import { TodoList } from '../../components/todo-list'
3+
import { toggle } from '../../actions'
34
import { mount } from '../..'
45

56
/* eslint-env mocha */
@@ -27,11 +28,9 @@ describe('TodoList', () => {
2728
}
2829
]
2930
}
31+
// reuse toggle action in the test - why not?
3032
const actions = {
31-
// TODO implement toggle
32-
toggle: () => state => {
33-
throw new Error('Toggle not implemented yet')
34-
}
33+
toggle
3534
}
3635
const view = (state, actions) =>
3736
// renders TodoList component, passing
@@ -71,7 +70,7 @@ describe('TodoList', () => {
7170
.should('not.be.checked')
7271
})
7372

74-
it.skip('completes todo by clicking', () => {
73+
it('completes todo by clicking', () => {
7574
cy.contains('.todo-list .todo', 'Profit').click()
7675
cy
7776
.contains('.todo-list .todo', 'Profit')

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@
5858
"secure": "nsp check",
5959
"size": "t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";",
6060
"test": "cypress run",
61-
"e2e": "cypress open",
61+
"cy:open": "cypress open",
62+
"cy:run": "cypress run",
6263
"unit": "mocha src/*-spec.js",
6364
"unused-deps": "dependency-check --unused --no-dev .",
6465
"semantic-release": "semantic-action pre && npm publish && semantic-action post"

0 commit comments

Comments
 (0)