forked from verdnatura/salix-front
152 lines
4.3 KiB
JavaScript
152 lines
4.3 KiB
JavaScript
|
import { jest, describe, expect, it, beforeAll } from '@jest/globals';
|
||
|
import { createWrapper, axios } from 'app/tests/jest/jestHelpers';
|
||
|
import Paginate from '../PaginateData.vue';
|
||
|
|
||
|
const mockPush = jest.fn();
|
||
|
|
||
|
jest.mock('vue-router', () => ({
|
||
|
useRouter: () => ({
|
||
|
push: mockPush,
|
||
|
currentRoute: { value: 'myCurrentRoute' }
|
||
|
}),
|
||
|
}));
|
||
|
|
||
|
describe('Paginate', () => {
|
||
|
const expectedUrl = '/api/customers';
|
||
|
let vm;
|
||
|
beforeAll(() => {
|
||
|
const options = {
|
||
|
attrs: {
|
||
|
url: expectedUrl,
|
||
|
sortBy: 'id DESC',
|
||
|
rowsPerPage: 3
|
||
|
}
|
||
|
};
|
||
|
vm = createWrapper(Paginate, options).vm;
|
||
|
|
||
|
jest.spyOn(axios, 'get').mockResolvedValue({
|
||
|
data: [
|
||
|
{ id: 1, name: 'Tony Stark' },
|
||
|
{ id: 2, name: 'Jessica Jones' },
|
||
|
{ id: 3, name: 'Bruce Wayne' },
|
||
|
]
|
||
|
});
|
||
|
});
|
||
|
|
||
|
afterEach(() => {
|
||
|
vm.rows = [];
|
||
|
vm.pagination.page = 1;
|
||
|
vm.hasMoreData = true;
|
||
|
})
|
||
|
|
||
|
describe('paginate()', () => {
|
||
|
it('should call to the paginate() method and set the data on the rows property', async () => {
|
||
|
const expectedOptions = {
|
||
|
params: {
|
||
|
filter: {
|
||
|
order: 'id DESC',
|
||
|
limit: 3,
|
||
|
skip: 0
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
|
||
|
await vm.paginate();
|
||
|
|
||
|
expect(axios.get).toHaveBeenCalledWith(expectedUrl, expectedOptions);
|
||
|
expect(vm.rows.length).toEqual(3);
|
||
|
});
|
||
|
|
||
|
it('should call to the paginate() method and then call it again to paginate', async () => {
|
||
|
const expectedOptions = {
|
||
|
params: {
|
||
|
filter: {
|
||
|
order: 'id DESC',
|
||
|
limit: 3,
|
||
|
skip: 0
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
|
||
|
await vm.paginate();
|
||
|
|
||
|
expect(axios.get).toHaveBeenCalledWith(expectedUrl, expectedOptions);
|
||
|
expect(vm.rows.length).toEqual(3);
|
||
|
|
||
|
const expectedOptionsPaginated = {
|
||
|
params: {
|
||
|
filter: {
|
||
|
order: 'id DESC',
|
||
|
limit: 3,
|
||
|
skip: 3
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
|
||
|
vm.pagination.page = 2;
|
||
|
|
||
|
await vm.paginate();
|
||
|
|
||
|
expect(axios.get).toHaveBeenCalledWith(expectedUrl, expectedOptionsPaginated);
|
||
|
expect(vm.rows.length).toEqual(6);
|
||
|
});
|
||
|
});
|
||
|
|
||
|
describe('onLoad()', () => {
|
||
|
it('should call to the done() callback and not increment the pagination', async () => {
|
||
|
const index = 1;
|
||
|
const done = jest.fn();
|
||
|
|
||
|
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 () => {
|
||
|
vm.rows = [
|
||
|
{ id: 1, name: 'Tony Stark' },
|
||
|
{ id: 2, name: 'Jessica Jones' },
|
||
|
{ id: 3, name: 'Bruce Wayne' },
|
||
|
];
|
||
|
|
||
|
expect(vm.pagination.page).toEqual(1);
|
||
|
|
||
|
const index = 1;
|
||
|
const done = jest.fn();
|
||
|
|
||
|
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 () => {
|
||
|
jest.spyOn(axios, 'get').mockResolvedValue({
|
||
|
data: [
|
||
|
{ id: 1, name: 'Tony Stark' },
|
||
|
{ id: 2, name: 'Jessica Jones' }
|
||
|
]
|
||
|
});
|
||
|
|
||
|
vm.rows = [
|
||
|
{ id: 1, name: 'Tony Stark' },
|
||
|
{ id: 2, name: 'Jessica Jones' },
|
||
|
{ id: 3, name: 'Bruce Wayne' },
|
||
|
];
|
||
|
|
||
|
expect(vm.pagination.page).toEqual(1);
|
||
|
|
||
|
const index = 1;
|
||
|
const done = jest.fn();
|
||
|
|
||
|
vm.hasMoreData = false;
|
||
|
|
||
|
await vm.onLoad(index, done);
|
||
|
|
||
|
expect(vm.pagination.page).toEqual(2);
|
||
|
expect(done).toHaveBeenCalledWith(true);
|
||
|
});
|
||
|
});
|
||
|
});
|