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-08-09 13:51:28 +01:00
|
|
|
import Pagination from '@/components/common/Pagination.vue';
|
|
|
|
|
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
|
|
|
},
|
|
|
|
mocks: {
|
|
|
|
$route: {
|
|
|
|
query: {
|
2019-09-13 15:58:18 +01:00
|
|
|
pageNumber: 2,
|
|
|
|
},
|
2019-08-09 13:51:28 +01:00
|
|
|
},
|
|
|
|
$router: {
|
2019-09-13 15:58:18 +01:00
|
|
|
replace: () => false,
|
|
|
|
},
|
|
|
|
},
|
2019-08-09 13:51:28 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(wrapper).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('inits correctly with totalPageCount equals 10 and current pageNumber in first block', () => {
|
|
|
|
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
|
|
|
},
|
|
|
|
mocks: {
|
|
|
|
$route: {
|
|
|
|
query: {
|
2019-09-13 15:58:18 +01:00
|
|
|
pageNumber: 2,
|
|
|
|
},
|
2019-08-09 13:51:28 +01:00
|
|
|
},
|
|
|
|
$router: {
|
2019-09-13 15:58:18 +01:00
|
|
|
replace: () => false,
|
|
|
|
},
|
|
|
|
},
|
2019-08-09 13:51:28 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const wrapperData = wrapper.vm.$data;
|
|
|
|
|
|
|
|
expect(wrapperData.currentPageNumber).toBe(2);
|
|
|
|
expect(wrapperData.pagesArray.length).toBe(10);
|
|
|
|
expect(wrapperData.firstBlockPages.length).toBe(3);
|
|
|
|
expect(wrapperData.middleBlockPages.length).toBe(0);
|
|
|
|
expect(wrapperData.lastBlockPages.length).toBe(1);
|
|
|
|
expect(wrapper.findAll('span').at(1).classes().includes('selected')).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('inits correctly with totalPageCount equals 10 and current pageNumber in middle block', () => {
|
|
|
|
const wrapper = shallowMount(Pagination, {
|
|
|
|
propsData: {
|
|
|
|
totalPageCount: 12,
|
2019-09-13 15:58:18 +01:00
|
|
|
onPageClickCallback: () => new Promise(() => false),
|
2019-08-09 13:51:28 +01:00
|
|
|
},
|
|
|
|
mocks: {
|
|
|
|
$route: {
|
|
|
|
query: {
|
2019-09-13 15:58:18 +01:00
|
|
|
pageNumber: 5,
|
|
|
|
},
|
2019-08-09 13:51:28 +01:00
|
|
|
},
|
|
|
|
$router: {
|
2019-09-13 15:58:18 +01:00
|
|
|
replace: () => false,
|
|
|
|
},
|
|
|
|
},
|
2019-08-09 13:51:28 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const wrapperData = wrapper.vm.$data;
|
|
|
|
|
|
|
|
expect(wrapperData.currentPageNumber).toBe(5);
|
|
|
|
expect(wrapperData.pagesArray.length).toBe(12);
|
|
|
|
expect(wrapperData.firstBlockPages.length).toBe(1);
|
|
|
|
expect(wrapperData.middleBlockPages.length).toBe(3);
|
|
|
|
expect(wrapperData.lastBlockPages.length).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('inits correctly with totalPageCount equals 10 and current pageNumber in last block', () => {
|
|
|
|
const wrapper = shallowMount(Pagination, {
|
|
|
|
propsData: {
|
|
|
|
totalPageCount: 13,
|
2019-09-13 15:58:18 +01:00
|
|
|
onPageClickCallback: () => new Promise(() => false),
|
2019-08-09 13:51:28 +01:00
|
|
|
},
|
|
|
|
mocks: {
|
|
|
|
$route: {
|
|
|
|
query: {
|
2019-09-13 15:58:18 +01:00
|
|
|
pageNumber: 12,
|
|
|
|
},
|
2019-08-09 13:51:28 +01:00
|
|
|
},
|
|
|
|
$router: {
|
2019-09-13 15:58:18 +01:00
|
|
|
replace: () => false,
|
|
|
|
},
|
|
|
|
},
|
2019-08-09 13:51:28 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const wrapperData = wrapper.vm.$data;
|
|
|
|
|
|
|
|
expect(wrapperData.currentPageNumber).toBe(12);
|
|
|
|
expect(wrapperData.pagesArray.length).toBe(13);
|
|
|
|
expect(wrapperData.firstBlockPages.length).toBe(1);
|
|
|
|
expect(wrapperData.middleBlockPages.length).toBe(0);
|
|
|
|
expect(wrapperData.lastBlockPages.length).toBe(3);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('inits correctly with totalPageCount equals 4 and no current pageNumber in query', () => {
|
|
|
|
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
|
|
|
},
|
|
|
|
mocks: {
|
|
|
|
$route: {
|
|
|
|
query: {
|
2019-09-13 15:58:18 +01:00
|
|
|
pageNumber: null,
|
|
|
|
},
|
2019-08-09 13:51:28 +01:00
|
|
|
},
|
|
|
|
$router: {
|
2019-09-13 15:58:18 +01:00
|
|
|
replace: () => 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 routerReplaceSpy = sinon.spy();
|
|
|
|
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
|
|
|
},
|
|
|
|
mocks: {
|
|
|
|
$route: {
|
|
|
|
query: {
|
2019-09-13 15:58:18 +01:00
|
|
|
pageNumber: null,
|
|
|
|
},
|
2019-08-09 13:51:28 +01:00
|
|
|
},
|
|
|
|
$router: {
|
2019-09-13 15:58:18 +01:00
|
|
|
replace: routerReplaceSpy,
|
|
|
|
},
|
|
|
|
},
|
2019-08-09 13:51:28 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const wrapperData = wrapper.vm.$data;
|
|
|
|
|
|
|
|
wrapper.findAll('span').at(2).trigger('click');
|
|
|
|
await expect(callbackSpy.callCount).toBe(1);
|
|
|
|
|
|
|
|
expect(routerReplaceSpy.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 routerReplaceSpy = sinon.spy();
|
|
|
|
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
|
|
|
},
|
|
|
|
mocks: {
|
|
|
|
$route: {
|
|
|
|
query: {
|
2019-09-13 15:58:18 +01:00
|
|
|
pageNumber: null,
|
|
|
|
},
|
2019-08-09 13:51:28 +01:00
|
|
|
},
|
|
|
|
$router: {
|
2019-09-13 15:58:18 +01:00
|
|
|
replace: routerReplaceSpy,
|
|
|
|
},
|
|
|
|
},
|
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(routerReplaceSpy.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 routerReplaceSpy = sinon.spy();
|
|
|
|
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
|
|
|
},
|
|
|
|
mocks: {
|
|
|
|
$route: {
|
|
|
|
query: {
|
2019-09-13 15:58:18 +01:00
|
|
|
pageNumber: 8,
|
|
|
|
},
|
2019-08-09 13:51:28 +01:00
|
|
|
},
|
|
|
|
$router: {
|
2019-09-13 15:58:18 +01:00
|
|
|
replace: routerReplaceSpy,
|
|
|
|
},
|
|
|
|
},
|
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(1);
|
|
|
|
|
|
|
|
expect(routerReplaceSpy.callCount).toBe(2);
|
|
|
|
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 routerReplaceSpy = sinon.spy();
|
|
|
|
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
|
|
|
},
|
|
|
|
mocks: {
|
|
|
|
$route: {
|
|
|
|
query: {
|
2019-09-13 15:58:18 +01:00
|
|
|
pageNumber: null,
|
|
|
|
},
|
2019-08-09 13:51:28 +01:00
|
|
|
},
|
|
|
|
$router: {
|
2019-09-13 15:58:18 +01:00
|
|
|
replace: routerReplaceSpy,
|
|
|
|
},
|
|
|
|
},
|
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(routerReplaceSpy.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 routerReplaceSpy = sinon.spy();
|
|
|
|
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
|
|
|
},
|
|
|
|
mocks: {
|
|
|
|
$route: {
|
|
|
|
query: {
|
2019-09-13 15:58:18 +01:00
|
|
|
pageNumber: 9,
|
|
|
|
},
|
2019-08-09 13:51:28 +01:00
|
|
|
},
|
|
|
|
$router: {
|
2019-09-13 15:58:18 +01:00
|
|
|
replace: routerReplaceSpy,
|
|
|
|
},
|
|
|
|
},
|
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(0);
|
|
|
|
|
|
|
|
expect(routerReplaceSpy.callCount).toBe(1);
|
|
|
|
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
|
|
|
},
|
|
|
|
mocks: {
|
|
|
|
$route: {
|
|
|
|
query: {
|
2019-09-13 15:58:18 +01:00
|
|
|
pageNumber: null,
|
|
|
|
},
|
2019-08-19 12:20:38 +01:00
|
|
|
},
|
|
|
|
$router: {
|
2019-09-13 15:58:18 +01:00
|
|
|
replace: () => false,
|
|
|
|
},
|
|
|
|
},
|
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
|
|
|
},
|
|
|
|
mocks: {
|
|
|
|
$route: {
|
|
|
|
query: {
|
2019-09-13 15:58:18 +01:00
|
|
|
pageNumber: null,
|
|
|
|
},
|
2019-08-19 12:20:38 +01:00
|
|
|
},
|
|
|
|
$router: {
|
2019-09-13 15:58:18 +01:00
|
|
|
replace: () => false,
|
|
|
|
},
|
|
|
|
},
|
2019-08-19 12:20:38 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
await wrapper.vm.nextPage();
|
|
|
|
|
|
|
|
expect(wrapper.vm.$data.currentPageNumber).toBe(2);
|
|
|
|
|
|
|
|
wrapper.setProps({totalPageCount: 7});
|
|
|
|
|
|
|
|
const wrapperData = wrapper.vm.$data;
|
|
|
|
|
|
|
|
expect(wrapperData.currentPageNumber).toBe(1);
|
|
|
|
expect(wrapperData.pagesArray.length).toBe(7);
|
|
|
|
});
|
2019-08-09 13:51:28 +01:00
|
|
|
});
|