2019-08-09 13:51:28 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-09-09 11:33:39 +01:00
|
|
|
import { mount, shallowMount } from '@vue/test-utils';
|
|
|
|
|
2022-09-08 15:11:09 +01:00
|
|
|
import Pagination from '@/components/common/VPagination.vue';
|
|
|
|
|
2019-08-09 13:51:28 +01:00
|
|
|
describe('Pagination.vue', () => {
|
|
|
|
it('renders correctly', () => {
|
2023-05-16 13:09:46 +01:00
|
|
|
const wrapper = shallowMount(Pagination);
|
2019-08-09 13:51:28 +01:00
|
|
|
|
|
|
|
expect(wrapper).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders correctly with props', () => {
|
2023-05-16 13:09:46 +01:00
|
|
|
const wrapper = shallowMount(Pagination, {
|
2019-08-09 13:51:28 +01:00
|
|
|
propsData: {
|
|
|
|
totalPageCount: 10,
|
2019-09-13 15:58:18 +01:00
|
|
|
onPageClickCallback: () => new Promise(() => false),
|
2019-08-09 13:51:28 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(wrapper).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2020-01-08 15:58:28 +00:00
|
|
|
it('inits correctly with totalPageCount equals 10 and current pageNumber in first block', async () => {
|
2023-05-16 13:09:46 +01:00
|
|
|
const wrapper = mount(Pagination, {
|
2019-08-09 13:51:28 +01:00
|
|
|
propsData: {
|
|
|
|
totalPageCount: 10,
|
2019-09-13 15:58:18 +01:00
|
|
|
onPageClickCallback: () => new Promise(() => false),
|
2019-08-09 13:51:28 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-01-08 15:58:28 +00:00
|
|
|
const wrapperData = await wrapper.vm.$data;
|
2019-08-09 13:51:28 +01:00
|
|
|
|
2019-12-27 16:41:43 +00:00
|
|
|
expect(wrapperData.currentPageNumber).toBe(1);
|
2019-08-09 13:51:28 +01:00
|
|
|
expect(wrapperData.pagesArray.length).toBe(10);
|
|
|
|
expect(wrapperData.firstBlockPages.length).toBe(3);
|
|
|
|
expect(wrapperData.middleBlockPages.length).toBe(0);
|
|
|
|
expect(wrapperData.lastBlockPages.length).toBe(1);
|
2023-05-16 13:09:46 +01:00
|
|
|
expect(wrapper.findAll('span').at(0)?.classes().includes('selected')).toBe(true);
|
2019-08-09 13:51:28 +01:00
|
|
|
});
|
|
|
|
|
2019-12-27 16:41:43 +00:00
|
|
|
it('inits correctly with totalPageCount equals 4', () => {
|
2023-05-16 13:09:46 +01:00
|
|
|
const wrapper = shallowMount(Pagination, {
|
2019-08-09 13:51:28 +01:00
|
|
|
propsData: {
|
|
|
|
totalPageCount: 4,
|
2019-09-13 15:58:18 +01:00
|
|
|
onPageClickCallback: () => new Promise(() => false),
|
2019-08-09 13:51:28 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const wrapperData = wrapper.vm.$data;
|
|
|
|
|
|
|
|
expect(wrapperData.currentPageNumber).toBe(1);
|
|
|
|
expect(wrapperData.pagesArray.length).toBe(4);
|
|
|
|
expect(wrapperData.firstBlockPages.length).toBe(4);
|
|
|
|
expect(wrapperData.middleBlockPages.length).toBe(0);
|
|
|
|
expect(wrapperData.lastBlockPages.length).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('behaves correctly on page click', async () => {
|
2022-04-07 12:04:24 +01:00
|
|
|
const callbackSpy = jest.fn();
|
2019-08-09 13:51:28 +01:00
|
|
|
|
2023-05-16 13:09:46 +01:00
|
|
|
const wrapper = mount(Pagination, {
|
2019-08-09 13:51:28 +01:00
|
|
|
propsData: {
|
|
|
|
totalPageCount: 9,
|
2019-09-13 15:58:18 +01:00
|
|
|
onPageClickCallback: callbackSpy,
|
2019-08-09 13:51:28 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-01-08 15:58:28 +00:00
|
|
|
const wrapperData = await wrapper.vm.$data;
|
2019-08-09 13:51:28 +01:00
|
|
|
|
2023-05-16 13:09:46 +01:00
|
|
|
await wrapper.findAll('span').at(2)?.trigger('click');
|
2022-04-07 12:04:24 +01:00
|
|
|
await expect(callbackSpy).toHaveBeenCalledTimes(1);
|
2019-08-09 13:51:28 +01:00
|
|
|
|
|
|
|
expect(wrapperData.currentPageNumber).toBe(3);
|
|
|
|
expect(wrapperData.firstBlockPages.length).toBe(1);
|
|
|
|
expect(wrapperData.middleBlockPages.length).toBe(3);
|
|
|
|
expect(wrapperData.lastBlockPages.length).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('behaves correctly on next page button click', async () => {
|
2022-04-07 12:04:24 +01:00
|
|
|
const callbackSpy = jest.fn();
|
2019-08-09 13:51:28 +01:00
|
|
|
|
2023-05-16 13:09:46 +01:00
|
|
|
const wrapper = mount(Pagination, {
|
2019-08-09 13:51:28 +01:00
|
|
|
propsData: {
|
|
|
|
totalPageCount: 9,
|
2019-09-13 15:58:18 +01:00
|
|
|
onPageClickCallback: callbackSpy,
|
2019-08-09 13:51:28 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const wrapperData = wrapper.vm.$data;
|
|
|
|
|
2023-05-16 13:09:46 +01:00
|
|
|
wrapper.findAll('.pagination-container__button').at(1)?.trigger('click');
|
2022-04-07 12:04:24 +01:00
|
|
|
await expect(callbackSpy).toHaveBeenCalledTimes(1);
|
2019-08-09 13:51:28 +01:00
|
|
|
|
|
|
|
expect(wrapperData.currentPageNumber).toBe(2);
|
|
|
|
expect(wrapperData.firstBlockPages.length).toBe(3);
|
|
|
|
expect(wrapperData.middleBlockPages.length).toBe(0);
|
|
|
|
expect(wrapperData.lastBlockPages.length).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('behaves correctly on previous page button click', async () => {
|
2022-04-07 12:04:24 +01:00
|
|
|
const callbackSpy = jest.fn();
|
2019-08-09 13:51:28 +01:00
|
|
|
|
2023-05-16 13:09:46 +01:00
|
|
|
const wrapper = mount(Pagination, {
|
2019-08-09 13:51:28 +01:00
|
|
|
propsData: {
|
|
|
|
totalPageCount: 9,
|
2019-09-13 15:58:18 +01:00
|
|
|
onPageClickCallback: callbackSpy,
|
2019-08-09 13:51:28 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2019-12-27 16:41:43 +00:00
|
|
|
await wrapper.vm.onPageClick(8);
|
|
|
|
|
2019-08-09 13:51:28 +01:00
|
|
|
const wrapperData = wrapper.vm.$data;
|
|
|
|
|
2023-05-16 13:09:46 +01:00
|
|
|
wrapper.findAll('.pagination-container__button').at(0)?.trigger('click');
|
2022-04-07 12:04:24 +01:00
|
|
|
await expect(callbackSpy).toHaveBeenCalledTimes(2);
|
2019-08-09 13:51:28 +01:00
|
|
|
|
|
|
|
expect(wrapperData.currentPageNumber).toBe(7);
|
|
|
|
expect(wrapperData.firstBlockPages.length).toBe(1);
|
|
|
|
expect(wrapperData.middleBlockPages.length).toBe(3);
|
|
|
|
expect(wrapperData.lastBlockPages.length).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('behaves correctly on previous page button click when current is 1', async () => {
|
2022-04-07 12:04:24 +01:00
|
|
|
const callbackSpy = jest.fn();
|
2019-08-09 13:51:28 +01:00
|
|
|
|
2023-05-16 13:09:46 +01:00
|
|
|
const wrapper = mount(Pagination, {
|
2019-08-09 13:51:28 +01:00
|
|
|
propsData: {
|
|
|
|
totalPageCount: 9,
|
2019-09-13 15:58:18 +01:00
|
|
|
onPageClickCallback: callbackSpy,
|
2019-08-09 13:51:28 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const wrapperData = wrapper.vm.$data;
|
|
|
|
|
2023-05-16 13:09:46 +01:00
|
|
|
wrapper.findAll('.pagination-container__button').at(0)?.trigger('click');
|
2022-04-07 12:04:24 +01:00
|
|
|
await expect(callbackSpy).toHaveBeenCalledTimes(0);
|
2019-08-09 13:51:28 +01:00
|
|
|
|
|
|
|
expect(wrapperData.currentPageNumber).toBe(1);
|
|
|
|
expect(wrapperData.firstBlockPages.length).toBe(3);
|
|
|
|
expect(wrapperData.middleBlockPages.length).toBe(0);
|
|
|
|
expect(wrapperData.lastBlockPages.length).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('behaves correctly on next page button click when current is last', async () => {
|
2022-04-07 12:04:24 +01:00
|
|
|
const callbackSpy = jest.fn();
|
2019-08-09 13:51:28 +01:00
|
|
|
|
2023-05-16 13:09:46 +01:00
|
|
|
const wrapper = mount(Pagination, {
|
2019-08-09 13:51:28 +01:00
|
|
|
propsData: {
|
|
|
|
totalPageCount: 9,
|
2019-09-13 15:58:18 +01:00
|
|
|
onPageClickCallback: callbackSpy,
|
2019-08-09 13:51:28 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const wrapperData = wrapper.vm.$data;
|
|
|
|
|
2019-12-27 16:41:43 +00:00
|
|
|
await wrapper.vm.onPageClick(9);
|
|
|
|
|
2023-05-16 13:09:46 +01:00
|
|
|
wrapper.findAll('.pagination-container__button').at(1)?.trigger('click');
|
2022-04-07 12:04:24 +01:00
|
|
|
await expect(callbackSpy).toHaveBeenCalledTimes(1);
|
2019-08-09 13:51:28 +01:00
|
|
|
|
|
|
|
expect(wrapperData.currentPageNumber).toBe(9);
|
|
|
|
expect(wrapperData.firstBlockPages.length).toBe(1);
|
|
|
|
expect(wrapperData.middleBlockPages.length).toBe(0);
|
|
|
|
expect(wrapperData.lastBlockPages.length).toBe(3);
|
|
|
|
});
|
2019-08-19 12:20:38 +01:00
|
|
|
|
|
|
|
it('should reset current page index to 1', async () => {
|
2023-05-16 13:09:46 +01:00
|
|
|
const wrapper = shallowMount(Pagination, {
|
2019-08-19 12:20:38 +01:00
|
|
|
propsData: {
|
|
|
|
totalPageCount: 4,
|
2019-09-13 15:58:18 +01:00
|
|
|
onPageClickCallback: () => Promise.resolve({}),
|
2019-08-19 12:20:38 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await wrapper.vm.nextPage();
|
|
|
|
|
|
|
|
expect(wrapper.vm.$data.currentPageNumber).toBe(2);
|
|
|
|
|
|
|
|
wrapper.vm.resetPageIndex();
|
|
|
|
|
|
|
|
const wrapperData = wrapper.vm.$data;
|
|
|
|
|
|
|
|
expect(wrapperData.currentPageNumber).toBe(1);
|
|
|
|
expect(wrapperData.pagesArray.length).toBe(4);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should completely reinitialize Pagination on totalPageCount change', async () => {
|
2023-05-16 13:09:46 +01:00
|
|
|
const wrapper = shallowMount(Pagination, {
|
2019-08-19 12:20:38 +01:00
|
|
|
propsData: {
|
|
|
|
totalPageCount: 4,
|
2019-09-13 15:58:18 +01:00
|
|
|
onPageClickCallback: () => Promise.resolve({}),
|
2019-08-19 12:20:38 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await wrapper.vm.nextPage();
|
|
|
|
|
|
|
|
expect(wrapper.vm.$data.currentPageNumber).toBe(2);
|
|
|
|
|
2022-09-08 15:11:09 +01:00
|
|
|
await wrapper.setProps({ totalPageCount: 7 });
|
2019-08-19 12:20:38 +01:00
|
|
|
|
|
|
|
const wrapperData = wrapper.vm.$data;
|
|
|
|
|
|
|
|
expect(wrapperData.currentPageNumber).toBe(1);
|
|
|
|
expect(wrapperData.pagesArray.length).toBe(7);
|
|
|
|
});
|
2019-08-09 13:51:28 +01:00
|
|
|
});
|