From 169389b5b80695fa15ac68ac5891d7d41a43e55f Mon Sep 17 00:00:00 2001 From: jorgep Date: Thu, 30 Jan 2025 17:51:18 +0100 Subject: [PATCH] test: refs #6919 fix typos in useArrayData tests and add new test cases for single record handling --- .../__tests__/useArrayData.spec.js | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/composables/__tests__/useArrayData.spec.js b/src/composables/__tests__/useArrayData.spec.js index d4c5d0949..a610ba9eb 100644 --- a/src/composables/__tests__/useArrayData.spec.js +++ b/src/composables/__tests__/useArrayData.spec.js @@ -16,7 +16,7 @@ describe('useArrayData', () => { vi.clearAllMocks(); }); - it('should fetch and repalce url with new params', async () => { + it('should fetch and replace url with new params', async () => { vi.spyOn(axios, 'get').mockReturnValueOnce({ data: [] }); const arrayData = useArrayData('ArrayData', { url: 'mockUrl' }); @@ -33,11 +33,11 @@ describe('useArrayData', () => { }); expect(routerReplace.path).toEqual('mockSection/list'); expect(JSON.parse(routerReplace.query.params)).toEqual( - expect.objectContaining(params) + expect.objectContaining(params), ); }); - it('Should get data and send new URL without keeping parameters, if there is only one record', async () => { + it('should get data and send new URL without keeping parameters, if there is only one record', async () => { vi.spyOn(axios, 'get').mockReturnValueOnce({ data: [{ id: 1 }] }); const arrayData = useArrayData('ArrayData', { url: 'mockUrl', navigate: {} }); @@ -56,7 +56,7 @@ describe('useArrayData', () => { expect(routerPush.query).toBeUndefined(); }); - it('Should get data and send new URL keeping parameters, if you have more than one record', async () => { + it('should get data and send new URL keeping parameters, if you have more than one record', async () => { vi.spyOn(axios, 'get').mockReturnValueOnce({ data: [{ id: 1 }, { id: 2 }] }); vi.spyOn(vueRouter, 'useRoute').mockReturnValue({ @@ -95,4 +95,25 @@ describe('useArrayData', () => { expect(routerPush.path).toEqual('mockName/'); expect(routerPush.query.params).toBeDefined(); }); + + it('should return one record', async () => { + vi.spyOn(axios, 'get').mockReturnValueOnce({ + data: [ + { id: 1, name: 'Entity 1' }, + { id: 2, name: 'Entity 2' }, + ], + }); + const arrayData = useArrayData('ArrayData', { url: 'mockUrl', oneRecord: true }); + await arrayData.fetch({}); + + expect(arrayData.store.data).toEqual({ id: 1, name: 'Entity 1' }); + }); + + it('should handle empty data gracefully if has to return one record', async () => { + vi.spyOn(axios, 'get').mockReturnValueOnce({ data: [] }); + const arrayData = useArrayData('ArrayData', { url: 'mockUrl', oneRecord: true }); + await arrayData.fetch({}); + + expect(arrayData.store.data).toBeUndefined(); + }); });