Commit 883cdfc4 authored by Martin Fahl's avatar Martin Fahl

adds action tests

parent ab565227
......@@ -3,7 +3,7 @@ import * as actions from '.';
describe('actions', () => {
it('should create an action to add an item locally', () => {
const id = 'a23'
const name = 'test'
const name = 'Coconut'
const quantity = 3
const expectedAction = {
type: actions.ITEM_ADDED,
......@@ -79,7 +79,7 @@ describe('actions', () => {
describe('actions', () => {
it('should create an action to signal that the items have been loaded', () => {
const items = [{id: 1, name: "Hugo", quantity: 98},{id: 2, name: "Peter", quantity: 22}]
const items = [{id: 1, name: "Lemon", quantity: 98},{id: 2, name: "Orange", quantity: 22}]
const expectedAction = {
type: actions.ITEMS_LOADED,
items
......@@ -91,7 +91,7 @@ describe('actions', () => {
describe('actions', () => {
it('should create an action to signal that the items have been updated', () => {
const id = 1
const name = "Hugo"
const name = "Butter"
const quantity = 98
const expectedAction = {
type: actions.ITEM_UPDATED,
......
......@@ -58,15 +58,15 @@ export const itemsLoaded = (items) => ({
})
export const getAllItems = () => dispatch => {
Axios.get('/api/items').then((response) => dispatch(itemsLoaded(response.data)))
return Axios.get('/api/items').then((response) => dispatch(itemsLoaded(response.data)))
}
export const addItem = (name, quantity) => dispatch => {
Axios.post('/api/items', {name, quantity}).then((response) => dispatch(itemAdded(response.data.id, response.data.name, response.data.quantity)))
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 => {
Axios.delete('/api/items/'+id).then(dispatch(itemDeleted(id)))
return Axios.delete('/api/items/'+id).then(dispatch(itemDeleted(id)))
}
export const updateItem = (id, name, quantity) => dispatch => {
......
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 '.'
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: actions.ITEMS_LOADED, items }
]
const store = mockStore({ items: [] })
await store.dispatch(actions.getAllItems())
return expect(store.getActions()).toEqual(expectedActions)
})
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)
const expectedActions = [
{ type: actions.ITEM_DELETED, id }
]
const store = mockStore({ items: [item] })
await store.dispatch(actions.deleteItem(id))
return expect(store.getActions()).toEqual(expectedActions)
})
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)
const expectedActions = [
{ type: actions.ITEM_ADDED, ...item}
]
const store = mockStore({ items: [] })
await store.dispatch(actions.addItem(item.name, item.quantity))
return expect(store.getActions()).toEqual(expectedActions)
})
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)
const expectedActions = [
{ type: actions.ITEM_UPDATED, ...item}
]
const store = mockStore({ items: [item] })
await store.dispatch(actions.updateItem(item.id, item.name, item.quantity))
return expect(store.getActions()).toEqual(expectedActions)
})
})
\ No newline at end of file
......@@ -409,6 +409,12 @@ aws4@^1.6.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.7.0.tgz#d4d0e9b9dbfca77bf08eeb0a8a471550fe39e289"
axios-mock-adapter@^1.15.0:
version "1.15.0"
resolved "https://registry.yarnpkg.com/axios-mock-adapter/-/axios-mock-adapter-1.15.0.tgz#fbc06825d8302c95c3334d21023bba996255d45d"
dependencies:
deep-equal "^1.0.1"
axios@^0.18.0:
version "0.18.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.18.0.tgz#32d53e4851efdc0a11993b6cd000789d70c05102"
......@@ -4567,6 +4573,10 @@ lodash.flattendeep@^4.4.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/lodash.flattendeep/-/lodash.flattendeep-4.4.0.tgz#fb030917f86a3134e5bc9bec0d69e0013ddfedb2"
lodash.isplainobject@^4.0.6:
version "4.0.6"
resolved "https://registry.yarnpkg.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb"
lodash.memoize@^4.1.2:
version "4.1.2"
resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
......@@ -6203,6 +6213,12 @@ reduce-function-call@^1.0.1:
dependencies:
balanced-match "^0.4.2"
redux-mock-store@^1.5.3:
version "1.5.3"
resolved "https://registry.yarnpkg.com/redux-mock-store/-/redux-mock-store-1.5.3.tgz#1f10528949b7ce8056c2532624f7cafa98576c6d"
dependencies:
lodash.isplainobject "^4.0.6"
redux-thunk@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/redux-thunk/-/redux-thunk-2.3.0.tgz#51c2c19a185ed5187aaa9a2d08b666d0d6467622"
......
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