Skip to content

Commit

Permalink
Show current page number in Pagination component
Browse files Browse the repository at this point in the history
  • Loading branch information
taehwanno committed Jan 21, 2018
1 parent 4653e88 commit e8cdbe0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
20 changes: 16 additions & 4 deletions app/components/Pagination/Pagination.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ describe('<Pagination />', () => {
expect(wrapper).toMatchSnapshot();
});

it('should have .Pagination__button--disabled when props.currentPage === 1', () => {
const wrapper = shallow(<Pagination currentPage={1} />);
const button = wrapper.find('.Pagination__button--disabled');
expect(button.props()['data-button']).toBe('prev');
});

it('should have .Pagination__button--disabled when props.currentPage === 10', () => {
const wrapper = shallow(<Pagination currentPage={10} />);
const button = wrapper.find('.Pagination__button--disabled');
expect(button.props()['data-button']).toBe('next');
});

it('should calls onPaginate with currentPage - 1 when simulate prev button click event', () => {
const currentPage = 3;
const onPaginate = jest.fn();
Expand All @@ -20,8 +32,8 @@ describe('<Pagination />', () => {
/>
));
wrapper.find('.Pagination__button').at(0).simulate('click');
expect(onPaginate.mock.calls.length).toBe(1);
expect(onPaginate.mock.calls[0]).toEqual([currentPage - 1]);
expect(onPaginate).toHaveBeenCalledTimes(1);
expect(onPaginate).toHaveBeenCalledWith(currentPage - 1);
});

it('should calls onPaginate with currentPage + 1 when simulate next button click event', () => {
Expand All @@ -35,7 +47,7 @@ describe('<Pagination />', () => {
/>
));
wrapper.find('.Pagination__button').at(1).simulate('click');
expect(onPaginate.mock.calls.length).toBe(1);
expect(onPaginate.mock.calls[0]).toEqual([currentPage + 1]);
expect(onPaginate).toHaveBeenCalledTimes(1);
expect(onPaginate).toHaveBeenCalledWith(currentPage + 1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@ exports[`<Pagination /> should match snapshot when render default 1`] = `
>
<button
className="Pagination__button Pagination__button--disabled"
data-button="prev"
disabled={true}
onClick={[Function]}
type="button"
>
&lt;
Prev
</button>
1
/ 10
<button
className="Pagination__button"
data-button="next"
disabled={false}
onClick={[Function]}
type="button"
>
Expand Down
11 changes: 10 additions & 1 deletion app/components/Pagination/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,34 @@ const defaultProps = {

function Pagination({ currentPage, onPaginate }) {
const prevDisabled = currentPage === 1;
const nextDisabled = currentPage === 10;

const prevButtonClassName = cx('Pagination__button', {
'Pagination__button--disabled': prevDisabled,
});

const nextButtonClassName = cx('Pagination__button', {
'Pagination__button--disabled': nextDisabled,
});

return (
<div className="Pagination">
<div className="Pagination__inner">
<button
className={prevButtonClassName}
data-button="prev"
disabled={prevDisabled}
type="button"
onClick={() => onPaginate(currentPage - 1)}
>
{'<'} Prev
</button>
{currentPage} / 10
{' '}
<button
className="Pagination__button"
className={nextButtonClassName}
data-button="next"
disabled={nextDisabled}
type="button"
onClick={() => onPaginate(currentPage + 1)}
>
Expand Down

0 comments on commit e8cdbe0

Please sign in to comment.