storj/web/satellite/tests/unit/common/VPagination.spec.ts
Egon Elbre 8620532a05 web/satellite: bump dependencies
This contains also multiple fixes to make it work.
The following is a non-exhaustive list.

When @Prop default value is a callback, then it is called
instead of set verbatim. This means, when you want a default
value to be a callback, then it needs to be `default: () => () => X`.

jest does not yet properly support WebWorkers, hence the code introduces
an indirection to provide the worker URL.

This in turn required removing the global "store" dependency from
the tests. As a consequence the new NotificatorPlugin takes store
as a dependency. And many of the tests are adjusted to not import
store directly.

Moved StoreModule definition to avoid initializing the global store.

Some of the router code was moved into store. We can later figure out
how to structure it better and move it back.

bip39 needs explicit fallbacks for some of the dependencies.

Fixes to timer mocking. jest supports it natively.

Remove sinon dependency. jest provides all the functionality we need.

Change-Id: I7af3599390c63ce9f99dbd0b1e0870e9f8ca994d
2022-05-04 15:02:01 +00:00

210 lines
6.9 KiB
TypeScript

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
import Pagination from '@/components/common/VPagination.vue';
import { mount, shallowMount } from '@vue/test-utils';
describe('Pagination.vue', () => {
it('renders correctly', () => {
const wrapper = shallowMount<Pagination>(Pagination);
expect(wrapper).toMatchSnapshot();
});
it('renders correctly with props', () => {
const wrapper = shallowMount<Pagination>(Pagination, {
propsData: {
totalPageCount: 10,
onPageClickCallback: () => new Promise(() => false),
},
});
expect(wrapper).toMatchSnapshot();
});
it('inits correctly with totalPageCount equals 10 and current pageNumber in first block', async () => {
const wrapper = mount<Pagination>(Pagination, {
propsData: {
totalPageCount: 10,
onPageClickCallback: () => new Promise(() => false),
},
});
const wrapperData = await wrapper.vm.$data;
expect(wrapperData.currentPageNumber).toBe(1);
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(0).classes().includes('selected')).toBe(true);
});
it('inits correctly with totalPageCount equals 4', () => {
const wrapper = shallowMount<Pagination>(Pagination, {
propsData: {
totalPageCount: 4,
onPageClickCallback: () => new Promise(() => false),
},
});
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 = jest.fn();
const wrapper = mount<Pagination>(Pagination, {
propsData: {
totalPageCount: 9,
onPageClickCallback: callbackSpy,
},
});
const wrapperData = await wrapper.vm.$data;
await wrapper.findAll('span').at(2).trigger('click');
await expect(callbackSpy).toHaveBeenCalledTimes(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 = jest.fn();
const wrapper = mount<Pagination>(Pagination, {
propsData: {
totalPageCount: 9,
onPageClickCallback: callbackSpy,
},
});
const wrapperData = wrapper.vm.$data;
wrapper.findAll('.pagination-container__button').at(1).trigger('click');
await expect(callbackSpy).toHaveBeenCalledTimes(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 = jest.fn();
const wrapper = mount<Pagination>(Pagination, {
propsData: {
totalPageCount: 9,
onPageClickCallback: callbackSpy,
},
});
await wrapper.vm.onPageClick(8);
const wrapperData = wrapper.vm.$data;
wrapper.findAll('.pagination-container__button').at(0).trigger('click');
await expect(callbackSpy).toHaveBeenCalledTimes(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 callbackSpy = jest.fn();
const wrapper = mount<Pagination>(Pagination, {
propsData: {
totalPageCount: 9,
onPageClickCallback: callbackSpy,
},
});
const wrapperData = wrapper.vm.$data;
wrapper.findAll('.pagination-container__button').at(0).trigger('click');
await expect(callbackSpy).toHaveBeenCalledTimes(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 = jest.fn();
const wrapper = mount<Pagination>(Pagination, {
propsData: {
totalPageCount: 9,
onPageClickCallback: callbackSpy,
},
});
const wrapperData = wrapper.vm.$data;
await wrapper.vm.onPageClick(9);
wrapper.findAll('.pagination-container__button').at(1).trigger('click');
await expect(callbackSpy).toHaveBeenCalledTimes(1);
expect(wrapperData.currentPageNumber).toBe(9);
expect(wrapperData.firstBlockPages.length).toBe(1);
expect(wrapperData.middleBlockPages.length).toBe(0);
expect(wrapperData.lastBlockPages.length).toBe(3);
});
it('should reset current page index to 1', async () => {
const wrapper = shallowMount<Pagination>(Pagination, {
propsData: {
totalPageCount: 4,
onPageClickCallback: () => Promise.resolve({}),
},
});
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>(Pagination, {
propsData: {
totalPageCount: 4,
onPageClickCallback: () => Promise.resolve({}),
},
});
await wrapper.vm.nextPage();
expect(wrapper.vm.$data.currentPageNumber).toBe(2);
await wrapper.setProps({totalPageCount: 7});
const wrapperData = wrapper.vm.$data;
expect(wrapperData.currentPageNumber).toBe(1);
expect(wrapperData.pagesArray.length).toBe(7);
});
});