|
| 1 | +import React from 'react'; |
| 2 | +import { mount } from 'enzyme'; |
| 3 | +import Pagination from '../src'; |
| 4 | + |
| 5 | +describe('Uncontrolled Pagination', () => { |
| 6 | + let wrapper; |
| 7 | + const onChange = jest.fn(); |
| 8 | + |
| 9 | + function shouldHighlightRight() { |
| 10 | + const pagers = wrapper.find('li:not(.rc-pagination-total-text)'); |
| 11 | + pagers.forEach((pager, index) => { |
| 12 | + // page starts from 1 |
| 13 | + if (index === wrapper.state().current) { |
| 14 | + expect(pager.hasClass('rc-pagination-item-active')).toBe(true); |
| 15 | + } else { |
| 16 | + expect(pager.hasClass('rc-pagination-item-active')).toBe(false); |
| 17 | + } |
| 18 | + }); |
| 19 | + } |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + wrapper = mount( |
| 23 | + <Pagination |
| 24 | + defaultCurrent={1} |
| 25 | + onChange={onChange} |
| 26 | + total={25} |
| 27 | + showQuickJumper={{ goButton: true }} |
| 28 | + showTotal={(total, range) => |
| 29 | + `${range[0]} - ${range[1]} of ${total} items` |
| 30 | + } |
| 31 | + />, |
| 32 | + ); |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(() => { |
| 36 | + wrapper.unmount(); |
| 37 | + onChange.mockReset(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('default current page is 1', () => { |
| 41 | + expect(wrapper.state().current).toBe(1); |
| 42 | + }); |
| 43 | + |
| 44 | + it('prev-button should be disabled', () => { |
| 45 | + const prevButton = wrapper.find('.rc-pagination-prev'); |
| 46 | + expect(prevButton.hasClass('rc-pagination-disabled')).toBe(true); |
| 47 | + expect(prevButton.getDOMNode().getAttribute('aria-disabled')).toBe('true'); |
| 48 | + }); |
| 49 | + |
| 50 | + it( |
| 51 | + 'should hightlight current page and not highlight other page', |
| 52 | + shouldHighlightRight, |
| 53 | + ); |
| 54 | + |
| 55 | + it('should calc page right', () => { |
| 56 | + const pagers = wrapper.find( |
| 57 | + 'li:not(.rc-pagination-total-text):not(.rc-pagination-options)', |
| 58 | + ); |
| 59 | + const knownPageCount = 3; |
| 60 | + const buttonLength = 2; |
| 61 | + expect(pagers.length).toBe(knownPageCount + buttonLength); |
| 62 | + }); |
| 63 | + |
| 64 | + it('next button should not be disabled', () => { |
| 65 | + const nextButton = wrapper.find('.rc-pagination-next'); |
| 66 | + expect(nextButton.hasClass('rc-pagination-disabled')).toBe(false); |
| 67 | + expect(nextButton.getDOMNode().getAttribute('aria-disabled')).toBe('false'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should response mouse click right', () => { |
| 71 | + const pagers = wrapper.find('.rc-pagination-item'); |
| 72 | + expect(pagers.length).toBe(3); |
| 73 | + const page2 = pagers.at(1); |
| 74 | + expect(page2.hasClass('rc-pagination-item-2')).toBe(true); |
| 75 | + page2.simulate('click'); |
| 76 | + expect(wrapper.state().current).toBe(2); |
| 77 | + expect(onChange).toHaveBeenLastCalledWith(2, 10); |
| 78 | + shouldHighlightRight(); |
| 79 | + }); |
| 80 | +}); |
0 commit comments