Updated unit test
gitea/salix-front/pipeline/head There was a failure building this commit Details

This commit is contained in:
Joan Sanchez 2023-02-24 13:21:37 +01:00
parent c16df3efbc
commit ed74282680
3 changed files with 40 additions and 69 deletions

View File

@ -86,19 +86,6 @@ async function paginate() {
if (!props.url) return; if (!props.url) return;
// const filter = {
// limit: rowsPerPage,
// skip: rowsPerPage * (page - 1),
// };
// Object.assign(filter, props.filter);
// if (props.where) filter.where = props.where;
// if (props.sortBy) filter.order = props.sortBy;
// if (props.limit) filter.limit = props.limit;
// if (sortBy) filter.order = sortBy;
await arrayData.loadMore(); await arrayData.loadMore();
if (!arrayData.hasMoreData.value) { if (!arrayData.hasMoreData.value) {

View File

@ -4,82 +4,65 @@ import Paginate from 'components/PaginateData.vue';
describe('Paginate', () => { describe('Paginate', () => {
const expectedUrl = '/api/customers'; const expectedUrl = '/api/customers';
let vm; let vm;
beforeAll(() => { beforeAll(() => {
const options = { const options = {
attrs: { attrs: {
url: expectedUrl, url: expectedUrl,
sortBy: 'id DESC', dataKey: 'CustomerList',
rowsPerPage: 3, order: 'id DESC',
limit: 3,
}, },
}; };
vm = createWrapper(Paginate, options).vm; vm = createWrapper(Paginate, options).vm;
vi.spyOn(axios, 'get').mockResolvedValue({
data: [
{ id: 1, name: 'Tony Stark' },
{ id: 2, name: 'Jessica Jones' },
{ id: 3, name: 'Bruce Wayne' },
],
});
}); });
afterEach(() => { afterEach(() => {
vm.rows = []; vm.store.data = [];
vm.pagination.page = 1; vm.pagination.page = 1;
vm.hasMoreData = true; vm.hasMoreData = true;
}); });
describe('paginate()', () => { describe('paginate()', () => {
it('should call to the paginate() method and set the data on the rows property', async () => { it('should call to the paginate() method and set the data on the rows property', async () => {
const expectedOptions = { vi.spyOn(vm.arrayData, 'loadMore');
params: { vm.store.data = [
filter: { { id: 1, name: 'Tony Stark' },
order: 'id DESC', { id: 2, name: 'Jessica Jones' },
limit: 3, { id: 3, name: 'Bruce Wayne' },
skip: 0, ];
},
},
};
await vm.paginate(); await vm.paginate();
expect(axios.get).toHaveBeenCalledWith(expectedUrl, expectedOptions); expect(vm.arrayData.loadMore).toHaveBeenCalledWith();
expect(vm.rows.length).toEqual(3); expect(vm.store.data.length).toEqual(3);
}); });
it('should call to the paginate() method and then call it again to paginate', async () => { it('should call to the paginate() method and then call it again to paginate', async () => {
const expectedOptions = { vi.spyOn(axios, 'get').mockResolvedValue({
params: { data: [
filter: { { id: 1, name: 'Tony Stark' },
order: 'id DESC', { id: 2, name: 'Jessica Jones' },
limit: 3, { id: 3, name: 'Bruce Wayne' },
skip: 0, ],
}, });
}, vm.arrayData.hasMoreData.value = true;
}; vm.store.data = [
{ id: 1, name: 'Tony Stark' },
{ id: 2, name: 'Jessica Jones' },
{ id: 3, name: 'Bruce Wayne' },
];
await vm.paginate(); await vm.paginate();
expect(axios.get).toHaveBeenCalledWith(expectedUrl, expectedOptions); expect(vm.store.skip).toEqual(0);
expect(vm.rows.length).toEqual(3); expect(vm.store.data.length).toEqual(6);
const expectedOptionsPaginated = {
params: {
filter: {
order: 'id DESC',
limit: 3,
skip: 3,
},
},
};
vm.pagination.page = 2;
await vm.paginate(); await vm.paginate();
expect(axios.get).toHaveBeenCalledWith(expectedUrl, expectedOptionsPaginated); expect(vm.store.skip).toEqual(3);
expect(vm.rows.length).toEqual(6); expect(vm.store.data.length).toEqual(9);
}); });
}); });
@ -95,16 +78,15 @@ describe('Paginate', () => {
}); });
it('should increment the pagination and then call to the done() callback', async () => { 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); expect(vm.pagination.page).toEqual(1);
const index = 1; const index = 1;
const done = vi.fn(); const done = vi.fn();
vm.store.data = [
{ id: 1, name: 'Tony Stark' },
{ id: 2, name: 'Jessica Jones' },
{ id: 3, name: 'Bruce Wayne' },
];
await vm.onLoad(index, done); await vm.onLoad(index, done);
@ -120,7 +102,7 @@ describe('Paginate', () => {
], ],
}); });
vm.rows = [ vm.store.data = [
{ id: 1, name: 'Tony Stark' }, { id: 1, name: 'Tony Stark' },
{ id: 2, name: 'Jessica Jones' }, { id: 2, name: 'Jessica Jones' },
{ id: 3, name: 'Bruce Wayne' }, { id: 3, name: 'Bruce Wayne' },

View File

@ -13,7 +13,9 @@ installQuasar({
}, },
}); });
const pinia = createTestingPinia({ createSpy: vi.fn, stubActions: false });
const mockPush = vi.fn(); const mockPush = vi.fn();
vi.mock('vue-router', () => ({ vi.mock('vue-router', () => ({
useRouter: () => ({ useRouter: () => ({
push: mockPush, push: mockPush,
@ -21,12 +23,12 @@ vi.mock('vue-router', () => ({
}), }),
useRoute: () => ({ useRoute: () => ({
matched: [], matched: [],
query: {},
params: {},
}), }),
})); }));
export function createWrapper(component, options) { export function createWrapper(component, options) {
const pinia = createTestingPinia({ createSpy: vi.fn, stubActions: false });
const defaultOptions = { const defaultOptions = {
global: { global: {
plugins: [i18n, pinia], plugins: [i18n, pinia],