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;
// 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();
if (!arrayData.hasMoreData.value) {

View File

@ -4,17 +4,42 @@ import Paginate from 'components/PaginateData.vue';
describe('Paginate', () => {
const expectedUrl = '/api/customers';
let vm;
beforeAll(() => {
const options = {
attrs: {
url: expectedUrl,
sortBy: 'id DESC',
rowsPerPage: 3,
dataKey: 'CustomerList',
order: 'id DESC',
limit: 3,
},
};
vm = createWrapper(Paginate, options).vm;
});
afterEach(() => {
vm.store.data = [];
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 () => {
vi.spyOn(vm.arrayData, 'loadMore');
vm.store.data = [
{ id: 1, name: 'Tony Stark' },
{ id: 2, name: 'Jessica Jones' },
{ id: 3, name: 'Bruce Wayne' },
];
await vm.paginate();
expect(vm.arrayData.loadMore).toHaveBeenCalledWith();
expect(vm.store.data.length).toEqual(3);
});
it('should call to the paginate() method and then call it again to paginate', async () => {
vi.spyOn(axios, 'get').mockResolvedValue({
data: [
{ id: 1, name: 'Tony Stark' },
@ -22,64 +47,22 @@ describe('Paginate', () => {
{ 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,
},
},
};
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();
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,
},
},
};
expect(vm.store.skip).toEqual(0);
expect(vm.store.data.length).toEqual(6);
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);
expect(vm.store.skip).toEqual(3);
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 () => {
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 = 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);
@ -120,7 +102,7 @@ describe('Paginate', () => {
],
});
vm.rows = [
vm.store.data = [
{ id: 1, name: 'Tony Stark' },
{ id: 2, name: 'Jessica Jones' },
{ id: 3, name: 'Bruce Wayne' },

View File

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