Commit 5bf935b0 authored by Martin Fahl's avatar Martin Fahl
Browse files

adds first reducer test and refactors action types

parent 883cdfc4
import * as actions from '.'; import * as actions from '.';
import * as types from '../constants/ActionTypes'
describe('actions', () => { describe('actions', () => {
it('should create an action to add an item locally', () => { it('should create an action to add an item locally', () => {
...@@ -6,7 +7,7 @@ describe('actions', () => { ...@@ -6,7 +7,7 @@ describe('actions', () => {
const name = 'Coconut' const name = 'Coconut'
const quantity = 3 const quantity = 3
const expectedAction = { const expectedAction = {
type: actions.ITEM_ADDED, type: types.ITEM_ADDED,
id, id,
name, name,
quantity quantity
...@@ -19,7 +20,7 @@ describe('actions', () => { ...@@ -19,7 +20,7 @@ describe('actions', () => {
it('should create an action to update the filter', () => { it('should create an action to update the filter', () => {
const filterTerm = 'Bananas' const filterTerm = 'Bananas'
const expectedAction = { const expectedAction = {
type: actions.UPDATE_FILTER, type: types.UPDATE_FILTER,
filterTerm filterTerm
} }
expect(actions.updateFilter(filterTerm)).toEqual(expectedAction) expect(actions.updateFilter(filterTerm)).toEqual(expectedAction)
...@@ -30,7 +31,7 @@ describe('actions', () => { ...@@ -30,7 +31,7 @@ describe('actions', () => {
it('should create an action to delete an item locally', () => { it('should create an action to delete an item locally', () => {
const id = 'aaaa-bbb23' const id = 'aaaa-bbb23'
const expectedAction = { const expectedAction = {
type: actions.ITEM_DELETED, type: types.ITEM_DELETED,
id id
} }
expect(actions.itemDeleted(id)).toEqual(expectedAction) expect(actions.itemDeleted(id)).toEqual(expectedAction)
...@@ -40,7 +41,7 @@ describe('actions', () => { ...@@ -40,7 +41,7 @@ describe('actions', () => {
describe('actions', () => { describe('actions', () => {
it('should create an action to open the add dialog', () => { it('should create an action to open the add dialog', () => {
const expectedAction = { const expectedAction = {
type: actions.OPEN_ADD_DIALOG, type: types.OPEN_ADD_DIALOG,
} }
expect(actions.openAddDialog()).toEqual(expectedAction) expect(actions.openAddDialog()).toEqual(expectedAction)
}) })
...@@ -49,7 +50,7 @@ describe('actions', () => { ...@@ -49,7 +50,7 @@ describe('actions', () => {
describe('actions', () => { describe('actions', () => {
it('should create an action to close the add dialog', () => { it('should create an action to close the add dialog', () => {
const expectedAction = { const expectedAction = {
type: actions.CLOSE_ADD_DIALOG, type: types.CLOSE_ADD_DIALOG,
} }
expect(actions.closeAddDialog()).toEqual(expectedAction) expect(actions.closeAddDialog()).toEqual(expectedAction)
}) })
...@@ -59,7 +60,7 @@ describe('actions', () => { ...@@ -59,7 +60,7 @@ describe('actions', () => {
it('should create an action to update the current name field for the add dialog', () => { it('should create an action to update the current name field for the add dialog', () => {
const name = 'Bananarama' const name = 'Bananarama'
const expectedAction = { const expectedAction = {
type: actions.UPDATE_ADD_DIALOG_NAME, type: types.UPDATE_ADD_DIALOG_NAME,
name name
} }
expect(actions.updateAddDialogName(name)).toEqual(expectedAction) expect(actions.updateAddDialogName(name)).toEqual(expectedAction)
...@@ -70,7 +71,7 @@ describe('actions', () => { ...@@ -70,7 +71,7 @@ describe('actions', () => {
it('should create an action to update the current quantity field for the add dialog', () => { it('should create an action to update the current quantity field for the add dialog', () => {
const quantity = 3452552253523 const quantity = 3452552253523
const expectedAction = { const expectedAction = {
type: actions.UPDATE_ADD_DIALOG_QUANTITY, type: types.UPDATE_ADD_DIALOG_QUANTITY,
quantity quantity
} }
expect(actions.updateAddDialogQuantity(quantity)).toEqual(expectedAction) expect(actions.updateAddDialogQuantity(quantity)).toEqual(expectedAction)
...@@ -81,7 +82,7 @@ describe('actions', () => { ...@@ -81,7 +82,7 @@ describe('actions', () => {
it('should create an action to signal that the items have been loaded', () => { it('should create an action to signal that the items have been loaded', () => {
const items = [{id: 1, name: "Lemon", quantity: 98},{id: 2, name: "Orange", quantity: 22}] const items = [{id: 1, name: "Lemon", quantity: 98},{id: 2, name: "Orange", quantity: 22}]
const expectedAction = { const expectedAction = {
type: actions.ITEMS_LOADED, type: types.ITEMS_LOADED,
items items
} }
expect(actions.itemsLoaded(items)).toEqual(expectedAction) expect(actions.itemsLoaded(items)).toEqual(expectedAction)
...@@ -94,7 +95,7 @@ describe('actions', () => { ...@@ -94,7 +95,7 @@ describe('actions', () => {
const name = "Butter" const name = "Butter"
const quantity = 98 const quantity = 98
const expectedAction = { const expectedAction = {
type: actions.ITEM_UPDATED, type: types.ITEM_UPDATED,
id, id,
name, name,
quantity quantity
......
import Axios from 'axios'; import Axios from 'axios';
// actions import * as types from '../constants/ActionTypes'
export const ITEM_ADDED = 'ITEM_ADDED';
export const UPDATE_FILTER = 'UPDATE_FILTER';
export const ITEM_DELETED = 'ITEM_DELETED';
export const ITEM_UPDATED = 'ITEM_UPDATED';
export const ITEMS_LOADED = 'ITEMS_LOADED';
export const UPDATE_ADD_DIALOG_NAME = 'UPDATE_ADD_DIALOG_NAME';
export const UPDATE_ADD_DIALOG_QUANTITY = 'UPDATE_ADD_DIALOG_QUANTITY';
export const OPEN_ADD_DIALOG = 'OPEN_ADD_DIALOG';
export const CLOSE_ADD_DIALOG = 'CLOSE_ADD_DIALOG';
export const itemAdded = (id, name, quantity) => ({ export const itemAdded = (id, name, quantity) => ({
type: ITEM_ADDED, type: types.ITEM_ADDED,
id, id,
name, name,
quantity quantity
}) })
export const updateFilter = filterTerm => ({ export const updateFilter = filterTerm => ({
type: UPDATE_FILTER, type: types.UPDATE_FILTER,
filterTerm: filterTerm filterTerm: filterTerm
}) })
export const itemDeleted = id => ({ export const itemDeleted = id => ({
type: ITEM_DELETED, type: types.ITEM_DELETED,
id id
}) })
export const itemUpdated = (id, name, quantity) => ({ export const itemUpdated = (id, name, quantity) => ({
type: ITEM_UPDATED, type: types.ITEM_UPDATED,
id, id,
name, name,
quantity quantity
}) })
export const openAddDialog = () => ({ export const openAddDialog = () => ({
type: OPEN_ADD_DIALOG type: types.OPEN_ADD_DIALOG
}) })
export const closeAddDialog = () => ({ export const closeAddDialog = () => ({
type: CLOSE_ADD_DIALOG type: types.CLOSE_ADD_DIALOG
}) })
export const updateAddDialogName = (name) => ({ export const updateAddDialogName = (name) => ({
type: UPDATE_ADD_DIALOG_NAME, type: types.UPDATE_ADD_DIALOG_NAME,
name name
}) })
export const updateAddDialogQuantity = (quantity) => ({ export const updateAddDialogQuantity = (quantity) => ({
type: UPDATE_ADD_DIALOG_QUANTITY, type: types.UPDATE_ADD_DIALOG_QUANTITY,
quantity quantity
}) })
export const itemsLoaded = (items) => ({ export const itemsLoaded = (items) => ({
type: ITEMS_LOADED, type: types.ITEMS_LOADED,
items items
}) })
......
...@@ -3,6 +3,7 @@ import thunk from 'redux-thunk' ...@@ -3,6 +3,7 @@ import thunk from 'redux-thunk'
import axios from 'axios'; import axios from 'axios';
import MockAdapter from 'axios-mock-adapter'; import MockAdapter from 'axios-mock-adapter';
import * as actions from '.' import * as actions from '.'
import * as types from '../constants/ActionTypes'
const middlewares = [thunk] const middlewares = [thunk]
const mockStore = configureMockStore(middlewares) const mockStore = configureMockStore(middlewares)
...@@ -16,7 +17,7 @@ describe('async actions', () => { ...@@ -16,7 +17,7 @@ describe('async actions', () => {
.onGet('/api/items').replyOnce(200, items) .onGet('/api/items').replyOnce(200, items)
const expectedActions = [ const expectedActions = [
{ type: actions.ITEMS_LOADED, items } { type: types.ITEMS_LOADED, items }
] ]
const store = mockStore({ items: [] }) const store = mockStore({ items: [] })
...@@ -31,7 +32,7 @@ describe('async actions', () => { ...@@ -31,7 +32,7 @@ describe('async actions', () => {
.onDelete('/api/items/'+id).replyOnce(204) .onDelete('/api/items/'+id).replyOnce(204)
const expectedActions = [ const expectedActions = [
{ type: actions.ITEM_DELETED, id } { type: types.ITEM_DELETED, id }
] ]
const store = mockStore({ items: [item] }) const store = mockStore({ items: [item] })
...@@ -45,7 +46,7 @@ describe('async actions', () => { ...@@ -45,7 +46,7 @@ describe('async actions', () => {
.onPost('/api/items').replyOnce(200, item) .onPost('/api/items').replyOnce(200, item)
const expectedActions = [ const expectedActions = [
{ type: actions.ITEM_ADDED, ...item} { type: types.ITEM_ADDED, ...item}
] ]
const store = mockStore({ items: [] }) const store = mockStore({ items: [] })
...@@ -59,7 +60,7 @@ describe('async actions', () => { ...@@ -59,7 +60,7 @@ describe('async actions', () => {
.onPut('/api/items/'+item.id).replyOnce(204, item) .onPut('/api/items/'+item.id).replyOnce(204, item)
const expectedActions = [ const expectedActions = [
{ type: actions.ITEM_UPDATED, ...item} { type: types.ITEM_UPDATED, ...item}
] ]
const store = mockStore({ items: [item] }) const store = mockStore({ items: [item] })
......
// actions
export const ITEM_ADDED = 'ITEM_ADDED';
export const UPDATE_FILTER = 'UPDATE_FILTER';
export const ITEM_DELETED = 'ITEM_DELETED';
export const ITEM_UPDATED = 'ITEM_UPDATED';
export const ITEMS_LOADED = 'ITEMS_LOADED';
export const UPDATE_ADD_DIALOG_NAME = 'UPDATE_ADD_DIALOG_NAME';
export const UPDATE_ADD_DIALOG_QUANTITY = 'UPDATE_ADD_DIALOG_QUANTITY';
export const OPEN_ADD_DIALOG = 'OPEN_ADD_DIALOG';
export const CLOSE_ADD_DIALOG = 'CLOSE_ADD_DIALOG';
\ No newline at end of file
import { OPEN_ADD_DIALOG, CLOSE_ADD_DIALOG, UPDATE_ADD_DIALOG_NAME, UPDATE_ADD_DIALOG_QUANTITY } from '../actions' import { OPEN_ADD_DIALOG, CLOSE_ADD_DIALOG, UPDATE_ADD_DIALOG_NAME, UPDATE_ADD_DIALOG_QUANTITY } from '../constants/ActionTypes'
const addDialog = (state = {isOpen: false, name: "", quantity: 0}, action) => { const addDialog = (state = {isOpen: false, name: "", quantity: 0}, action) => {
switch (action.type) { switch (action.type) {
......
import { UPDATE_FILTER } from '../actions' import { UPDATE_FILTER } from '../constants/ActionTypes'
const filterTerm = (state = "", action) => { const filterTerm = (state = "", action) => {
switch (action.type) { switch (action.type) {
......
import reducer from './filterTerm'
import * as actions from '../actions'
import * as types from '../constants/ActionTypes'
describe('filterTerm reducer', () => {
it('should return the initial state', () => {
expect(reducer(undefined, {})).toEqual("")
})
it('should handle UPDATE_FILTER', () => {
expect(
reducer([], {
type: types.UPDATE_FILTER,
filterTerm: 'Police Car'
})
).toEqual(
'Police Car'
)
expect(
reducer(
'Mac',
{
type: types.UPDATE_FILTER,
filterTerm: 'Macbook'
}
)
).toEqual(
'Macbook'
)
})
})
\ No newline at end of file
import { ITEM_DELETED, ITEMS_LOADED, ITEM_ADDED, ITEM_UPDATED } from '../actions'; import { ITEM_DELETED, ITEMS_LOADED, ITEM_ADDED, ITEM_UPDATED } from '../constants/ActionTypes';
const items = (state = [], action) => { const items = (state = [], action) => {
switch (action.type) { switch (action.type) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment