2019-08-09 13:51:28 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
import * as sinon from 'sinon';
|
2019-09-09 11:33:39 +01:00
|
|
|
|
2019-09-26 14:36:12 +01:00
|
|
|
import Pagination from '@/components/common/VPagination.vue';
|
2019-08-09 13:51:28 +01:00
|
|
|
|
2019-09-09 11:33:39 +01:00
|
|
|
import { mount, shallowMount } from '@vue/test-utils';
|
|
|
|
|
2019-08-09 13:51:28 +01:00
|
|
|
describe('Pagination.vue', () => {
|
|
|
|
it('renders correctly', () => {
|
|
|
|
const wrapper = shallowMount(Pagination);
|
|
|
|
|
|
|
|
expect(wrapper).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders correctly with props', () => {
|
|
|
|
const wrapper = shallowMount(Pagination, {
|
|
|
|
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 () => {
|
2019-08-09 13:51:28 +01:00
|
|
|
const wrapper = mount(Pagination, {
|
|
|
|
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);
|
2019-12-27 16:41:43 +00: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', () => {
|
2019-08-09 13:51:28 +01:00
|
|
|
const wrapper = shallowMount(Pagination, {
|
|
|
|
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 () => {
|
|
|
|
const callbackSpy = sinon.stub();
|
|
|
|
|
|
|
|
const wrapper = mount(Pagination, {
|
|
|
|
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
|
|
|
|
2020-01-08 15:58:28 +00:00
|
|
|
await wrapper.findAll('span').at(2).trigger('click');
|
2019-08-09 13:51:28 +01:00
|
|
|
await expect(callbackSpy.callCount).toBe(1);
|
|
|
|
|
|
|
|
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 () => {
|
|
|
|
const callbackSpy = sinon.stub();
|
|
|
|
|
|
|
|
const wrapper = mount(Pagination, {
|
|
|
|
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;
|
|
|
|
|
|
|
|
wrapper.findAll('.pagination-container__button').at(1).trigger('click');
|
|
|
|
await expect(callbackSpy.callCount).toBe(1);
|
|
|
|
|
|
|
|
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 () => {
|
|
|
|
const callbackSpy = sinon.stub();
|
|
|
|
|
|
|
|
const wrapper = mount(Pagination, {
|
|
|
|
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;
|
|
|
|
|
|
|
|
wrapper.findAll('.pagination-container__button').at(0).trigger('click');
|
2019-12-27 16:41:43 +00:00
|
|
|
await expect(callbackSpy.callCount).toBe(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 () => {
|
|
|
|
const callbackSpy = sinon.stub();
|
|
|
|
|
|
|
|
const wrapper = mount(Pagination, {
|
|
|
|
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;
|
|
|
|
|
|
|
|
wrapper.findAll('.pagination-container__button').at(0).trigger('click');
|
|
|
|
await expect(callbackSpy.callCount).toBe(0);
|
|
|
|
|
|
|
|
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 () => {
|
|
|
|
const callbackSpy = sinon.stub();
|
|
|
|
|
|
|
|
const wrapper = mount(Pagination, {
|
|
|
|
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);
|
|
|
|
|
2019-08-09 13:51:28 +01:00
|
|
|
wrapper.findAll('.pagination-container__button').at(1).trigger('click');
|
2019-12-27 16:41:43 +00:00
|
|
|
await expect(callbackSpy.callCount).toBe(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 () => {
|
|
|
|
const wrapper = shallowMount(Pagination, {
|
|
|
|
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 () => {
|
|
|
|
const wrapper = shallowMount(Pagination, {
|
|
|
|
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);
|
|
|
|
|
2020-01-08 15:58:28 +00: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
|
|
|
});
|