-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature: #162727528] add test for article bookmark
- Loading branch information
1 parent
98505e7
commit ae7dca2
Showing
1 changed file
with
180 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
import thunk from 'redux-thunk'; | ||
import configureStore from 'redux-mock-store'; | ||
import axios from 'axios'; | ||
import { Provider } from 'react-redux'; | ||
import ArticleContainer from './ArticleContainer'; | ||
import { actions, constants, bookmarkArticleReducer, types } from './duck'; | ||
|
||
jest.mock('axios'); | ||
|
||
const { | ||
setBookmarkArticleState, | ||
setBookmarkArticleError, | ||
setRemoveBookmarkError, | ||
} = actions; | ||
|
||
describe('Bookmark Action', () => { | ||
it('it should set Bookmark state', () => { | ||
const action = setBookmarkArticleState(constants.BOOKMARKING_ARTICLE); | ||
expect(action).toEqual({ | ||
type: types.SET_BOOKMARK_ARTICLE_STATE, | ||
bookmarkArticleState: constants.BOOKMARKING_ARTICLE, | ||
}); | ||
}); | ||
|
||
it('it should set bookmarkArticle error message', () => { | ||
const action = setBookmarkArticleError(constants.BOOKMARK_ARTICLE_ERROR); | ||
expect(action).toEqual({ | ||
type: types.SET_BOOKMARK_ARTICLE_ERROR, | ||
errorMessage: constants.BOOKMARK_ARTICLE_ERROR, | ||
}); | ||
}); | ||
|
||
it('it should set removeBookmark error message', () => { | ||
const action = setRemoveBookmarkError(constants.REMOVE_BOOKMARK_ERROR); | ||
expect(action).toEqual({ | ||
type: types.SET_REMOVE_BOOKMARK_ERROR, | ||
errorMessage: constants.REMOVE_BOOKMARK_ERROR, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('fetchArticleReducers', () => { | ||
it('should setup default state values', () => { | ||
const state = bookmarkArticleReducer(undefined, { | ||
type: '@@INIT', | ||
}); | ||
expect(state.bookmarkArticleStatus).toEqual({ | ||
bookmarkArticleState: '', | ||
errorMessage: '', | ||
}); | ||
}); | ||
|
||
it('it should change the bookmarkArticle state', () => { | ||
const action = { | ||
type: types.SET_BOOKMARK_ARTICLE_STATE, | ||
bookmarkArticleState: constants.BOOKMARKING_ARTICLE, | ||
}; | ||
const state = bookmarkArticleReducer(undefined, action); | ||
expect(state.bookmarkArticleState).toEqual(constants.BOOKMARKING_ARTICLE); | ||
}); | ||
|
||
it('it should change the bookmark article error message', () => { | ||
const action = { | ||
type: types.SET_BOOKMARK_ARTICLE_ERROR, | ||
errorMessage: 'failed to bookmark', | ||
}; | ||
const state = bookmarkArticleReducer(undefined, action); | ||
expect(state.errorMessage).toEqual(action.errorMessage); | ||
}); | ||
}); | ||
|
||
describe('bookmark article button', () => { | ||
const response = { response: { message: 'successfully fetched articles' } }; | ||
axios.get.mockImplementation(() => Promise.resolve(response)); | ||
axios.post.mockResolvedValue(response); | ||
const initialState = { | ||
createArticle: { | ||
singleFetchStatus: { | ||
status: constants.FETCH_SINGLE_SUCCESS, | ||
data: { | ||
createdAt: '2019-01-26 15:02:22.391+01', | ||
author: { | ||
username: 'spicy-dicy', | ||
}, | ||
comments: [ | ||
{ | ||
author: { | ||
username: 'rajeman', | ||
}, | ||
comment: { | ||
timestamp: 'comment text', | ||
}, | ||
id: 'commentid', | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
bookmarkArticle: { | ||
bookmarkArticleState: 'NOT_BOOKMARKED', | ||
}, | ||
fetchArticle: { | ||
articleData: [], | ||
}, | ||
}; | ||
const mockStore = configureStore([thunk]); | ||
const store = mockStore(() => initialState); | ||
let wrapper; | ||
beforeEach(() => { | ||
wrapper = mount( | ||
<Provider store={store}> | ||
<ArticleContainer | ||
match={{ params: { slug: 'article-001-spicydicy' } }} | ||
/> | ||
</Provider>, | ||
); | ||
}); | ||
|
||
it('should bookmark an article', () => { | ||
wrapper.find('#bookmarkBtn').simulate('click'); | ||
const storeActions = store.getActions(); | ||
expect(storeActions[3].type).toEqual('SET_BOOKMARK_ARTICLE_STATE'); | ||
}); | ||
}); | ||
|
||
describe('remove bookmark button', () => { | ||
const response = { response: { message: 'successfully fetched articles' } }; | ||
axios.get.mockImplementation(() => Promise.resolve(response)); | ||
axios.delete.mockResolvedValue(response); | ||
const initialState = { | ||
createArticle: { | ||
singleFetchStatus: { | ||
status: constants.FETCH_SINGLE_SUCCESS, | ||
data: { | ||
createdAt: '2019-01-26 15:02:22.391+01', | ||
author: { | ||
username: 'spicy-dicy', | ||
}, | ||
comments: [ | ||
{ | ||
author: { | ||
username: 'rajeman', | ||
}, | ||
comment: { | ||
timestamp: 'comment text', | ||
}, | ||
id: 'commentid', | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
bookmarkArticle: { | ||
bookmarkArticleState: 'BOOKMARKED', | ||
}, | ||
fetchArticle: { | ||
articleData: [], | ||
}, | ||
}; | ||
const mockStore = configureStore([thunk]); | ||
const store = mockStore(() => initialState); | ||
let wrapper; | ||
beforeEach(() => { | ||
wrapper = mount( | ||
<Provider store={store}> | ||
<ArticleContainer | ||
match={{ params: { slug: 'article-001-spicydicy' } }} | ||
/> | ||
</Provider>, | ||
); | ||
}); | ||
|
||
it('should unbookmark an article', () => { | ||
wrapper.find('#removeBookmarkBtn').simulate('click'); | ||
const storeActions = store.getActions(); | ||
expect(storeActions[3].type).toEqual('SET_BOOKMARK_ARTICLE_STATE'); | ||
}); | ||
}); |