-
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.
- Loading branch information
Showing
12 changed files
with
211 additions
and
52 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,34 @@ | ||
import React from 'react'; | ||
import { Provider } from 'react-redux'; | ||
import { MemoryRouter } from 'react-router-dom'; | ||
import { mockStore, initialState } from '../../../__mocks__/store'; | ||
import { | ||
Pagination as PaginationComponent, | ||
mapStateToProps | ||
} from '../../../components/Articles/Pagination/Pagination'; | ||
import { shallow, mount } from '../../../../config/enzymeConfig'; | ||
|
||
describe('<Pagination />', () => { | ||
const state = { | ||
buttons: ['button'], | ||
pageNumber: 1, | ||
getAllArticles: jest.fn(), | ||
displayButtons: jest.fn(), | ||
paginateArticles: jest.fn() | ||
}; | ||
const component = shallow(<PaginationComponent {...state} />); | ||
it('should create <PaginationComponent /> snapshot ', () => { | ||
expect(component).toMatchSnapshot(); | ||
}); | ||
it('should trigger submit button ', () => { | ||
component.instance().displayButtons(40); | ||
}); | ||
it('should trigger close message ', () => { | ||
component.setProps({ limit: 1, offset: 10, label: 1 }); | ||
component | ||
.find('.pagination button') | ||
.at(0) | ||
.simulate('click'); | ||
component.instance().paginateArticles({ limit: 1, offset: 10, label: 1 }); | ||
}); | ||
}); |
3 changes: 3 additions & 0 deletions
3
src/__tests__/components/Articles/__snapshots__/Pagination.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,3 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`<Pagination /> should create <PaginationComponent /> snapshot 1`] = `ShallowWrapper {}`; |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,77 @@ | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { connect } from 'react-redux'; | ||
import { getAllArticles } from '../../../actions'; | ||
import './Pagination.scss'; | ||
|
||
export class Pagination extends Component { | ||
state = { buttons: [], pageNumber: 1, articlesCount: 0 }; | ||
|
||
displayButtons = (articlesCount) => { | ||
const maxPerPage = 10; | ||
const numberOfButtons = (articlesCount % maxPerPage) + articlesCount / maxPerPage; | ||
let buttons = []; | ||
for (let i = 1; i <= numberOfButtons; i += 1) { | ||
buttons = [ | ||
...buttons, | ||
{ | ||
offset: i === 1 ? 0 : i * 10 - 10, | ||
limit: 10, | ||
label: i | ||
} | ||
]; | ||
} | ||
this.setState(prevState => ({ ...prevState, buttons })); | ||
return buttons; | ||
}; | ||
|
||
componentWillReceiveProps = (nextProps) => { | ||
const { articlesCount } = nextProps; | ||
return articlesCount && this.displayButtons(articlesCount); | ||
}; | ||
|
||
componentDidMount = () => { | ||
const { articlesCount } = this.props; | ||
return articlesCount && this.displayButtons(articlesCount); | ||
}; | ||
|
||
paginateArticles = ({ offset, limit, label }) => { | ||
const { getAllArticles } = this.props; | ||
this.setState(prevState => ({ | ||
...prevState, | ||
pageNumber: label | ||
})); | ||
return getAllArticles(offset, limit); | ||
}; | ||
|
||
render() { | ||
const { buttons, pageNumber } = this.state; | ||
return ( | ||
<div className="row"> | ||
<ul className="list-inline pagination"> | ||
{buttons.map((button, key) => ( | ||
<li key={key}> | ||
<button | ||
className={`${pageNumber === button.label ? 'yellow' : 'light bold'}`} | ||
onClick={() => this.paginateArticles(button)} | ||
> | ||
{button.label} | ||
</button> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
Pagination.propTypes = { | ||
articlesCount: PropTypes.any, | ||
getAllArticles: PropTypes.func | ||
}; | ||
export const mapStateToProps = ({ articles: { articlesCount } }) => ({ articlesCount }); | ||
|
||
export default connect( | ||
mapStateToProps, | ||
{ getAllArticles } | ||
)(Pagination); |
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,16 @@ | ||
@import '../../../assets/css/_colors'; | ||
@import '../../../assets/css/_radius'; | ||
|
||
.pagination { | ||
margin: 10px 0; | ||
ul li { | ||
background: none !important; | ||
button { | ||
padding: 8px 20px; | ||
border: none; | ||
margin-right: 5px; | ||
cursor: pointer; | ||
@extend .radius-4; | ||
} | ||
} | ||
} |
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,3 @@ | ||
import Pagination from './Pagination'; | ||
|
||
export default Pagination; |
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