Skip to content

Commit

Permalink
docs(examples/tasks): add tasks entity
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad Hasani committed Feb 14, 2019
1 parent f6c8247 commit 35372da
Show file tree
Hide file tree
Showing 11 changed files with 437 additions and 7 deletions.
6 changes: 5 additions & 1 deletion examples/tasks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
"dependencies": {
"@types/redux": "^3.6.0",
"deox": "^1.0.2",
"ramda": "^0.26.1",
"react": "^16.7.0",
"react-dom": "^16.7.0",
"react-redux": "^5.1.1",
"redux": "^4.0.1",
"redux-devtools-extension": "^2.13.7"
},
"devDependencies": {
"@types/ramda": "types/npm-ramda#dist",
"@types/react": "^16.7.20",
"@types/react-dom": "^16.0.11",
"@types/react-redux": "^7.0.0",
Expand All @@ -33,6 +35,8 @@
"parcel-bundler": "^1.11.0",
"rimraf": "^2.6.3",
"sass": "^1.16.1",
"ts-jest": "^23.10.5"
"ts-jest": "^23.10.5",
"tslint": "^5.12.1",
"typescript": "^3.2.4"
}
}
153 changes: 153 additions & 0 deletions examples/tasks/src/store/__tests__/__snapshots__/tasks.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`tasks entity action creators should create an action to add task 1`] = `
Object {
"payload": Object {
"description": "Obviously a good tasks is good",
"id": "5",
"isFinished": false,
"title": "A good task",
},
"type": "[Tasks] add",
}
`;

exports[`tasks entity action creators should create an action to add task 2`] = `
Object {
"payload": Object {
"description": "Obviously a good tasks is good",
"id": "5",
"isFinished": true,
"title": "A good task",
},
"type": "[Tasks] add",
}
`;

exports[`tasks entity action creators should create an action to edit task 1`] = `
Object {
"payload": Object {
"x": Object {
"isFinished": true,
"title": "New title for task",
},
},
"type": "[Tasks] edit",
}
`;

exports[`tasks entity action creators should create an action to remove all finished tasks 1`] = `
Object {
"type": "[Tasks] remove finished",
}
`;

exports[`tasks entity action creators should create an action to remove task 1`] = `
Object {
"payload": "x",
"type": "[Tasks] remove",
}
`;

exports[`tasks entity tasks reducer should handle "[Tasks] add" 1`] = `
Object {
"allIds": Array [
"5",
],
"byId": Object {
"5": Object {
"description": "Obviously a good tasks is good",
"id": "5",
"isFinished": false,
"title": "A good task",
},
},
}
`;

exports[`tasks entity tasks reducer should handle "[Tasks] add" 2`] = `
Object {
"allIds": Array [
"5",
],
"byId": Object {
"5": Object {
"description": "Obviously a good tasks is good",
"id": "5",
"isFinished": true,
"title": "A good task",
},
},
}
`;

exports[`tasks entity tasks reducer should handle "[Tasks] edit" 1`] = `
Object {
"allIds": Array [
"x",
],
"byId": Object {
"x": Object {
"description": "Obviously a good tasks is good",
"id": "x",
"isFinished": false,
"title": "New title for task",
},
},
}
`;

exports[`tasks entity tasks reducer should handle "[Tasks] edit" 2`] = `
Object {
"allIds": Array [
"x",
],
"byId": Object {
"x": Object {
"description": "New description for task",
"id": "x",
"isFinished": false,
"title": "A good task",
},
},
}
`;

exports[`tasks entity tasks reducer should handle "[Tasks] edit" 3`] = `
Object {
"allIds": Array [
"x",
],
"byId": Object {
"x": Object {
"description": "Obviously a good tasks is good",
"id": "x",
"isFinished": true,
"title": "A good task",
},
},
}
`;

exports[`tasks entity tasks reducer should handle "[Tasks] remove finished" 1`] = `
Object {
"allIds": Array [
"y",
],
"byId": Object {
"y": Object {
"description": ":|",
"id": "y",
"isFinished": false,
"title": "Another good task",
},
},
}
`;

exports[`tasks entity tasks reducer should handle "[Tasks] remove" 1`] = `
Object {
"allIds": Array [],
"byId": Object {},
}
`;
130 changes: 130 additions & 0 deletions examples/tasks/src/store/__tests__/tasks.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import {
addTask,
editTask,
removeFinishedTasks,
removeTask,
tasks,
} from '../tasks'

describe('tasks entity', () => {
describe('action creators', () => {
it.each([
{
title: 'A good task',
description: 'Obviously a good tasks is good',
},
{
title: 'A good task',
description: 'Obviously a good tasks is good',
isFinished: true,
},
])('should create an action to add task', task => {
const now = Date.now
Date.now = () => 5

expect(addTask(task)).toMatchSnapshot()

Date.now = now
})

it('should create an action to remove task', () => {
expect(removeTask('x')).toMatchSnapshot()
})

it('should create an action to edit task', () => {
const id = 'x'
const update = { title: 'New title for task', isFinished: true }
expect(editTask(id, update)).toMatchSnapshot()
})

it('should create an action to remove all finished tasks', () => {
expect(removeFinishedTasks()).toMatchSnapshot()
})
})

describe('tasks reducer', () => {
it.each([
{
title: 'A good task',
description: 'Obviously a good tasks is good',
},
{
title: 'A good task',
description: 'Obviously a good tasks is good',
isFinished: true,
},
])('should handle "[Tasks] add"', task => {
const now = Date.now
Date.now = () => 5

const action = addTask(task)

expect(tasks(undefined, action)).toMatchSnapshot()

Date.now = now
})

it('should handle "[Tasks] remove"', () => {
const id = 'x'
const state = {
byId: {
[id]: {
id,
title: 'A good task',
description: 'Obviously a good tasks is good',
isFinished: false,
},
},
allIds: [id],
}
const action = removeTask(id)

expect(tasks(state, action)).toMatchSnapshot()
})

it.each([
{ title: 'New title for task' },
{ description: 'New description for task' },
{ isFinished: true },
])('should handle "[Tasks] edit"', update => {
const id = 'x'
const state = {
byId: {
[id]: {
id,
title: 'A good task',
description: 'Obviously a good tasks is good',
isFinished: false,
},
},
allIds: [id],
}
const action = editTask(id, update)

expect(tasks(state, action)).toMatchSnapshot()
})

it('should handle "[Tasks] remove finished"', () => {
const state = {
byId: {
x: {
id: 'x',
title: 'A good task',
description: 'Obviously a good tasks is good',
isFinished: true,
},
y: {
id: 'y',
title: 'Another good task',
description: ':|',
isFinished: false,
},
},
allIds: ['x', 'y'],
}
const action = removeFinishedTasks()

expect(tasks(state, action)).toMatchSnapshot()
})
})
})
10 changes: 10 additions & 0 deletions examples/tasks/src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
export { configureStore } from './configure-store'

export { RootState } from './root'

export {
addTask,
removeTask,
removeFinishedTasks,
editTask,
Task,
} from './tasks'

export * from './selectors'
4 changes: 3 additions & 1 deletion examples/tasks/src/store/root.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { combineReducers } from 'redux'

import { tasks } from './tasks'

export type RootState = ReturnType<typeof root>

export const root = combineReducers({})
export const root = combineReducers({ tasks })
5 changes: 5 additions & 0 deletions examples/tasks/src/store/selectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { RootState } from './root'
import * as fromTasks from './tasks'

export const getAllTasks = (state: RootState) =>
fromTasks.getAllTasks(state.tasks)
Loading

0 comments on commit 35372da

Please sign in to comment.