-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Delete): User should be able to delete an article
- Add button to delete an article - Add Component for delete article. - Add tests for the detete article feature [#162696281]
- Loading branch information
ATUHAIRE JUDE INNOCENT
committed
Dec 21, 2018
1 parent
cbf6795
commit c8622d0
Showing
21 changed files
with
385 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import axios from 'axios'; | ||
import ACTION_TYPE from '../actionTypes'; | ||
import APP_URL from '../../utils/constants'; | ||
|
||
export const deleteArticleSuccess = response => ({ | ||
type: ACTION_TYPE.DELETE_ARTICLE_SUCCESS, | ||
payload: response, | ||
}); | ||
|
||
export const deleteArticleFailure = errorMessage => ({ | ||
type: ACTION_TYPE.DELETE_ARTICLE_FAILED, | ||
payload: errorMessage, | ||
}); | ||
|
||
const tok = localStorage.getItem('token'); | ||
export const deleteArticleThunk = articleId => (dispatch) => { | ||
const url = `${APP_URL}/articles/${articleId}`; | ||
const token = `Token ${tok}`; | ||
const headers = { | ||
headers: { Authorization: token }, | ||
}; | ||
return axios.delete(url, headers) | ||
.then((response) => { | ||
dispatch(deleteArticleSuccess(response.data.results)); | ||
}) | ||
.catch((error) => { | ||
dispatch(deleteArticleFailure(error.response)); | ||
}); | ||
}; |
58 changes: 58 additions & 0 deletions
58
src/actions/deleteArticleActions/deleteArticleAction.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import moxios from 'moxios'; | ||
import configureMockStore from 'redux-mock-store'; | ||
import thunk from 'redux-thunk'; | ||
import { | ||
deleteArticleSuccess, deleteArticleThunk, | ||
} from './deleteArticleAction'; | ||
import ACTION_TYPE from '../actionTypes'; | ||
import APP_URL from '../../utils/constants'; | ||
|
||
|
||
describe('Create article Actions tests', () => { | ||
let store; | ||
let actionTypesData; | ||
let response; | ||
beforeEach(() => { | ||
response = { delete: 'Article deleted successfully' }; | ||
const mockStore = configureMockStore([thunk]); | ||
store = mockStore({}); | ||
actionTypesData = actionType => ({ | ||
type: actionType, | ||
}); | ||
// import and pass your custom axios instance to this method | ||
moxios.install(); | ||
}); | ||
afterEach(() => { | ||
// import and pass your custom axios instance to this method | ||
moxios.uninstall(); | ||
}); | ||
test('Successful delete article action', () => { | ||
expect(deleteArticleSuccess(response)).toEqual(expect.objectContaining( | ||
actionTypesData(ACTION_TYPE.DELETE_ARTICLE_SUCCESS), | ||
)); | ||
}); | ||
test('Delete article successfull', () => { | ||
moxios.stubRequest(`${APP_URL}/articles/7`, { | ||
status: 200, | ||
}); | ||
store.dispatch(deleteArticleThunk(7)).then(() => { | ||
expect(store.getActions()).toEqual(expect.objectContaining( | ||
{ | ||
type: ACTION_TYPE.CREATE_ARTICLE_SUCCESS, | ||
}, | ||
)); | ||
}); | ||
}); | ||
test('Delete article failed', () => { | ||
moxios.stubRequest(`${APP_URL}/articles/7`, { | ||
status: 400, | ||
}); | ||
store.dispatch(deleteArticleThunk(7)).catch(() => { | ||
expect(store.getActions()).toEqual(expect.objectContaining( | ||
[{ | ||
type: ACTION_TYPE.DELETE_ARTICLE_FAILED, | ||
}], | ||
)); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/components/deleteArticle/__snapshots__/deleteArticle.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`<DeleteArticle /> renders the component 1`] = ` | ||
<div> | ||
<button | ||
className="btn btn-danger" | ||
id="delete-bt" | ||
onClick={[MockFunction]} | ||
type="submit" | ||
> | ||
DELETE ARTICLE | ||
</button> | ||
</div> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#delete-bt { | ||
padding: 10px; | ||
font-size: 12px | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
import DeleteArticle from './index'; | ||
|
||
describe('<DeleteArticle /> ', () => { | ||
const DeleteArticleComponent = shallow( | ||
<DeleteArticle | ||
onClick={jest.fn()} | ||
/>, | ||
); | ||
test('renders the component', () => { | ||
expect(DeleteArticleComponent).toMatchSnapshot(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React from 'react'; | ||
import * as PropTypes from 'prop-types'; | ||
import './deleteArticle.scss'; | ||
|
||
const DeleteArticle = ({ onClick }) => ( | ||
<div> | ||
<button type="submit" onClick={onClick} className="btn btn-danger" id="delete-bt">DELETE ARTICLE</button> | ||
</div> | ||
); | ||
|
||
DeleteArticle.propTypes = { | ||
onClick: PropTypes.func.isRequired, | ||
}; | ||
|
||
export default DeleteArticle; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/containers/deleteArticle/__snapshots__/deleteArticle.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`<CreateArticlePage /> should render the component 1`] = ` | ||
<Connect(DeleteArticlePage) | ||
article={[MockFunction]} | ||
articleId={1} | ||
store={ | ||
Object { | ||
"clearActions": [Function], | ||
"dispatch": [Function], | ||
"getActions": [Function], | ||
"getState": [Function], | ||
"replaceReducer": [Function], | ||
"subscribe": [Function], | ||
} | ||
} | ||
> | ||
<DeleteArticlePage | ||
article={[MockFunction]} | ||
articleDelete={[Function]} | ||
articleId={1} | ||
deleteArticleReducer={ | ||
Object { | ||
"articleDelete": Object { | ||
"message": "Article deleted succesfully", | ||
}, | ||
} | ||
} | ||
store={ | ||
Object { | ||
"clearActions": [Function], | ||
"dispatch": [Function], | ||
"getActions": [Function], | ||
"getState": [Function], | ||
"replaceReducer": [Function], | ||
"subscribe": [Function], | ||
} | ||
} | ||
storeSubscription={ | ||
Subscription { | ||
"listeners": Object { | ||
"clear": [Function], | ||
"get": [Function], | ||
"notify": [Function], | ||
"subscribe": [Function], | ||
}, | ||
"onStateChange": [Function], | ||
"parentSub": undefined, | ||
"store": Object { | ||
"clearActions": [Function], | ||
"dispatch": [Function], | ||
"getActions": [Function], | ||
"getState": [Function], | ||
"replaceReducer": [Function], | ||
"subscribe": [Function], | ||
}, | ||
"unsubscribe": [Function], | ||
} | ||
} | ||
> | ||
<div> | ||
<DeleteArticle | ||
onClick={[Function]} | ||
> | ||
<div> | ||
<button | ||
className="btn btn-danger" | ||
id="delete-bt" | ||
onClick={[Function]} | ||
type="submit" | ||
> | ||
DELETE ARTICLE | ||
</button> | ||
</div> | ||
</DeleteArticle> | ||
</div> | ||
</DeleteArticlePage> | ||
</Connect(DeleteArticlePage)> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React from 'react'; | ||
import { mount, shallow } from 'enzyme'; | ||
import configureStore from 'redux-mock-store'; | ||
import thunk from 'redux-thunk'; | ||
import DeleteArticlePage, { mapDispatchToProps } from './index'; | ||
|
||
describe('<CreateArticlePage />', () => { | ||
let DeleteArticlePageComponent; | ||
let wrapper; | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
const middlewares = [thunk]; | ||
const mockStore = configureStore(middlewares); | ||
const initialState = { deleteArticleReducer: { articleDelete: { message: 'Article deleted succesfully' } } }; | ||
const store = mockStore(initialState); | ||
const props = { articleId: 1 }; | ||
DeleteArticlePageComponent = mount( | ||
<DeleteArticlePage article={jest.fn()} store={store} articleId={props.articleId} />, | ||
); | ||
wrapper = shallow( | ||
<DeleteArticlePage store={store} articleId={props.articleId} />, | ||
); | ||
}); | ||
|
||
it('should render the component', () => { | ||
expect(DeleteArticlePageComponent).toMatchSnapshot(); | ||
}); | ||
|
||
it('should dispatch a method to get user input', () => { | ||
const dispatch = jest.fn(); | ||
mapDispatchToProps(dispatch).articleDelete({}); | ||
}); | ||
|
||
test('Test function', () => { | ||
const event = { preventDefault: jest.fn() }; | ||
expect(wrapper.dive().instance().handleSubmit(event)); | ||
}); | ||
|
||
it('should test user data without fail', () => { | ||
DeleteArticlePageComponent | ||
.find('button') | ||
.simulate('submit'); | ||
DeleteArticlePageComponent | ||
.find('#delete-bt') | ||
.simulate('click'); | ||
}); | ||
}); |
Oops, something went wrong.