salix-front/test/vitest/__tests__/components/Paginate.spec.js

125 lines
3.8 KiB
JavaScript
Raw Normal View History

2023-01-03 13:17:22 +00:00
import { vi, describe, expect, it, beforeAll, afterEach } from 'vitest';
import { createWrapper, axios } from 'app/test/vitest/helper';
import Paginate from 'components/PaginateData.vue';
2022-12-20 11:30:25 +00:00
describe('Paginate', () => {
const expectedUrl = '/api/customers';
2023-02-24 12:21:37 +00:00
2022-12-20 11:30:25 +00:00
let vm;
beforeAll(() => {
const options = {
attrs: {
url: expectedUrl,
2023-02-24 12:21:37 +00:00
dataKey: 'CustomerList',
order: 'id DESC',
limit: 3,
2022-12-22 12:46:43 +00:00
},
2022-12-20 11:30:25 +00:00
};
vm = createWrapper(Paginate, options).vm;
});
afterEach(() => {
2023-02-24 12:21:37 +00:00
vm.store.data = [];
2022-12-20 11:30:25 +00:00
vm.pagination.page = 1;
vm.hasMoreData = true;
2022-12-22 12:46:43 +00:00
});
2022-12-20 11:30:25 +00:00
describe('paginate()', () => {
it('should call to the paginate() method and set the data on the rows property', async () => {
2023-02-24 12:21:37 +00:00
vi.spyOn(vm.arrayData, 'loadMore');
vm.store.data = [
{ id: 1, name: 'Tony Stark' },
{ id: 2, name: 'Jessica Jones' },
{ id: 3, name: 'Bruce Wayne' },
];
2022-12-20 11:30:25 +00:00
await vm.paginate();
2023-02-24 12:21:37 +00:00
expect(vm.arrayData.loadMore).toHaveBeenCalledWith();
expect(vm.store.data.length).toEqual(3);
2022-12-20 11:30:25 +00:00
});
it('should call to the paginate() method and then call it again to paginate', async () => {
2023-02-24 12:21:37 +00:00
vi.spyOn(axios, 'get').mockResolvedValue({
data: [
{ id: 1, name: 'Tony Stark' },
{ id: 2, name: 'Jessica Jones' },
{ id: 3, name: 'Bruce Wayne' },
],
});
vm.arrayData.hasMoreData.value = true;
vm.store.data = [
{ id: 1, name: 'Tony Stark' },
{ id: 2, name: 'Jessica Jones' },
{ id: 3, name: 'Bruce Wayne' },
];
2022-12-20 11:30:25 +00:00
await vm.paginate();
2023-02-24 12:21:37 +00:00
expect(vm.store.skip).toEqual(0);
expect(vm.store.data.length).toEqual(6);
2022-12-20 11:30:25 +00:00
await vm.paginate();
2023-02-24 12:21:37 +00:00
expect(vm.store.skip).toEqual(3);
expect(vm.store.data.length).toEqual(9);
2022-12-20 11:30:25 +00:00
});
});
describe('onLoad()', () => {
it('should call to the done() callback and not increment the pagination', async () => {
const index = 1;
2023-01-03 13:17:22 +00:00
const done = vi.fn();
2022-12-20 11:30:25 +00:00
await vm.onLoad(index, done);
expect(vm.pagination.page).toEqual(1);
expect(done).toHaveBeenCalledWith(false);
});
it('should increment the pagination and then call to the done() callback', async () => {
expect(vm.pagination.page).toEqual(1);
const index = 1;
2023-01-03 13:17:22 +00:00
const done = vi.fn();
2023-02-24 12:21:37 +00:00
vm.store.data = [
{ id: 1, name: 'Tony Stark' },
{ id: 2, name: 'Jessica Jones' },
{ id: 3, name: 'Bruce Wayne' },
];
2022-12-20 11:30:25 +00:00
await vm.onLoad(index, done);
expect(vm.pagination.page).toEqual(2);
expect(done).toHaveBeenCalledWith(false);
});
it('should call to the done() callback with true as argument to finish pagination', async () => {
2023-01-03 13:17:22 +00:00
vi.spyOn(axios, 'get').mockResolvedValue({
2022-12-20 11:30:25 +00:00
data: [
{ id: 1, name: 'Tony Stark' },
2022-12-22 12:46:43 +00:00
{ id: 2, name: 'Jessica Jones' },
],
2022-12-20 11:30:25 +00:00
});
2023-02-24 12:21:37 +00:00
vm.store.data = [
2022-12-20 11:30:25 +00:00
{ id: 1, name: 'Tony Stark' },
{ id: 2, name: 'Jessica Jones' },
{ id: 3, name: 'Bruce Wayne' },
];
expect(vm.pagination.page).toEqual(1);
const index = 1;
2023-01-03 13:17:22 +00:00
const done = vi.fn();
2022-12-20 11:30:25 +00:00
vm.hasMoreData = false;
await vm.onLoad(index, done);
expect(vm.pagination.page).toEqual(2);
expect(done).toHaveBeenCalledWith(true);
});
});
});