diff --git a/components/pagination/__tests__/__snapshots__/pagination.test.tsx.snap b/components/pagination/__tests__/__snapshots__/pagination.test.tsx.snap new file mode 100644 index 0000000..142bc1f --- /dev/null +++ b/components/pagination/__tests__/__snapshots__/pagination.test.tsx.snap @@ -0,0 +1,189 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Pagination should render correctly 1`] = ` +"" +`; diff --git a/components/pagination/__tests__/pagination.test.tsx b/components/pagination/__tests__/pagination.test.tsx new file mode 100644 index 0000000..8bf9dfb --- /dev/null +++ b/components/pagination/__tests__/pagination.test.tsx @@ -0,0 +1,148 @@ +import React from 'react' +import { mount } from 'enzyme' +import { Pagination } from 'components' +import { act } from 'react-dom/test-utils' +import { updateWrapper } from 'tests/utils' + +describe('Pagination', () => { + it('should render correctly', () => { + const wrapper = mount() + expect(wrapper.html()).toMatchSnapshot() + expect(() => wrapper.unmount()).not.toThrow() + }) + + it('the specified page should be activated', async () => { + const wrapper = mount() + expect(wrapper.find('.active').text()).toEqual('2') + await act(async () => { + wrapper.setProps({ page: 10 }) + }) + await updateWrapper(wrapper, 200) + expect(wrapper.find('.active').text()).toEqual('10') + }) + + it('should trigger change event', async () => { + let current = 1 + const handler = jest.fn().mockImplementation(val => (current = val)) + const wrapper = mount() + + await act(async () => { + wrapper.setProps({ page: 10 }) + }) + await updateWrapper(wrapper, 200) + expect(handler).toHaveBeenCalled() + expect(current).toEqual(10) + + const btns = wrapper.find('button') + btns.at(0).simulate('click') + await updateWrapper(wrapper, 200) + expect(current).toEqual(9) + + btns.at(btns.length - 1).simulate('click') + btns.at(btns.length - 1).simulate('click') + btns.at(btns.length - 1).simulate('click') + btns.at(btns.length - 1).simulate('click') + await updateWrapper(wrapper, 200) + expect(current).toEqual(10) + handler.mockRestore() + }) + + it('the page should be rendered to follow the specified limit', async () => { + const wrapper = mount() + expect(wrapper.find('button').length).toBeGreaterThanOrEqual(20) + await act(async () => { + wrapper.setProps({ limit: 5 }) + }) + await updateWrapper(wrapper, 200) + expect(wrapper.find('button').length).toBeLessThanOrEqual(10) + }) + + it('should be render all pages when limit is greater than the total', async () => { + const handler = jest.fn() + const wrapper = mount() + expect(wrapper.find('button').length).toBeGreaterThanOrEqual(15) + wrapper.find('button').at(10).simulate('click') + await updateWrapper(wrapper, 200) + + expect(handler).toHaveBeenCalled() + handler.mockRestore() + }) + + it('omit pages by limit value', async () => { + const wrapper = mount() + const btn4 = wrapper.find('button').at(4) + expect(btn4.text()).toEqual('4') + btn4.simulate('click') + await updateWrapper(wrapper, 200) + let btns = wrapper.find('button').map(btn => btn.text()) + expect(btns.includes('2')).not.toBeTruthy() + expect(btns.includes('1')).toBeTruthy() + expect(btns.includes('3')).toBeTruthy() + expect(btns.includes('4')).toBeTruthy() + expect(btns.includes('5')).toBeTruthy() + expect(btns.includes('6')).not.toBeTruthy() + expect(btns.includes('20')).toBeTruthy() + + const btn5 = wrapper.find('button').at(5) + expect(btn5.text()).toEqual('5') + btn5.simulate('click') + await updateWrapper(wrapper, 200) + btns = wrapper.find('button').map(btn => btn.text()) + expect(btns.includes('1')).toBeTruthy() + expect(btns.includes('2')).not.toBeTruthy() + expect(btns.includes('3')).not.toBeTruthy() + expect(btns.includes('4')).toBeTruthy() + expect(btns.includes('5')).toBeTruthy() + expect(btns.includes('6')).toBeTruthy() + expect(btns.includes('7')).not.toBeTruthy() + expect(btns.includes('8')).not.toBeTruthy() + expect(btns.includes('20')).toBeTruthy() + }) + + it('should trigger change event when ellipsis clicked', async () => { + let current = 20 + const handler = jest.fn().mockImplementation(val => (current = val)) + const wrapper = mount() + const btn = wrapper.find('svg').at(0).parents('button') + btn.at(0).simulate('click') + await updateWrapper(wrapper, 200) + expect(handler).toHaveBeenCalled() + expect(current).toEqual(15) + + await act(async () => { + wrapper.setProps({ page: 1 }) + }) + await updateWrapper(wrapper, 200) + const lastBtn = wrapper.find('svg').at(0).parents('button') + lastBtn.at(0).simulate('click') + await updateWrapper(wrapper, 200) + expect(current).toEqual(1 + 5) + }) + + it('another SVG should be displayed when the mouse is moved in', async () => { + const wrapper = mount() + const svg = wrapper.find('svg').at(0) + const btn = svg.parents('button') + + const html = svg.html() + btn.simulate('mouseEnter') + await updateWrapper(wrapper) + expect(html).not.toEqual(wrapper.find('svg').at(0).html()) + + btn.simulate('mouseLeave') + await updateWrapper(wrapper) + expect(html).toEqual(wrapper.find('svg').at(0).html()) + }) + + it('custom buttons should be display', () => { + const wrapper = mount( + + custom-prev + custom-next + , + ) + const btns = wrapper.find('button') + expect(btns.at(0).text()).toEqual('custom-prev') + expect(btns.at(btns.length - 1).text()).toEqual('custom-next') + }) +}) diff --git a/components/pagination/pagination-item.tsx b/components/pagination/pagination-item.tsx index e4b7283..74824d7 100644 --- a/components/pagination/pagination-item.tsx +++ b/components/pagination/pagination-item.tsx @@ -1,6 +1,6 @@ import React, { useMemo } from 'react' import useTheme from '../styles/use-theme' -import { addColorAlpha } from 'components/utils/color' +import { addColorAlpha } from '../utils/color' interface Props { active?: boolean diff --git a/components/pagination/pagination-pages.tsx b/components/pagination/pagination-pages.tsx index 50a24a1..a90f58f 100644 --- a/components/pagination/pagination-pages.tsx +++ b/components/pagination/pagination-pages.tsx @@ -1,6 +1,6 @@ import React, { Dispatch, SetStateAction, useCallback, useMemo } from 'react' import PaginationItem from './pagination-item' -import PaginationEllipsis from 'components/pagination/pagination-ellipsis' +import PaginationEllipsis from './pagination-ellipsis' interface Props { limit: number