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

adds first reducer test and refactors action types

parent 883cdfc4
import * as actions from '.';
import * as types from '../constants/ActionTypes'
describe('actions', () => {
it('should create an action to add an item locally', () => {
......@@ -6,7 +7,7 @@ describe('actions', () => {
const name = 'Coconut'
const quantity = 3
const expectedAction = {
type: actions.ITEM_ADDED,
type: types.ITEM_ADDED,
id,
name,
quantity
......@@ -19,7 +20,7 @@ describe('actions', () => {
it('should create an action to update the filter', () => {
const filterTerm = 'Bananas'
const expectedAction = {
type: actions.UPDATE_FILTER,
type: types.UPDATE_FILTER,
filterTerm
}
expect(actions.updateFilter(filterTerm)).toEqual(expectedAction)
......@@ -30,7 +31,7 @@ describe('actions', () => {
it('should create an action to delete an item locally', () => {
const id = 'aaaa-bbb23'
const expectedAction = {
type: actions.ITEM_DELETED,
type: types.ITEM_DELETED,
id
}
expect(actions.itemDeleted(id)).toEqual(expectedAction)
......@@ -40,7 +41,7 @@ describe('actions', () => {
describe('actions', () => {
it('should create an action to open the add dialog', () => {
const expectedAction = {
type: actions.OPEN_ADD_DIALOG,
type: types.OPEN_ADD_DIALOG,
}
expect(actions.openAddDialog()).toEqual(expectedAction)
})
......@@ -49,7 +50,7 @@ describe('actions', () => {
describe('actions', () => {
it('should create an action to close the add dialog', () => {
const expectedAction = {
type: actions.CLOSE_ADD_DIALOG,
type: types.CLOSE_ADD_DIALOG,
}
expect(actions.closeAddDialog()).toEqual(expectedAction)
})
......@@ -59,7 +60,7 @@ describe('actions', () => {
it('should create an action to update the current name field for the add dialog', () => {
const name = 'Bananarama'
const expectedAction = {
type: actions.UPDATE_ADD_DIALOG_NAME,
type: types.UPDATE_ADD_DIALOG_NAME,
name
}
expect(actions.updateAddDialogName(name)).toEqual(expectedAction)
......@@ -70,7 +71,7 @@ describe('actions', () => {
it('should create an action to update the current quantity field for the add dialog', () => {
const quantity = 3452552253523
const expectedAction = {
type: actions.UPDATE_ADD_DIALOG_QUANTITY,
type: types.UPDATE_ADD_DIALOG_QUANTITY,
quantity
}
expect(actions.updateAddDialogQuantity(quantity)).toEqual(expectedAction)
......@@ -81,7 +82,7 @@ describe('actions', () => {
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 expectedAction = {
type: actions.ITEMS_LOADED,
type: types.ITEMS_LOADED,
items
}
expect(actions.itemsLoaded(items)).toEqual(expectedAction)
......@@ -94,7 +95,7 @@ describe('actions', () => {
const name = "Butter"
const quantity = 98
const expectedAction = {
type: actions.ITEM_UPDATED,
type: types.ITEM_UPDATED,
id,
name,
quantity
......
import Axios from 'axios';
// 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';
import * as types from '../constants/ActionTypes'
export const itemAdded = (id, name, quantity) => ({
type: ITEM_ADDED,
type: types.ITEM_ADDED,
id,
name,
quantity
})
export const updateFilter = filterTerm => ({
type: UPDATE_FILTER,
type: types.UPDATE_FILTER,
filterTerm: filterTerm
})
export const itemDeleted = id => ({
type: ITEM_DELETED,
type: types.ITEM_DELETED,
id
})
export const itemUpdated = (id, name, quantity) => ({
type: ITEM_UPDATED,
type: types.ITEM_UPDATED,
id,
name,
quantity
})
export const openAddDialog = () => ({
type: OPEN_ADD_DIALOG
type: types.OPEN_ADD_DIALOG
})
export const closeAddDialog = () => ({
type: CLOSE_ADD_DIALOG
type: types.CLOSE_ADD_DIALOG
})
export const updateAddDialogName = (name) => ({
type: UPDATE_ADD_DIALOG_NAME,
type: types.UPDATE_ADD_DIALOG_NAME,
name
})
export const updateAddDialogQuantity = (quantity) => ({
type: UPDATE_ADD_DIALOG_QUANTITY,
type: types.UPDATE_ADD_DIALOG_QUANTITY,
quantity
})
export const itemsLoaded = (items) => ({
type: ITEMS_LOADED,
type: types.ITEMS_LOADED,
items
})
......
......@@ -3,6 +3,7 @@ import thunk from 'redux-thunk'
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import * as actions from '.'
import * as types from '../constants/ActionTypes'
const middlewares = [thunk]
const mockStore = configureMockStore(middlewares)
......@@ -16,7 +17,7 @@ describe('async actions', () => {
.onGet('/api/items').replyOnce(200, items)
const expectedActions = [
{ type: actions.ITEMS_LOADED, items }
{ type: types.ITEMS_LOADED, items }
]
const store = mockStore({ items: [] })
......@@ -31,7 +32,7 @@ describe('async actions', () => {
.onDelete('/api/items/'+id).replyOnce(204)
const expectedActions = [
{ type: actions.ITEM_DELETED, id }
{ type: types.ITEM_DELETED, id }
]
const store = mockStore({ items: [item] })
......@@ -45,7 +46,7 @@ describe('async actions', () => {
.onPost('/api/items').replyOnce(200, item)
const expectedActions = [
{ type: actions.ITEM_ADDED, ...item}
{ type: types.ITEM_ADDED, ...item}
]
const store = mockStore({ items: [] })
......@@ -59,7 +60,7 @@ describe('async actions', () => {
.onPut('/api/items/'+item.id).replyOnce(204, item)
const expectedActions = [
{ type: actions.ITEM_UPDATED, ...item}
{ type: types.ITEM_UPDATED, ...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) => {
switch (action.type) {
......
import { UPDATE_FILTER } from '../actions'
import { UPDATE_FILTER } from '../constants/ActionTypes'
const filterTerm = (state = "", action) => {
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) => {
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