Commit 88603a69 authored by Martin Fahl's avatar Martin Fahl

removes code for adding items

parent d0956bc4
...@@ -3,13 +3,5 @@ import * as types from "../../constants/ActionTypes" ...@@ -3,13 +3,5 @@ import * as types from "../../constants/ActionTypes"
describe("itemsActions", () => { describe("itemsActions", () => {
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: types.ITEMS_LOADED,
items
}
expect(actions.itemsLoaded(items)).toEqual(expectedAction)
})
}) })
\ No newline at end of file
import configureMockStore from "redux-mock-store"
import thunk from "redux-thunk"
import axios from "axios";
import MockAdapter from "axios-mock-adapter";
import * as actions from "../itemsActions"
import * as types from "../../constants/ActionTypes"
const middlewares = [thunk]
const mockStore = configureMockStore(middlewares)
const mockAxios = new MockAdapter(axios)
describe("async actions", () => {
it("creates ITEMS_LOADED when fetching items has been done", async () => {
const items = [{id: 1, name: "Football", quantity: 2},{id: 2, name: "Headphones", quantity: 3}]
mockAxios
.onGet("/api/items").replyOnce(200, items)
const expectedActions = [
{ type: types.ITEMS_LOADED, items }
]
const store = mockStore({ items: [] })
await store.dispatch(actions.getAllItems())
return expect(store.getActions()).toEqual(expectedActions)
})
})
\ No newline at end of file
import * as types from "../constants/ActionTypes" import * as types from "../constants/ActionTypes"
export const addItem = (id, name, quantity) => ({
type: types.ITEM_ADDED,
id,
name,
quantity
})
export const deleteItem = id => ({ export const deleteItem = id => ({
type: types.ITEM_DELETED, type: types.ITEM_DELETED,
id id
......
import { connect } from 'react-redux' import { connect } from 'react-redux'
import AddDialog from '../../components/AddDialog/AddDialog' import AddDialog from '../../components/AddDialog/AddDialog'
import { createSelector } from 'reselect' import { createSelector } from 'reselect'
import { closeAddDialog, updateAddDialogName, updateAddDialogQuantity, addItem } from '../../actions' import { closeAddDialog, updateAddDialogName, updateAddDialogQuantity } from '../../actions'
const getAddDialog = state => state.addDialog; const getAddDialog = state => state.addDialog;
...@@ -26,14 +26,9 @@ const getNewQuantity = createSelector( ...@@ -26,14 +26,9 @@ const getNewQuantity = createSelector(
} }
) )
const genId = () => {
return String(Math.floor(Math.random()*10000))
}
const handleAdd = (name, quantity, dispatch) => { const handleAdd = (name, quantity, dispatch) => {
if (name !== "") { if (name !== "") {
dispatch(closeAddDialog()); dispatch(closeAddDialog());
dispatch(addItem(genId(), name, quantity))
} }
} }
......
...@@ -5,7 +5,6 @@ import { Provider } from "react-redux" ...@@ -5,7 +5,6 @@ import { Provider } from "react-redux"
import registerServiceWorker from "./registerServiceWorker"; import registerServiceWorker from "./registerServiceWorker";
import rootReducer from "./reducers" import rootReducer from "./reducers"
import App from "./components/App/App"; import App from "./components/App/App";
import { addItem } from "./actions";
const store = createStore( const store = createStore(
rootReducer rootReducer
...@@ -16,10 +15,6 @@ store.subscribe(() => { ...@@ -16,10 +15,6 @@ store.subscribe(() => {
console.log(store.getState()); console.log(store.getState());
}); });
store.dispatch(addItem("1156", "Milk", 4));
store.dispatch(addItem("2257", "Chocolate", 1));
store.dispatch(addItem("3358", "Wine", 40));
render( render(
<Provider store={store}> <Provider store={store}>
<App /> <App />
......
...@@ -12,27 +12,10 @@ describe("items reducer", () => { ...@@ -12,27 +12,10 @@ describe("items reducer", () => {
item3 = {id: "ab25", name: "Couch", quantity: 3} item3 = {id: "ab25", name: "Couch", quantity: 3}
}) })
it("should return the initial state", () => { it("should return the initial state", () => {
expect(reducer(undefined, {})).toEqual([]) expect(reducer(undefined, {})).toEqual([
}) {id: "1156", name: "Peanuts", quantity: 4},
{id: "2257", name: "Chocolate", quantity: 1},
it("should handle ITEMS_LOADED", () => { {id: "3358", name: "Wine", quantity: 40} ])
expect(
reducer([], {
type: types.ITEMS_LOADED,
items: [item1, item2]
})
).toEqual(
[item1, item2]
)
expect(
reducer([item3], {
type: types.ITEMS_LOADED,
items: [item1, item2]
})
).toEqual(
[item1, item2]
)
}) })
it("should handle ITEM_ADDED", () => { it("should handle ITEM_ADDED", () => {
...@@ -55,43 +38,4 @@ describe("items reducer", () => { ...@@ -55,43 +38,4 @@ describe("items reducer", () => {
) )
}) })
it("should handle ITEM_DELETED", () => {
expect(
reducer([], {
type: types.ITEM_DELETED,
id: "5"
})
).toEqual(
[]
)
expect(
reducer([item1, item2], {
type: types.ITEM_DELETED,
id: item2.id,
})
).toEqual(
[item1]
)
})
it("should handle ITEM_UPDATED", () => {
expect(
reducer([], {
type: types.ITEM_UPDATED,
...item1
})
).toEqual(
[]
)
expect(
reducer([item1, item3], {
type: types.ITEM_UPDATED,
...item3,
quantity: 6
})
).toEqual(
[item1, {...item3, quantity: 6}]
)
})
}) })
\ No newline at end of file
import { ITEM_DELETED, ITEMS_LOADED, ITEM_ADDED, ITEM_UPDATED } from "../constants/ActionTypes"; import { ITEM_DELETED, ITEMS_LOADED, ITEM_ADDED, ITEM_UPDATED } from "../constants/ActionTypes";
const items = (state = [], action) => { const INITIAL_STATE = [ {id: "1156", name: "Peanuts", quantity: 4},
{id: "2257", name: "Chocolate", quantity: 1},
{id: "3358", name: "Wine", quantity: 40} ]
const items = (state = INITIAL_STATE, action) => {
switch (action.type) { switch (action.type) {
case ITEMS_LOADED:
return action.items
case ITEM_ADDED:
return [
...state,
{
id: action.id,
name: action.name,
quantity: action.quantity
}
]
case ITEM_DELETED: case ITEM_DELETED:
return state.filter(item => (item.id !== action.id)) return state.filter(item => (item.id !== action.id))
case ITEM_UPDATED: case ITEM_UPDATED:
......
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