-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pagination): add article pagination
- add article pagination - Revise article like count prop validation from array to object since it has been modified in the back end [starts #165273536]
- Loading branch information
Arthur Kalule
committed
Jun 20, 2019
1 parent
da27459
commit 3c51443
Showing
11 changed files
with
270 additions
and
11 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,6 @@ | ||
.article-pagination { | ||
display: flex; | ||
flex-direction: row; | ||
justify-content: space-around; | ||
margin: 1rem; | ||
} |
49 changes: 49 additions & 0 deletions
49
src/components/ArticlePagination/ArticlePagination.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,49 @@ | ||
import React from 'react'; | ||
|
||
import { mount } from 'enzyme'; | ||
|
||
import ArticlePagination from 'components/ArticlePagination'; | ||
import Button from 'components/Button'; | ||
|
||
describe('ArticlePagination', () => { | ||
const props = { | ||
previous: null, | ||
next: 'next', | ||
paginate: jest.fn(), | ||
count: 20, | ||
}; | ||
let wrapper = mount(<ArticlePagination {...props} />); | ||
|
||
it('should render without crushing', () => { | ||
expect(wrapper).toMatchSnapshot(); | ||
}); | ||
|
||
const previousButton = wrapper.find(Button).at(0); | ||
const nextButton = wrapper.find(Button).at(1); | ||
|
||
it('should disable buttons if null', () => { | ||
expect(previousButton.props().disabled).toBe(true); | ||
expect(nextButton.props().disabled).toBe(false); | ||
}); | ||
|
||
|
||
it('should call the paginate function on button click', () => { | ||
const event = { | ||
target: { | ||
type: 'button', | ||
name: '', | ||
}, | ||
preventDefault: jest.fn(), | ||
}; | ||
|
||
nextButton.simulate('click', event); | ||
expect(props.paginate).toBeCalled(); | ||
previousButton.simulate('click', event); | ||
|
||
|
||
props.previous = 'previous'; | ||
wrapper = mount(<ArticlePagination {...props} />); | ||
wrapper.find(Button).at(0).simulate('click', event); | ||
expect(props.paginate).toBeCalled(); | ||
}); | ||
}); |
50 changes: 50 additions & 0 deletions
50
src/components/ArticlePagination/__snapshots__/ArticlePagination.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,50 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`ArticlePagination should render without crushing 1`] = ` | ||
<ArticlePagination | ||
count={20} | ||
next="next" | ||
paginate={[MockFunction]} | ||
previous={null} | ||
> | ||
<div | ||
className="article-pagination" | ||
> | ||
<Button | ||
btnClass="fa fa-backward" | ||
btnEvent={[Function]} | ||
btnName="" | ||
disabled={true} | ||
> | ||
<button | ||
className="fa fa-backward" | ||
disabled={true} | ||
onClick={[Function]} | ||
type="button" | ||
/> | ||
</Button> | ||
<span> | ||
1 | ||
/ | ||
2 | ||
pages | ||
</span> | ||
<Button | ||
btnClass="fa fa-forward" | ||
btnEvent={[Function]} | ||
btnName="" | ||
disabled={false} | ||
> | ||
<button | ||
className="fa fa-forward" | ||
disabled={false} | ||
onClick={[Function]} | ||
type="button" | ||
/> | ||
</Button> | ||
</div> | ||
</ArticlePagination> | ||
`; |
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,65 @@ | ||
// react | ||
import React from 'react'; | ||
|
||
// Proptypes for validation | ||
import PropTypes from 'prop-types'; | ||
|
||
// stylesheets | ||
import './ArticlePagination.scss'; | ||
|
||
// Button component | ||
import Button from 'components/Button'; | ||
|
||
const ArticlePagination = ({ | ||
previous, next, paginate, count, page | ||
}) => { | ||
const pages = Math.ceil(count / 10); | ||
return ( | ||
|
||
<div className="article-pagination"> | ||
<Button | ||
btnName="" | ||
btnClass="fa fa-backward" | ||
btnEvent={(e) => { | ||
e.preventDefault(); | ||
paginate(previous, -1); | ||
}} | ||
disabled={!previous} | ||
/> | ||
<span> | ||
{page || 1} | ||
{' '} | ||
{'/'} | ||
{' '} | ||
{pages} | ||
{' '} | ||
{'pages '} | ||
</span> | ||
<Button | ||
btnName="" | ||
btnClass="fa fa-forward" | ||
btnEvent={(e) => { | ||
e.preventDefault(); | ||
paginate(next, 1); | ||
}} | ||
disabled={!next} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
ArticlePagination.defaultProps = { | ||
next: null, | ||
previous: null, | ||
}; | ||
|
||
ArticlePagination.propTypes = { | ||
next: PropTypes.string, | ||
page: PropTypes.number.isRequired, | ||
count: PropTypes.number.isRequired, | ||
previous: PropTypes.string, | ||
paginate: PropTypes.func.isRequired, | ||
}; | ||
|
||
|
||
export default ArticlePagination; |
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
Oops, something went wrong.