From ed7428268061ef6366d828106dafca1199cb8a1c Mon Sep 17 00:00:00 2001 From: joan Date: Fri, 24 Feb 2023 13:21:37 +0100 Subject: [PATCH] Updated unit test --- src/components/PaginateData.vue | 13 --- .../__tests__/components/Paginate.spec.js | 90 ++++++++----------- test/vitest/helper.js | 6 +- 3 files changed, 40 insertions(+), 69 deletions(-) diff --git a/src/components/PaginateData.vue b/src/components/PaginateData.vue index 88f2157ca..2c091645c 100644 --- a/src/components/PaginateData.vue +++ b/src/components/PaginateData.vue @@ -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) { diff --git a/test/vitest/__tests__/components/Paginate.spec.js b/test/vitest/__tests__/components/Paginate.spec.js index dd6f31478..cb0109960 100644 --- a/test/vitest/__tests__/components/Paginate.spec.js +++ b/test/vitest/__tests__/components/Paginate.spec.js @@ -4,82 +4,65 @@ 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; - - vi.spyOn(axios, 'get').mockResolvedValue({ - data: [ - { id: 1, name: 'Tony Stark' }, - { id: 2, name: 'Jessica Jones' }, - { id: 3, name: 'Bruce Wayne' }, - ], - }); }); afterEach(() => { - vm.rows = []; + 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 () => { - const expectedOptions = { - params: { - filter: { - order: 'id DESC', - limit: 3, - skip: 0, - }, - }, - }; + 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(axios.get).toHaveBeenCalledWith(expectedUrl, expectedOptions); - expect(vm.rows.length).toEqual(3); + 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 () => { - const expectedOptions = { - params: { - filter: { - order: 'id DESC', - limit: 3, - skip: 0, - }, - }, - }; + 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' }, + ]; 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; + expect(vm.store.skip).toEqual(0); + expect(vm.store.data.length).toEqual(6); 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' }, diff --git a/test/vitest/helper.js b/test/vitest/helper.js index 13987080d..6fadc4609 100644 --- a/test/vitest/helper.js +++ b/test/vitest/helper.js @@ -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],