Commit 823b7331 authored by Martin Fahl's avatar Martin Fahl

devides actions in multiple files

parent 86133c1e
import * as actions from '.'
import * as types from '../constants/ActionTypes'
describe('actions', () => {
it('should create an action to add an item locally', () => {
const id = 'a23'
const name = 'Coconut'
const quantity = 3
const expectedAction = {
type: types.ITEM_ADDED,
id,
name,
quantity
}
expect(actions.itemAdded(id, name, quantity)).toEqual(expectedAction)
})
})
describe('actions', () => {
it('should create an action to update the filter', () => {
const filterTerm = 'Bananas'
const expectedAction = {
type: types.UPDATE_FILTER,
filterTerm
}
expect(actions.updateFilter(filterTerm)).toEqual(expectedAction)
})
})
describe('actions', () => {
it('should create an action to delete an item locally', () => {
const id = 'aaaa-bbb23'
const expectedAction = {
type: types.ITEM_DELETED,
id
}
expect(actions.itemDeleted(id)).toEqual(expectedAction)
})
})
describe('actions', () => {
it('should create an action to open the add dialog', () => {
const expectedAction = {
type: types.OPEN_ADD_DIALOG,
}
expect(actions.openAddDialog()).toEqual(expectedAction)
})
})
describe('actions', () => {
it('should create an action to close the add dialog', () => {
const expectedAction = {
type: types.CLOSE_ADD_DIALOG,
}
expect(actions.closeAddDialog()).toEqual(expectedAction)
})
})
describe('actions', () => {
it('should create an action to update the current name field for the add dialog', () => {
const name = 'Bananarama'
const expectedAction = {
type: types.UPDATE_ADD_DIALOG_NAME,
name
}
expect(actions.updateAddDialogName(name)).toEqual(expectedAction)
})
})
describe('actions', () => {
it('should create an action to update the current quantity field for the add dialog', () => {
const quantity = 3452552253523
const expectedAction = {
type: types.UPDATE_ADD_DIALOG_QUANTITY,
quantity
}
expect(actions.updateAddDialogQuantity(quantity)).toEqual(expectedAction)
})
})
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: types.ITEMS_LOADED,
items
}
expect(actions.itemsLoaded(items)).toEqual(expectedAction)
})
})
describe('actions', () => {
it('should create an action to signal that the items have been updated', () => {
const id = 1
const name = "Butter"
const quantity = 98
const expectedAction = {
type: types.ITEM_UPDATED,
id,
name,
quantity
}
expect(actions.itemUpdated(id, name, quantity)).toEqual(expectedAction)
})
})
\ No newline at end of file
import * as types from "../constants/ActionTypes"
export const openAddDialog = () => ({
type: types.OPEN_ADD_DIALOG
})
export const closeAddDialog = () => ({
type: types.CLOSE_ADD_DIALOG
})
export const updateAddDialogName = (name) => ({
type: types.UPDATE_ADD_DIALOG_NAME,
name
})
export const updateAddDialogQuantity = (quantity) => ({
type: types.UPDATE_ADD_DIALOG_QUANTITY,
quantity
})
import * as actions from "./addDialogActions"
import * as types from "../constants/ActionTypes"
describe("addDialogActions", () => {
it("should create an action to open the add dialog", () => {
const expectedAction = {
type: types.OPEN_ADD_DIALOG,
}
expect(actions.openAddDialog()).toEqual(expectedAction)
})
it("should create an action to close the add dialog", () => {
const expectedAction = {
type: types.CLOSE_ADD_DIALOG,
}
expect(actions.closeAddDialog()).toEqual(expectedAction)
})
it("should create an action to update the current name field for the add dialog", () => {
const name = "Bananarama"
const expectedAction = {
type: types.UPDATE_ADD_DIALOG_NAME,
name
}
expect(actions.updateAddDialogName(name)).toEqual(expectedAction)
})
it("should create an action to update the current quantity field for the add dialog", () => {
const quantity = 3452552253523
const expectedAction = {
type: types.UPDATE_ADD_DIALOG_QUANTITY,
quantity
}
expect(actions.updateAddDialogQuantity(quantity)).toEqual(expectedAction)
})
})
\ No newline at end of file
const debounced = (delay, fn) => {
let timerId;
return function (...args) {
if (timerId) {
clearTimeout(timerId)
}
timerId = setTimeout(() => {
fn(...args)
timerId = null
}, delay)
}
}
export default debounced
\ No newline at end of file
import * as types from "../constants/ActionTypes"
export const updateFilter = filterTerm => ({
type: types.UPDATE_FILTER,
filterTerm: filterTerm
})
\ No newline at end of file
import * as actions from "./filterTermActions"
import * as types from "../constants/ActionTypes"
describe("filterTermActions", () => {
it("should create an action to update the filter", () => {
const filterTerm = "Bananas"
const expectedAction = {
type: types.UPDATE_FILTER,
filterTerm
}
expect(actions.updateFilter(filterTerm)).toEqual(expectedAction)
})
})
import Axios from "axios"
import * as types from "../constants/ActionTypes"
export const itemAdded = (id, name, quantity) => ({
type: types.ITEM_ADDED,
id,
name,
quantity
})
export const updateFilter = filterTerm => ({
type: types.UPDATE_FILTER,
filterTerm: filterTerm
})
export const itemDeleted = id => ({
type: types.ITEM_DELETED,
id
})
export const itemUpdated = (id, name, quantity) => ({
type: types.ITEM_UPDATED,
id,
name,
quantity
})
export const openAddDialog = () => ({
type: types.OPEN_ADD_DIALOG
})
export const closeAddDialog = () => ({
type: types.CLOSE_ADD_DIALOG
})
export const updateAddDialogName = (name) => ({
type: types.UPDATE_ADD_DIALOG_NAME,
name
})
export const updateAddDialogQuantity = (quantity) => ({
type: types.UPDATE_ADD_DIALOG_QUANTITY,
quantity
})
export const itemsLoaded = (items) => ({
type: types.ITEMS_LOADED,
items
})
export const getAllItems = () => dispatch => {
return Axios.get("/api/items").then((response) => dispatch(itemsLoaded(response.data)))
}
export const addItem = (name, quantity) => dispatch => {
return Axios.post("/api/items", {name, quantity}).then((response) => dispatch(itemAdded(response.data.id, response.data.name, response.data.quantity)))
}
export const deleteItem = (id) => dispatch => {
return Axios.delete("/api/items/"+id).then(dispatch(itemDeleted(id)))
}
export const updateItem = (id, name, quantity) => dispatch => {
dispatch(itemUpdated(id, name, quantity))
debounced(500, Axios.put("/api/items/"+id, {id, name, quantity}).catch(dispatch(getAllItems)))
}
const debounced = (delay, fn) => {
let timerId;
return function (...args) {
if (timerId) {
clearTimeout(timerId);
}
timerId = setTimeout(() => {
fn(...args);
timerId = null;
}, delay);
}
}
\ No newline at end of file
export * from "./addDialogActions"
export * from "./filterTermActions"
export * from "./itemsActions"
\ No newline at end of file
import * as types from "../constants/ActionTypes"
import Axios from "axios"
import debounced from "./debounced"
export const itemAdded = (id, name, quantity) => ({
type: types.ITEM_ADDED,
id,
name,
quantity
})
export const itemDeleted = id => ({
type: types.ITEM_DELETED,
id
})
export const itemUpdated = (id, name, quantity) => ({
type: types.ITEM_UPDATED,
id,
name,
quantity
})
export const itemsLoaded = (items) => ({
type: types.ITEMS_LOADED,
items
})
export const getAllItems = () => dispatch => {
return Axios.get("/api/items").then((response) => dispatch(itemsLoaded(response.data)))
}
export const addItem = (name, quantity) => dispatch => {
return Axios.post("/api/items", {name, quantity}).then((response) => dispatch(itemAdded(response.data.id, response.data.name, response.data.quantity)))
}
export const deleteItem = (id) => dispatch => {
return Axios.delete("/api/items/"+id).then(dispatch(itemDeleted(id)))
}
export const updateItem = (id, name, quantity) => dispatch => {
dispatch(itemUpdated(id, name, quantity))
debounced(500, Axios.put("/api/items/"+id, {id, name, quantity}).catch(dispatch(getAllItems)))
}
\ No newline at end of file
import * as actions from "./itemsActions"
import * as types from "../constants/ActionTypes"
describe("itemsActions", () => {
it("should create an action to add an item locally", () => {
const id = "a23"
const name = "Coconut"
const quantity = 3
const expectedAction = {
type: types.ITEM_ADDED,
id,
name,
quantity
}
expect(actions.itemAdded(id, name, quantity)).toEqual(expectedAction)
})
it("should create an action to delete an item locally", () => {
const id = "aaaa-bbb23"
const expectedAction = {
type: types.ITEM_DELETED,
id
}
expect(actions.itemDeleted(id)).toEqual(expectedAction)
})
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)
})
it("should create an action to signal that the items have been updated", () => {
const id = 1
const name = "Butter"
const quantity = 98
const expectedAction = {
type: types.ITEM_UPDATED,
id,
name,
quantity
}
expect(actions.itemUpdated(id, name, quantity)).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 '.'
import * as types from '../constants/ActionTypes'
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', () => {
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}]
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)
.onGet("/api/items").replyOnce(200, items)
const expectedActions = [
{ type: types.ITEMS_LOADED, items }
......@@ -25,11 +25,11 @@ describe('async actions', () => {
return expect(store.getActions()).toEqual(expectedActions)
})
it('creates ITEM_DELETED when deleting an item has been done', async () => {
it("creates ITEM_DELETED when deleting an item has been done", async () => {
const id = 25
const item = { id, name: "Axe", quantity: 5}
mockAxios
.onDelete('/api/items/'+id).replyOnce(204)
.onDelete("/api/items/"+id).replyOnce(204)
const expectedActions = [
{ type: types.ITEM_DELETED, id }
......@@ -40,10 +40,10 @@ describe('async actions', () => {
return expect(store.getActions()).toEqual(expectedActions)
})
it('creates ITEM_ADDED when adding an item has been done', async () => {
it("creates ITEM_ADDED when adding an item has been done", async () => {
const item = { id: 25, name: "Hammer", quantity: 10}
mockAxios
.onPost('/api/items').replyOnce(200, item)
.onPost("/api/items").replyOnce(200, item)
const expectedActions = [
{ type: types.ITEM_ADDED, ...item}
......@@ -54,10 +54,10 @@ describe('async actions', () => {
return expect(store.getActions()).toEqual(expectedActions)
})
it('creates ITEM_UPDATED when an item has been updated', async () => {
it("creates ITEM_UPDATED when an item has been updated", async () => {
const item = { id: 25, name: "Hammer", quantity: 10}
mockAxios
.onPut('/api/items/'+item.id).replyOnce(204, item)
.onPut("/api/items/"+item.id).replyOnce(204, item)
const expectedActions = [
{ type: types.ITEM_UPDATED, ...item}
......
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