#6919 syncData #941

Merged
jorgep merged 95 commits from 6919-syncData into dev 2025-02-06 09:39:49 +00:00
1 changed files with 25 additions and 4 deletions
Showing only changes of commit 169389b5b8 - Show all commits

View File

@ -16,7 +16,7 @@ describe('useArrayData', () => {
vi.clearAllMocks(); 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: [] }); vi.spyOn(axios, 'get').mockReturnValueOnce({ data: [] });
const arrayData = useArrayData('ArrayData', { url: 'mockUrl' }); const arrayData = useArrayData('ArrayData', { url: 'mockUrl' });
@ -33,11 +33,11 @@ describe('useArrayData', () => {
}); });
expect(routerReplace.path).toEqual('mockSection/list'); expect(routerReplace.path).toEqual('mockSection/list');
expect(JSON.parse(routerReplace.query.params)).toEqual( 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 }] }); vi.spyOn(axios, 'get').mockReturnValueOnce({ data: [{ id: 1 }] });
const arrayData = useArrayData('ArrayData', { url: 'mockUrl', navigate: {} }); const arrayData = useArrayData('ArrayData', { url: 'mockUrl', navigate: {} });
@ -56,7 +56,7 @@ describe('useArrayData', () => {
expect(routerPush.query).toBeUndefined(); 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(axios, 'get').mockReturnValueOnce({ data: [{ id: 1 }, { id: 2 }] });
vi.spyOn(vueRouter, 'useRoute').mockReturnValue({ vi.spyOn(vueRouter, 'useRoute').mockReturnValue({
@ -95,4 +95,25 @@ describe('useArrayData', () => {
expect(routerPush.path).toEqual('mockName/'); expect(routerPush.path).toEqual('mockName/');
expect(routerPush.query.params).toBeDefined(); 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();
});
}); });